Note that there are some explanatory texts on larger screens.

plurals
  1. POPython decryption with lists
    text
    copied!<p>I'm stuck with a decryption problem I'm having. I have a really basic cipher that is only the alphabet and is offset by 1 letter, like this:</p> <pre><code>A B B C C D D E to z to A </code></pre> <p>the right column is the letters I'm given, and I need to turn them to the letters on the left.</p> <p>I'm reading this from a file, and saving each column into a list like this</p> <pre><code>#!/usr/bin/python key = "key.txt" encrypted = "encrypted.txt" decrypted = "decrypted.txt" encryptedList = [] decryptedList = [] with open(key, "r") as file: for line in file: currentLine = line.split() currentDecrypted = currentLine[0] currentEncrypted = currentLine[1] decryptedList.append(currentEncrypted) encryptedList.append(currentDecrypted) file.close() counter = 0 with open(encrypted, "r") as file: for line in file: currentLine = line for letter in currentLine: currentLetter = letter for item in encryptedList: if(item == currentLetter): ####here's where the problem starts#### ####I've tried just printing counter, and I get mostly go printencryptedList[counter-1] counter = 0 break counter += 1 </code></pre> <p>what I'm trying to decrypt is a text file that looks like this</p> <pre><code>FMMP NZ OBNF JT KSMBB KPIO TVDLT BQQ GWWWWWWBMT PG DJJJJJH </code></pre> <p>I get the correct count numbers for FMMP, (5, 12, 12, 15) with counter -1, but then i get 39, 25,40 and so on. Any help would be greatly appreciated, and let me know if you need more info.</p> <p>I also welcome ideas on better/easier ways to do this, but I would also like a solution similar to this, so I can figure out what's going on here. Thanks</p> <p>Alright, thanks for all the answers and info. I'm posting what I finally did and it works. I'm sure it's not as pythonic as it should be, but I implemented a few things that people mentioned. Thanks for the info.</p> <pre><code>import string key = "key.txt" encrypted = ""encrypted.txt" decrypted = "decrypted.txt encryptedString = "" decryptedString = "" keyDict = {} with open(key, "r") as file: for line in file: currentLine = line.split() currentDecrypted = currentLine[0] currentEncrypted = currentLine[1] keyDict[currentDecrypted] = currentEncrypted with open(encrypted, "r") as file: for line in file: currentLine = line for letter in currentLine: currentLetter = letter encryptedString += letter for key in keyDict: if(keyDict[key] == letter): decryptedString += key break elif(letter == " "): decryptedString += " " break elif(letter == "\n"): decryptedString += "\n" break with open(decrypted, "a") as file: file.write(decryptedString) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload