Unfortunately, I think you need to change your algorithm rather than merely fix a bug in your implementation. Your encryption algorithm appears to change only characters for which the ASCII value plus the finalNumber
exceeds 126. In general, this means you can't reliably tell whether a letter was encrypted or not. For example, in encrypt
, this block
added = finalNumber + valLetter
if added > 126:
subtracted = added - 94
encrypted += chr(subtracted)
else:
encrypted += chr(valLetter)
will make some letters overlap. Suppose finalNumber
is 30
. To encrypt 'a'
, which is ASCII 97, added=127
. Thus subtracted=33
, and '!'
will be written to encrypted
. However, when you encrypt a '!'
itself, added
=33+30
=63
, which is not greater than 126
, so the '!'
will be copied through. When you try to decrypt, you won't be able to tell whether a '!'
in the encrypted text should be decrypted to an 'a'
or a '!'
.
This question is about building a Caesar cipher in Python, which appears to be what you are trying to do. I recommend starting there. Good luck!
(note: not tested; this answer is based on reading the source only.)