944,059 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 321
  • Python RSS
Nov 3rd, 2009
0

File size is increased after pickle

Expand Post »
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?

Python Syntax (Toggle Plain Text)
  1. #Encrypt data and get hashes
  2. def encryptAndExportFile(self, key, inFile, outFile):
  3.  
  4. openInFile = open(inFile,"rb")
  5. inFileSize = os.path.getsize(inFile)
  6. inFileData = openInFile.readlines()
  7. openInFile.close()
  8.  
  9. """ initialise cipher """
  10.  
  11. cipher = AES.new(key, AES.MODE_CFB)
  12.  
  13. """ initialise MD5 """
  14.  
  15. m = hashlib.md5() #hash
  16. h = hashlib.md5() #hash of encrypted dataq
  17.  
  18. encryptedData = []
  19.  
  20. for data in inFileData:
  21.  
  22. m.update(data)
  23. encData = cipher.encrypt(data)
  24. h.update(encData)
  25. encryptedData.append(encData)
  26.  
  27.  
  28. hashResult = m.digest()
  29. encHashResult = h.digest()
  30.  
  31. return hashResult, encryptedData, encHashResult

Python Syntax (Toggle Plain Text)
  1. def storeEncryptedObject(self, obj, path):
  2.  
  3. outFile = open(path, 'wb')
  4. pickle.dump(obj, outFile)
  5. outFile.close()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zyrus001 is offline Offline
14 posts
since Sep 2009
Nov 3rd, 2009
2
Re: File size is increased after pickle
Using protocol 2 greatly improved file size, which only increases by 5% in some cases.

Python Syntax (Toggle Plain Text)
  1. def storeEncryptedObject(self, obj, path):
  2.  
  3. outFile = open(path, 'wb')
  4. pickle.dump(obj, outFile, protocol = 2)
  5. outFile.close()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zyrus001 is offline Offline
14 posts
since Sep 2009
Nov 3rd, 2009
0
Re: File size is increased after pickle
Thanks for letting the rest of us know!
Most folks use protocol=-1 as the highest possible protocol.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Removing a substring from string
Next Thread in Python Forum Timeline: Ceasar Cipher





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC