Advertisement
Peaser

pyaes

Jul 23rd, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import sys, getpass, os
  2. from hashlib import md5
  3. from Crypto.Cipher import AES
  4. from Crypto import Random
  5. if os.name == "nt":
  6.     warning = """Usage:python pyaes.py <IN_file> <OUT_file> <mode>
  7.  
  8.    modes:
  9.        encrypt
  10.        decrypt
  11.  
  12.    *You will be prompted for a password."""
  13.     try:
  14.         inf = sys.argv[1]
  15.         outf = sys.argv[2]
  16.         mode = sys.argv[3]
  17.         if mode not in ("encrypt","decrypt"):raise StandardError
  18.     except:
  19.         print "Something went wrong!\n\n",warning
  20.         sys.exit(0)
  21.     def derive_key_and_iv(password, salt, key_length, iv_length):
  22.         d = d_i = ''
  23.         while len(d) < key_length + iv_length:
  24.             d_i = md5(d_i + password + salt).digest()
  25.             d += d_i
  26.         return d[:key_length], d[key_length:key_length+iv_length]
  27.  
  28.     def encrypt(in_file, out_file, password, key_length=32):
  29.         bs = AES.block_size
  30.         salt = Random.new().read(bs - len('Salted__'))
  31.         key, iv = derive_key_and_iv(password, salt, key_length, bs)
  32.         cipher = AES.new(key, AES.MODE_CBC, iv)
  33.         out_file.write('Salted__' + salt)
  34.         finished = False
  35.         while not finished:
  36.             chunk = in_file.read(1024 * bs)
  37.             if len(chunk) == 0 or len(chunk) % bs != 0:
  38.                 padding_length = (bs - len(chunk) % bs) or bs
  39.                 chunk += padding_length * chr(padding_length)
  40.                 finished = True
  41.             out_file.write(cipher.encrypt(chunk))
  42.  
  43.     def decrypt(in_file, out_file, password, key_length=32):
  44.         bs = AES.block_size
  45.         salt = in_file.read(bs)[len('Salted__'):]
  46.         key, iv = derive_key_and_iv(password, salt, key_length, bs)
  47.         cipher = AES.new(key, AES.MODE_CBC, iv)
  48.         next_chunk = ''
  49.         finished = False
  50.         while not finished:
  51.             chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
  52.             if len(next_chunk) == 0:
  53.                 padding_length = ord(chunk[-1])
  54.                 chunk = chunk[:-padding_length]
  55.                 finished = True
  56.             out_file.write(chunk)
  57.  
  58.     if mode == "encrypt":
  59.         password = ""
  60.         def verify():
  61.             global password
  62.             vpw1 = getpass.getpass("Type a Password:")
  63.             vpw2 = getpass.getpass("Retype Password:")
  64.             if vpw1 != vpw2:
  65.                 print "\nPasswords do not patch.\n"
  66.                 verify()
  67.             else:
  68.                 password += vpw2
  69.         verify()
  70.         with open(inf, 'rb') as in_file, open(outf, 'wb') as out_file:
  71.             encrypt(in_file, out_file, password)
  72.  
  73.     if mode == "decrypt":
  74.         password = getpass.getpass()
  75.         with open(inf, 'rb') as in_file, open(outf, 'wb') as out_file:
  76.             decrypt(in_file, out_file, password)
  77. else:exec('7261697365204f534572726f722c20224e6f742057696e646f77732c206b696c6c20796f757273656c662e22'.decode('hex'))#cheeky
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement