2017-03-06 214 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