简体   繁体   中英

comparing between two lists to decrypt a txt file

Blessings,

I have a text file which needs to be decrypted,

It contains the following:

'Puackich, hvhnkrally oaths phufhck. All ymr nhhd is Pykemn.' JUUU Kmltin. mmps iks nmk eio; ---> hkmu

The key to decrypt is this :

if the text holds the value of the following letters in the list > encrypted = ["a", "b", "d", "m", "f", "g", "r", "y", "i"]

it should be changed to the following > decrypted = ["m", "y", "c", "a", "t", "i", "s", "b", "g"]

while encrypted[1] == decrypted[1] and so forth

what I tried so far

encrypted = ["a", "b", "d", "m", "f", "g", "r", "y", "i"]
decrypted = ["m", "y", "c", "a", "t", "i", "s", "b", "g"]

new = []
counter = 0
x = len(encrypted)
with open("encrypt.txt") as f:
    lines = f.readlines()
    for i in lines: # will go over words in text
        for b in i: # will go over letters in text
            if b == encrypted[counter]: 
                counter += 1
                new.append[b]
                if counter == x + 1:
                    break
                else:
                    pass
print(new)

This is the approach I'd suggest:

encrypted = "abdmfgryi"
decrypted = "mycatisbg"
lookup = dict(zip(
    encrypted + encrypted.upper(),
    decrypted + decrypted.upper()
))

msg = (
    "'Puackich, hvhnkrally oaths phufhck. All ymr nhhd is Pykemn.' "
    "J.U.U.U Kmltin. mmps iks nmk eio; ---> hkmu"
)

print(''.join(lookup.get(c, c) for c in msg))

The lookup dict is created by zipping the encrypted and decrypted alphabets together into key: value pairs, producing:

{'a': 'm', 'b': 'y', 'd': 'c', 'm': 'a', 'f': 't', 'g': 'i', 'r': 's', 'y': 'b', 'i': 'g', 'A': 'M', 'B': 'Y', 'D': 'C', 'M': 'A', 'F': 'T', 'G': 'I', 'R': 'S', 'Y': 'B', 'I': 'G'}

Given this dict you can easily translate each encrypted letter to its corresponding letter, yielding the output:

'Pumckgch, hvhnksmllb omths phuthck. Mll bas nhhc gs Pbkean.' J.U.U.U Kaltgn. aaps gks nak ego; ---> hkau

which doesn't look very decrypted to me, but is the "correct" output given your cipher key.

A better cipher key might be:

encrypted = "ehkmortu"
decrypted = "hetomukr"

which yields the output:

'Practice, eventually makes perfect. All you need is Python.' J.R.R.R Tolkin. oops its not him; ---> etor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM