File size is increased after pickle

Thread Solved

Join Date: Sep 2009
Posts: 12
Reputation: zyrus001 is an unknown quantity at this point 
Solved Threads: 0
zyrus001 zyrus001 is offline Offline
Newbie Poster

File size is increased after pickle

 
0
  #1
24 Days Ago
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?

  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

  1. def storeEncryptedObject(self, obj, path):
  2.  
  3. outFile = open(path, 'wb')
  4. pickle.dump(obj, outFile)
  5. outFile.close()
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: zyrus001 is an unknown quantity at this point 
Solved Threads: 0
zyrus001 zyrus001 is offline Offline
Newbie Poster
 
2
  #2
24 Days Ago
Using protocol 2 greatly improved file size, which only increases by 5% in some cases.

  1. def storeEncryptedObject(self, obj, path):
  2.  
  3. outFile = open(path, 'wb')
  4. pickle.dump(obj, outFile, protocol = 2)
  5. outFile.close()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #3
24 Days Ago
Thanks for letting the rest of us know!
Most folks use protocol=-1 as the highest possible protocol.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC