pickle 后文件大小急剧增加

发布于 2024-08-10 00:20:47 字数 970 浏览 3 评论 0原文

我正在读取文件并将数据(加密后)发送到字典,并包含加密前后数据的哈希值。然后我对字典进行了pickle,但发现文件大小与源文件大小相比很大。如果我将加密数据直接写入文件,则大小与源相同。知道为什么我的腌制文件这么大吗?

#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()

I'm reading in a file and sending the data (once encrypted) to a dictionary, with a hash of the data before and after encryption. I then pickle the dictionary but find the file size is massive compared to the source file size. If I write the encrypted data straight to a file the size is identical to the source. Any idea why my pickled file is so large?

#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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-08-17 00:20:47

尝试通过指定 protocol=2 作为 pickle.dump 的关键字参数来使用二进制 pickle。它应该更有效率。

Try using a binary pickle by specifying protocol=2 as a keyword argument to pickle.dump. It should be much more efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文