2017-04-03 36 views
-2

我想解密一個Python文件,我用另一個程序加密。一些字母被正確解密,而另一些則沒有。我不確定發生了什麼事。我基本上所做的只是反轉解密文件的代碼。我認爲它與迭代文本的方式有關,但我不知道如何解決它。如何在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 ^[email protected]^) 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 
+3

你解密邏輯似乎是正確的,但你的'decryption_library'有重複:')'映射到'T'和'x',' 〜'映射到't'以及'X',...由於這些歧義,你將無法恢復原始的明文。 – Pit

+0

你對傳遞值有相同的關鍵!例如:']':'h'和']':'Z' – RaminNietzsche

回答

1

你decryption_library是不正確的。鐵索引「)」你纔有價值「T」也「X」