密码解密器失败

时间:2017-03-06 12:07:04

标签: python cryptography aes

美好的一天,

我创建了一个脚本,用于读取用户输入并将其与保存在文本文件中的密钥进行比较。即使字符串相同也会失败请帮助我:

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

1 个答案:

答案 0 :(得分: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 的帮助。你的问题让我思考,所以我设法用快速谷歌修复它。非常感谢