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.’
J.U.U.U 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)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x