2

Good day,

I have created a simple ASCII encryption program and I just have 3 questions about it:

  1. 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.
  2. Why is the encrypted text longer than the original?
  3. If I wanted to encrypt other things not ASCII text how hard would it be?

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your quick response. Your either answered my question or blew my mind or both. Well at least I have dipped my toes in the icy waters of encryption. Its was fun and in the future I make just import a pre-made cipher class for my programs. When I become more advanced I make try and make another more complex cipher in the future because its good to know how they work. Thanks again.
One possible way to check key correctness is to add padding at the end of the message before encryption. Unfortunately the standard PKCS#7 padding uses non-ASCII bytes, but could be adapted for ASCII only with a little work. Check the padding after decryption, and if it is incorrect, then you know the key, or something, is wrong.
@rossum No, that just works for the CBC block cipher mode of operation and then there is still a ~1/256 chance that the padding looks correct even if the key or data isn't. It would not work (well) for the cipher in the question or the one time pad.
Maarten, I disagree. You can add padding to, say, a stream cipher and check the padding after decryption to see if it matches what you expect. Even something as simple as "XzebraX" could be added before encryption and checked then removed after decryption. Yes, there is a small chance of the padding missing a problem.
You are quite illegible to disagree :) But note that a MAC is proven secure for modern crypto, and if can use a modern stream cipher you can certainly also use a modern MAC or authenticated mode of encryption. For classic Vigenere ciphers and the like, padding doesn't work that well. For instance, it is easy to miss mistakes if the key is larger than the padding (see the key in the question!). But yeah, sure, any known plaintext can give an rough indication - if it fails then something is definitely wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.