Reputation: 33
Good day,
I have created a simple ASCII encryption program and I just have 3 questions about it:
Thank you here is my code and results below:
import time
key = "This■isôthe╝key¦b££glkHPAfgbm(*&%$$*(■ô▀"
string = "Encryption the hell out of me, even if I repeatttttttt lettersssss you can't tell"
entext = ""
detext = ""
keycnt=0
print("Displaing Text to be Encrypted")
time.sleep(1)
print(string)
time.sleep(5)
#Loops through the string and the key and adds the ascii values together to create a Encrypted character
for sLetter in string:
entext += chr(ord(sLetter) + ord(key[keycnt]))
keycnt += 1
if keycnt == len(key):
keycnt =0
print("Displaying encrypted Text")
time.sleep(1)
print(entext)
#Resetting key position
keycnt=0
#Loops through the string and the key and subtracts the ascii values together to create a decrypted character
for sLetter in entext:
detext += chr(ord(sLetter) - ord(key[keycnt]))
keycnt += 1
if keycnt == len(key):
keycnt =0
time.sleep(2)
print("Displaying decrypted Text")
time.sleep(1)
print(detext)
time.sleep(1)
Upvotes: 2
Views: 3973
Reputation: 94058
First of all, addition with characters of the key is not a good cipher, not even a good Caesar or Vigenère cipher. You would need modular addition for that: either modulus 26 for the normal alphabet (but without up- or lowercase and other characters) or modulus 256 for bytes. In the case of bytes you would need a random value for each byte of the key.
Currently your ciphertext has a bias: if you would add a character value with 0x00 with a key valued 0x00 then you will get 0x00 as ciphertext byte. The problem is that the value 0x00 is only ever reached with that particular combination in your encryption scheme. So if you see the value 0x00 then you will immediately know both the key and plaintext value.
How could I check if the key in entered incorrectly and tell my program not to attempt to decrypt if it has been entered incorrectly.
It is not possible to check if the value of the key is correct or not. The only thing you can do is to validate if the output is what you expect.
Modern cryptography uses a message authentication code (MAC) to create an authentication tag. This tag can be validated against the ciphertext and key (or, for a less secure scheme, plaintext and key). There are also authenticated modes of encryption such as GCM, which are basically ciphers with the MAC authentication build in.
Why is the encrypted text longer than the original?
If you add values with a value of 255 or lower then you will get values of 510 or lower. Those values however take two bytes to encode at least.
If I wanted to encrypt other things not ASCII text how hard would it be?
Not that hard: just perform XOR or modular addition (e.g. modulo 256 for 8 bits / one byte) with a truly random key. However, to create anything secure you would either use a one-time-pad (where the key is the same size as the binary plaintext) or a modern cipher.
Upvotes: 1