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

これは何ですか: 'messlen <16: diff = 16-messlen message = 'z' * diff + messagein' –

+0

「E536D.dat」は改行を伴わずにこれらのバイトを含むバイナリファイルですか?その場合は、バイナリモードで開き、 '.read()'メソッドを使用してバイトデータを取得する必要があります。そうでない場合は、その内容を16進数で表示して、正しく読み込む方法を見つけ出す必要があります。 –

答えて

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あなたのお手伝いをします。あなたの質問に私は考えていましたので、すばやくgoogleで修正することができました。ありがとうございます

+0

バイトをバイトとして読み書きすることは意味のある解決策です。 :)将来の参考として、 'ast.literal_eval()'を使用してバイトオブジェクトの文字列表現を適切なバイトオブジェクトに変換することは可能ですが、余分なレベルの複雑さを避ける方がはるかに優れています。 –

+0

暗号化する必要がある(本当にパスワードを必要としない)場合は、CBCモードでランダムIVを使用し、暗号化されたデータにIVを付加して復号化に使用するだけで、秘密にする必要はありません。もちろん、次の問題は、暗号鍵を安全に保つ方法です。これは一般的に容易ではありません。 – zaph

関連する問題