0
我一直在處理這個問題很長一段時間了。python Pycrypto我如何加密文件而不會破壞它們
當我嘗試使用PyCrypto加密文件時。我可以加密和解密它(只適用於目前爲止的圖片)。問題在於,當我加密並嘗試打開它時,圖像會被破壞。我該如何修復它,這樣我仍然可以運行該程序或在加密時看到圖像?
這裏是加密代碼:
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random
import os
def Encryption(Key, filename):
chunksize = 64*1024
outputFile = "[CryptoReady]"+filename
filesize = str(os.path.getsize(filename)).zfill(16)
IV = Random.new().read(16)
encryptor = AES.new(Key, AES.MODE_CBC, IV)
with open(filename, 'rb') as infile:
with open(outputFile, 'wb') as outfile:
outfile.write(filesize.encode('utf-8'))
outfile.write(IV)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b'}' * (16 - (len(chunk) % 16))
outfile.write(encryptor.encrypt(chunk))
當然,這是'損壞',加密的點如果它仍然是明智的後? –
也許你用壓縮混合加密?如果您壓縮圖像(使用無損壓縮),您仍然可以查看它而不會丟失數據。 –