0

I have been working on a program which will take a plaintext file, encrypt the message and then decrypt the message. When I decrypt the message, the commas within the text file are replaced with different ASCII characters. Does anybody have a solution or can help me on the problem?

Please note: I have contained my code within a pastebin due to the fact it is quite long and I don't know what parts I could post that specify the problem. Thank you!

Pastebin Link

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Matt Jones
  • 13
  • 1
  • 7
  • Welcome to the site! Check out the [tour](http://stackoverflow.com/tour) for an intro to asking questions that will likely receive answers, and for a badge :) . – cxw Feb 01 '16 at 14:03
  • Welcome to Stack Overflow! If you have a problem with your code, then you should provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that demonstrates your problem. Help us help you without wasting time on guessing. Your code must be on Stack Overflow and not hidden behind some link. Please [edit] your question to include the code here. – Artjom B. Feb 01 '16 at 20:09

1 Answers1

0

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.)

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81