Alessandro Power
Alessandro Power

Reputation: 2472

Python - encryption and decryption scripts produce occasional errors

I made a Python script to encrypt plaintext files using the symmetric-key algorithm described in this video. I then created a second script to decrypt the encrypted message. Here is the original text:

I came, I saw, I conquered.

Here is the text after being encrypted and decrypted:

I came, I saw, I conquerdd.

Almost perfect, except for a single letter. For longer texts, there will be multiple letters which are just off ie the numerical representation of the character which appears is one lower than the numerical representation of the original character. I have no idea why this is.

Here's how my scripts work. First, I generated a random sequence of digits -- my PAD -- and saved it in the text file "pad.txt". I won't show the code because it is so straightforward. I then saved the text which I want to be encrypted in "text.txt". Next, I run the encryption script, which encrypts the text and saves it in the file "encryptedText.txt":

#!/usr/bin/python3.4
import string

def getPad():
    padString = open("pad.txt","r").read()
    pad = padString.split(" ")
    return pad

def encrypt(textToEncrypt,pad):
    encryptedText = ""
    possibleChars = string.printable[:98]   # last two elements are not used bec
                                            # ause they don't show up well on te
                                            # xt files.
    for i in range(len(textToEncrypt)):
        char = textToEncrypt[i]
        if char in possibleChars:
            num = possibleChars.index(char)
        else:
            return False
        encryptedNum = num + int(pad[(i)%len(pad)])
        if encryptedNum >= len(possibleChars):
            encryptedNum = encryptedNum - len(possibleChars)
        encryptedChar = possibleChars[encryptedNum]
        encryptedText = encryptedText + encryptedChar
    return encryptedText

if __name__ == "__main__":
    textToEncrypt = open("text.txt","r").read()
    pad = getPad()
    encryptedText = encrypt(textToEncrypt,pad)
    if not encryptedText:
        print("""An error occurred during the encryption process. Confirm that \
there are no forbidden symbols in your text.""")
    else:
        open("encryptedText.txt","w").write(encryptedText) 

Finally, I decrypt the text with this script:

#!/usr/bin/python3.4
import string

def getPad():
    padString = open("pad.txt","r").read()
    pad = padString.split(" ")
    return pad

def decrypt(textToDecrypt,pad):
    trueText = ""
    possibleChars = string.printable[:98]
    for i in range(len(textToDecrypt)):
        encryptedChar = textToDecrypt[i]
        encryptedNum = possibleChars.index(encryptedChar)
        trueNum = encryptedNum - int(pad[i%len(pad)])
        if trueNum < 0:
            trueNum = trueNum + len(possibleChars)
        trueChar = possibleChars[trueNum]
        trueText = trueText + trueChar
    return trueText

if __name__ == "__main__":
    pad = getPad()
    textToDecrypt = open("encryptedText.txt","r").read()
    trueText = decrypt(textToDecrypt,pad)
    open("decryptedText.txt","w").write(trueText)

Both scripts seem very straightforward, and they obvious work almost perfectly. However, every once in a while there is an error and I cannot see why.

Upvotes: 0

Views: 405

Answers (1)

Alessandro Power
Alessandro Power

Reputation: 2472

I found the solution to this problem. It turns out that every character that was not decrypted properly was encrypted to \r, which my text editor changed to a \n for whatever reason. Removing \r from the list of possible characters fixed the issue.

Upvotes: 1

Related Questions