2017-08-12 195 views
0

當我試圖加密/與蟒蛇pycrypto解密加密失去字符串的一部分。大部分事情都順利工作,但我得到一個奇怪的問題,當解密data.I試圖加密/解密的一些JPG文件進行測試,雖然它們加密/沒有問題解密,解密後的文件無法/損壞打開。試圖找到我保存的文本文件裏有類似的隨機一句「測試此文件的完整性等等等等等等」的問題,它只有在「誠信....等等等等等等」正確解密,完整性之前一切都還是亂碼字符。我不是AES該見地,但即時假設這是一個編碼/解碼或填充錯誤。pycrypto加密/解密,解密

這裏是我的代碼:

#encryption 
iv = Random.new().read(AES.block_size) 

filePath = input("Path to file for encryption: ") 
selFile = open(filePath, 'rb') 
getBytes = bytes(selFile.read()) 

encPW = input("Enter password: ") 
hashpw = hashlib.sha256(encPW.encode('UTF-8').digest()) 

destination = input("Destination path for encrypted file: ") 

aes = AES.new(hashpw, AES.Mode_CFB, iv) 
encFile = base65.b64encode(aes.encrypt(getBytes)) 

writetofile = open(destination, 'wb') 
writetofile.write(encFile) 
writetofile.close() 
print("Encryption successful") 

#Decryption 
iv = Random.new().read(AES.block_size) 

filePath = input("Path to file for decryption: ") 
selFile = open(filePath, 'rb') 
getBytes = bytes(selFile.read()) 

decPW = input("Enter password: ") 
hashdecpw = hashlib.sha256(encPW.encode('UTF-8').digest()) 

destination = input("Destination path for decrypted file: ") 

aes = AES.new(hashdecpw, AES.Mode_CFB, iv) 
decFile = aes.decrypt(getBytes) 

writetofile = open(destination, 'wb') 
writetofile.write(decFile) 
writetofile.close() 
print("Decryption successful") 

上什麼可能會造成的第一個字符的損失,並阻止我的加密/解密正確的文件任何想法?

+0

請不要惡意破壞你的帖子。 – DJMcMayhem

+1

請不要破壞你的帖子。一旦你發佈了一個問題,你已經將內容授權給了Stack Overflow社區(在CC-by-SA許可下)。如果您想取消關聯此帳戶與您的帳戶關聯,請參閱[解除請求的正確途徑是什麼?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-換一個 - 解離 - 請求)。 – Bugs

回答

1

你至少有三個問題:

  • 你可能意味着hashlib.sha256(encPW.encode('UTF-8')).digest()代替hashlib.sha256(encPW.encode('UTF-8').digest())(右括號是在錯誤的位置)

  • 你編碼使用Base64密文寫作之前它到一個文件。在解密文件之前,您已經忘記從文件中讀迴文件後再進行解碼。例如:

    getBytes = base64.b64decode(bytes(selFile.read())) 
    
  • 這是大的:你認爲你已經使用了加密解密過程中需要完全一樣的IV。 IV並不是祕密,但對於您使用同一個密鑰進行的每種加密,它都必須是唯一的。通常將IV寫在密文前並回讀解密。

    #encryption 
    encFile = base64.b64encode(iv + aes.encrypt(getBytes)) 
    
    #decryption 
    getBytes = base64.b64decode(bytes(selFile.read())) 
    iv = getBytes[:16] 
    aes = AES.new(hashdecpw, AES.Mode_CFB, iv) 
    decFile = aes.decrypt(getBytes[16:]) 
    
+0

勉強重要,但'海峽。編碼()'自動承擔UTF-8 :) –

+0

謝謝Artjom一切工作,因爲它應該現在應該在這些變化之後。看起來像IV是絕對是我失去了良好的捕捉的重大一步。感謝您的幫助 – mypython

1

你生成用於加密和解密seperately,這涉及到產生這樣的問題一個新的IV。下面是我建議這樣做:

def encrypt(inpath, outpath, password): 
    iv = Random.new().read(AES.block_size) 
    with open(inpath, "rb") as f: 
     contents = f.read() 
    # A context manager automatically calls f.close() 
    key = pbkdf2.crypt(password, "") 
    # See notes 

    aes = AES.new(key, AES.Mode_CFB, iv) 
    encrypted = aes.encrypt(contents) 
    with open(outpath, "wb") as f: 
     f.write(iv + b":") 
     f.write(encrypted) 
    print("Encryption successful") 


def decrypt(inpath, outpath, password): 
    with open(inpath, "rb") as f: 
     contents = f.read() 

    iv, encrypted = contents.split(b":") 
    key = pbkdf2.crypt(password, "") 
    aes = AES.new(key, AES.Mode_CFB, iv) 

    decrypted = aes.decrypt(contents) 
    with open(outpath, "wb") as f: 
     f.write(decrypted) 
    print("Decryption successful") 

一些注意事項:

  • 的靜脈注射並不意味着是祕密的,所以它可以隨機生成一次,然後寫入一個文件供以後使用的解密(如本例中所示)

  • 哈希算法對於派生密鑰來說不夠強大,這就是爲什麼有一些稱爲密鑰派生算法的特殊工具(如Python中的PBKDF2)。改用它們!

我自己沒有測試過這個代碼,所以可能無法正常工作。

+1

密碼散列的好處。我錯過了。 –

+1

優秀,兩個答案都是完全正確的。 我只使用散列算法進行測試,並且現在將執行PBKDF2,這些問題已經排序。它無論如何不是關鍵的,只是學術而優秀的一點。感謝您的回答和幫助! – mypython