如何在Python中解密?

时间:2017-04-03 06:49:35

标签: python encryption

我正在尝试解密我用其他程序加密的Python文件。有些字母被正确解密,而有些则没有。我不确定发生了什么。我基本上做的就是反转解密文件的代码。我认为它与迭代文本的方式有关,但我不确定如何解决它。

这是我的解密代码:

decryption_library = {'%':'A','9':'a','@':'B','#':'b','1':'C','2':'c','3':'D','4':'d',
                      '5':'E','6':'e','7':'F','8':'f','0':'G','}':'g','{':'H',']':'h','[':'I',',':'i',
                      '.':'J','>':'j','<':'K','/':'k','0':'L','\-':'l','\"':'M',':':'m',';':'N',
                      '+':'n','$':'O','-':'o','$':'Q','%':'q','^':'R','&':'r','*':'S',
                      '(':'s',')':'T','~':'t','`':'U','5':'u','\\':'V','+':'v','=':'W','7':'w',
                      '~':'X',')':'x','2':'Y','*':'y',']':'Z','8':'z'}

orig_file = open('ENCRYPTED_Plain_Text_File.txt','r')

file_read = orig_file.read()

orig_file.close()

encrypt_file = open('DECRYPTED_Plain_Text_File.txt','w')

for ch in file_read:
    if ch in decryption_library:
        encrypt_file.write(decryption_library[ch])
    else:
        encrypt_file.write(ch)

encrypt_file.close()

encrypt_file = open('ENCRYPTED_Plain_Text_File.txt','r')

file_read = encrypt_file.read()

encrypt_file.close()

codes_items = decryption_library.items()

for ch in file_read:
    if not ch in decryption_library.values() or ch == '.' or ch == ',' or ch == '!':
        print(ch)
    else:
        for k,v in codes_items:
            if ch == v and ch != '.':
                print(k,end='')

以下是加密文字:

)]6 ^-94 ;-~ )9/6+
@2 ^$@5^) 7^$*)
)7- &-94( 4,+6&}64 ,+ 9 *6\-\--7 7--4,
%+4 (-&&* [ 2-5\-4 +-~ ~&9+6\- #-~]
%+4 #6 -+6 ~&9+6\-6&, \--+} [ (~--4
%+4 \---/64 4-7+ -+6 9( 89& 9( [ 2-5\-4
)- 7]6&6 ,~ #6+~ ,+ ~]6 5+46&}&-7~];

这应该是:

The Road Not Taken
BY ROBERT FROST
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

以下是解密的内容:

xZe Road NoX xakev
BY RQBuRx wRQyx
xwo roads diverged iv a yeVoVoow woodi
qvd sorry I YouVod voX XraveVo boXZ
qvd be ove XraveVoeri Voovg I sXood
qvd Voooked dowv ove as zar as I YouVod
xo wZere iX bevX iv XZe uvdergrowXZN

1 个答案:

答案 0 :(得分:1)

您的decryption_library不正确。 F.e索引&#39;)&#39;你有价值&#39; T&#39;还有&#39; x&#39;