biometry_hash_8.py 638 B

123456789101112131415161718192021222324252627
  1. def encrypt(key, filename):
  2. chunksize = 64*1024
  3. #print filename
  4. #print "4th time: ", key
  5. outputFile = "(encrypted)"+filename
  6. filesize = str(os.path.getsize(filename)).zfill(16)
  7. IV = ''
  8. for i in range(16):
  9. IV += chr(random.randint(0, 0xFF))
  10. encryptor = AES.new(key, AES.MODE_CBC, IV)
  11. with open(filename, 'rb') as infile:
  12. with open(outputFile, 'wb') as outfile:
  13. outfile.write(filesize)
  14. outfile.write(IV)
  15. while True:
  16. chunk = infile.read(chunksize)
  17. if len(chunk) == 0:
  18. break
  19. elif len(chunk) % 16 != 0:
  20. chunk += ' ' * (16 - (len(chunk) % 16))
  21. outfile.write(encryptor.encrypt(chunk))