Advertisement
furas

encode/decode data in file

Jul 28th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import os
  4. import struct
  5. from Crypto.Cipher import AES
  6. import hashlib
  7. import getpass
  8.  
  9.  
  10. def encryption_file(in_filename, key, chunksize=64*1024):
  11.    
  12.     out_filename = in_filename + '.enc'
  13.  
  14.     TheHash = hashlib.sha256(key.encode('utf-8')).hexdigest()
  15.     key = TheHash[0:32]
  16.    
  17.     IV = b'rp1\\\xe6@\x04\x13\x7f\x81\x1d<\x0b\x13\x1a@'
  18.     encryptor = AES.new(key, AES.MODE_CBC, IV)
  19.     file_size = os.path.getsize(in_filename)
  20.  
  21.     with open(in_filename, 'rb') as infile:
  22.         with open(out_filename, 'wb') as outfile:
  23.             outfile.write(struct.pack('<Q', file_size))
  24.             outfile.write(IV)
  25.  
  26.             while True:
  27.                 chunk = infile.read(chunksize)
  28.              
  29.                 if len(chunk) == 0:
  30.                     break
  31.                 elif len(chunk) % 16 != 0:
  32.                     chunk += ' '.encode('utf-8') * (16 - len(chunk) % 16)
  33.              
  34.                 text = encryptor.encrypt(chunk)
  35.                 print('[DEBUG]: encrypted:', text)
  36.                 outfile.write(text)
  37.  
  38.         os.unlink(in_filename)
  39.  
  40.  
  41. def decrypt_file(in_filename, key, chunksize=64*1024):
  42.  
  43.     out_filename = os.path.splitext(filename)[0]
  44.    
  45.     TheHash2 = hashlib.sha256(key.encode('utf-8')).hexdigest()
  46.     key = TheHash2[0:32]
  47.  
  48.     #IV = b'rp1\\\xe6@\x04\x13\x7f\x81\x1d<\x0b\x13\x1a@'
  49.     #print(len(IV))
  50.  
  51.     with open(filename, 'rb') as infile:
  52.         file_size = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
  53.         IV = infile.read(16) # len(IV)
  54.         decryptor = AES.new(key, AES.MODE_CBC, IV)
  55.  
  56.         with open(out_filename, 'wb') as outfile:
  57.             while True:
  58.                 chunk = infile.read(chunksize)
  59.                
  60.                 if len(chunk) == 0:
  61.                     break
  62.                
  63.                 text = decryptor.decrypt(chunk)
  64.                 print('[DEBUG]: decrypted:', text)
  65.                 outfile.write(text)
  66.  
  67.             outfile.truncate(file_size)
  68.         os.unlink(in_filename)
  69.  
  70. #----------------------------------------------------------
  71.  
  72. choice = input("(E)ncrypt or (D)ecrypt: ").lower()
  73.  
  74. if choice == 'e':
  75.     #filename = input("Enter filename: ")
  76.     #key = getpass.getpass("Enter password: ")
  77.     filename = 'test.txt'
  78.     key = 'hello world'
  79.     encryption_file(filename, key)
  80. elif choice == 'd':
  81.     #filename = input("Enter filename: ")
  82.     #key = getpass.getpass("Enter password: ")
  83.     filename = 'test.txt.enc'
  84.     key = 'hello world'
  85.     decrypt_file(filename, key)
  86. else:
  87.     print("Select E or D")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement