2017-03-06 213 views
1

好日子,密碼解密失敗

我有一個劇本我創建,讀取用戶輸入,並將其與保存在一個文本文件中的關鍵。即使字符串是相同的,請幫助我:

from Crypto.Cipher import AES 
from tkinter import * 
#create the window 
root = Tk() 

#modify root window 
root.title("Button Example") 
#root.geometry("500x500") 

app = Frame(root) 
key='Key is unique!!!' 
IV='This is an IV456' 
#password=b'\xa3Y\x00\xae\xad\xad\x1c\xc6Js\xa9\xf4\x0e\xf3\x0f\xe3' 


def encrypt(): 
    obj = AES.new(key, AES.MODE_CBC, IV) 
    messagein = inputbar.get() 
    inputbar.delete(0,END) 
    messlen = len(messagein) 
    if messlen < 16: 
     diff = 16-messlen 
     message = 'z'*diff + messagein 
    global ciphertext 
    ciphertext = obj.encrypt(message) 
    print(ciphertext) 
    del obj 
def check(): 
    encrypt() 
    passwordfunc() 
    if ciphertext == password: 
     print('Success!') 
    else: 
     print('Fail!') 

def passwordfunc(): 
    file=open("E536D.dat","r") 
    global password 
    password = file.readline() 
    file.close() 
    print(password) 

inputbar = Entry(root,font='TkDefaultFont 30') 
inputbar.pack() 

button1= Button(text='Encrpyt',command=lambda:encrypt()) 
button1.pack() 
button2 = Button(text='Compare',command=lambda:check()) 
button2.pack() 
button3 = Button(text='File',command=lambda:passwordfunc()) 
button3.pack() 

root.mainloop() 

我做了什麼錯了?行#password=b'\xa3Y\x00\xae\xad\xad\x1c\xc6Js\xa9\xf4\x0e\xf3\x0f\xe3'是它需要比較的關鍵,但它從文件比較中返回false,但在它自己內部工作。請幫幫我。我通過移動obj = AES.new(key, AES.MODE_CBC, IV)來加密和刪除函數結尾處的不同密鑰輸出。但是,當我比較文件中的字符串和正確的輸入python仍然說他們不一樣。下面是我的意思的截圖。 enter image description here

+0

這個鍵和IV必須有16個字符,並且要被描述的文本必須是16個字符的倍數,如果它不是 –

+0

那麼我必須填充一些像空格這樣的字符。 :'if messlen <16: diff = 16-messlen message ='z'* diff + messagein' –

+0

「E536D.dat」是一個二進制文件,包含_just_那些字節,沒有換行符?如果是這樣,你應該以二進制模式打開它,並使用'.read()'方法來獲取字節數據。如果不是,您應該向我們展示其內容的十六進制轉儲,以便我們瞭解如何正確讀取它。 –

回答

1

好的我修好了。問題是密文是字節,我的文件有它理解爲一個字符串,所以我沒了下文,並使用字節讀取它以字節爲單位,現在它的工作原理100%

from Crypto.Cipher import AES 
from tkinter import * 
#create the window 
root = Tk() 

#modify root window 
root.title("Button Example") 
#root.geometry("500x500") 

app = Frame(root) 
key='Key is unique!!!' 
IV='This is an IV456' 
#password=b'\xa3Y\x00\xae\xad\xad\x1c\xc6Js\xa9\xf4\x0e\xf3\x0f\xe3' 


def encrypt(): 
    obj = AES.new(key, AES.MODE_CBC, IV) 
    messagein = inputbar.get() 
    inputbar.delete(0,END) 
    messlen = len(messagein) 
    if messlen < 16: 
     diff = 16-messlen 
     message = 'z'*diff + messagein 
    global ciphertext 
    ciphertext = obj.encrypt(message) 
    del obj 

def passwordfunc(): 
    file=open("E536D.dat","rb") 
    global password 
    password = file.readline() 
    file.close() 

def check(): 
    encrypt() 
    passwordfunc() 
    print('ciphertext = ',ciphertext) 
    print(' password = ',password) 
    if ciphertext == password: 
     print('Success!') 
    else: 
     print('Fail!') 

def write(): 
    encrypt() 
    file=open("E536D.dat","wb") 
    file.write(ciphertext) 
    file.close() 


inputbar = Entry(root,font='TkDefaultFont 30') 
inputbar.pack() 

button1= Button(text='Encrpyt',command=lambda:encrypt()) 
button1.pack() 
button2 = Button(text='Compare',command=lambda:check()) 
button2.pack() 
button3 = Button(text='File',command=lambda:passwordfunc()) 
button3.pack() 
button4 = Button(text='Write',command=lambda:write()) 
button4.pack() 

root.mainloop() 

由於其寫入文件PM 2RIng爲您的幫助。你的問題讓我想到了,所以我設法用快速的谷歌修復它。非常感謝

+0

以字節爲單位寫入和讀取字節是明智的解決方案。 :)爲了將來的參考,它可以通過使用'ast.literal_eval()'將字節對象的字符串表示轉換爲適當的字節對象,但避免額外複雜程度要好得多。 –

+0

如果您必須進行加密(並非真的需要密碼纔是安全的),請使用帶有隨機IV的CBC模式,只需將加密的數據前綴與IV一起用於解密即可,並不需要保密。當然下一個問題是如何保持加密密鑰的安全,這通常不容易。 – zaph