2016-12-06 40 views
0

我使用pycrypto庫的AES算法在python瓶中構建網站&。在註冊網頁時,我將加密的密鑰保存在文本文件中。在登錄頁面,我比較進入PWD與解密PWD,使用下面的代碼使用AES算法的python中的解密問題

def decryption(encryptedString,key_from_file): 
    PADDING = '{' 
    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 
    #Key is FROM the printout of 'secret' in encryption 
    #below is the encryption. 
    encryption = encryptedString 
    key = key_from_file 
    cipher = AES.new(key) #### error comes here 
    decoded = DecodeAES(cipher, encryption) 
    return decoded 

def login(): 
    if request.method == 'GET': 
     return render_template('login.html') 
    if request.method == 'POST': 
     username = request.form['username'] 
     password = request.form['password'] 
     d2 = pandas.read_csv("Employee_Info.txt",header=0) 
     search_id = d2[d2['email'] == username] 
     pdb.set_trace() 
     if search_id.empty: 
      error = "username does not exists" 
      return render_template('login.html', error = error) 
     else: 
      pwd_from_file=search_id.iloc[0]['pwd'] 
      key_from_file=search_id.iloc[0]['key'] 

      if decryption(pwd_from_file,key_from_file) == password: 
       print "matching password" 
      else: 
       print "mismatch" 

,但我得到的錯誤爲ValueError: AES key must be 16,24 or 32 bytes long.

文本文件有以下字段:

id,email,pwd,key 
qq,qq,h4vvEPuVNwjw22yJKz8QGg==,xéðjŸ¸AOݬ‡ 

回答

1

你存儲原始Unicode字節中的關鍵字,因此可能存在序列化/反序列化錯誤。在存儲到文件中之前,將原始密鑰字節編碼爲十六進制或Base64,然後在初始化密碼之前將其轉換回原始編碼。

注:存儲在憑證數據存儲的關鍵是非常糟糕憑據驗證加密密碼非常糟糕查看Why should I hash passwordsHow to securely hash passwords瞭解更多信息。