2

The problem is:

Your program needs to decode an encrypted text file called "encrypted.txt". The person who wrote it used a cipher specified in "key.txt". This key file would look similar to the following:

A    B
B    C
C    D
D    E
E    F
F    G
G    H
H    I
I    J
J    K
K    L
L    M
M    N
N    O
O    P
P    Q
Q    R
R    S
S    T
T    U
U    V
V    W
W    X
X    Y
Y    Z
Z    A

The left column represents the plaintext letter, and the right column represents the corresponding ciphertext.

Your program should decode the "encrypted.txt" file using "key.txt" and write the plaintext to "decrypted.txt".

I have:

decrypt = ""
keyfile = open("key.txt", "r")
cipher = {}

for keyline in keyfile:
    cipher[keyline.split()[0]] = keyline.split()[1]
keyfile.close()
encrypt = []
encryptedfile = open("encrypted.txt", "r")
readlines = encryptedfile.readlines()
str = ""

for line in readlines:
    str = line
    letter = list(str)
    for i in range(len(letter)):
        if letter[i]==",":
            decrypt += ","
        elif letter[i] == ".":
            decrypt+= "."
        elif letter[i] == "!":
            decrypt += "!"
        elif letter[i] == " ":
            decrypt += " "
        else:
            found = False
            count = 0

while (found == False):
    if (letter[i] == keyline.split()[0][count]):
        decrypt += keyline.split()[1][count]
        found = True
        count += 1
        print decrypt

encryptedfile.close()
decryptedfile = open("decrypted.txt", "w")
decryptedfile.write(decrypt)
decryptedfile.close()

This is the Python language. The output does make a file titled decrypted.txt but the only thing that is in the file is an A, which to me does not make sense. For the problem it should output more than that right?

Maddie
  • 41
  • 3
  • 1
    Welcome to stackoverflow, can you be a bit more specific about what your question is, how your program fails? Also, we need to know what language you're using, can you edit your question to add tat information? – mmgross Apr 13 '15 at 19:09
  • If that is indeed your code, then the indention levels give it significantly different semantics than what you probably intended... And, in fact, in any file that contains only punctuation or whitespace, you'll get a `NameError` because you have a code path wherein you might enter your `while (found == False)` loop with `found` having never been defined... That may still be the case, even with indentation fixed. – twalberg Apr 13 '15 at 20:04

2 Answers2

1

Deciphering a file enrcypted.txt with the key given in key.txt and saving the result to decrypted.txt:

with open('key.txt', 'r') as keyfile:                                           
    pairs = [line.split() for line in keyfile]                                  
    columns = [''.join(x) for x in zip(*pairs)]                                 
    # Or to make it work with lower case letters as well:                       
    # columns = [''.join(x)+''.join(x).lower() for x in zip(*pairs)]            
    key = str.maketrans(                                                        
            *reversed(columns)                                                  
            )                                                                   

with open('encrypted.txt', 'r') as encrypted_file:                              
    decrypted = [line.translate(key) for line in encrypted_file]                

with open('decrypted.txt', 'w') as decrypted_file:
    decrypted_file.writelines(decrypted) 
karlson
  • 5,325
  • 3
  • 30
  • 62
0

Your while block should be indented three times.
Except count += 1 which should be one block less than its neighbours

And the inside of the while block doesn't use at all the fact that keyline is a dictionary.

Eric Levieil
  • 3,554
  • 2
  • 13
  • 18