我正在读取文件并将数据(一旦加密)发送到字典,并在加密前后加上数据的哈希值。然后我挑选字典,但发现文件大小与源文件大小相比是巨大的。如果我将加密数据直接写入文件,其大小与源相同。知道为什么我的腌制文件太大了吗?
#Encrypt data and get hashes
def encryptAndExportFile(self, key, inFile, outFile):
openInFile = open(inFile,"rb")
inFileSize = os.path.getsize(inFile)
inFileData = openInFile.readlines()
openInFile.close()
""" initialise cipher """
cipher = AES.new(key, AES.MODE_CFB)
""" initialise MD5 """
m = hashlib.md5() #hash
h = hashlib.md5() #hash of encrypted dataq
encryptedData = []
for data in inFileData:
m.update(data)
encData = cipher.encrypt(data)
h.update(encData)
encryptedData.append(encData)
hashResult = m.digest()
encHashResult = h.digest()
return hashResult, encryptedData, encHashResult
def storeEncryptedObject(self, obj, path):
outFile = open(path, 'wb')
pickle.dump(obj, outFile)
outFile.close()
答案 0 :(得分:6)
通过将protocol=2
指定为pickle.dump的关键字参数,尝试使用二进制pickle。它应该更有效率。