fix crypt.py
`crypt`.py now uses `pyaes` instead of `simplecrypto`. This is
backwards-incompatibld. Fixes
@@ -13,14 +13,17 @@ optional arguments:
-k KEY, --key KEY Encrypt/Decrypt Key.
-d, --decrypt Flag to decrypt.
'''
-import sys
import os
+import argparse
+import base64
+
try:
- from simplecrypto.key import AesKey
+ import pyaes
except:
print 'Installing Required packages.'
- _stash('pip install simplecrypto')
- sys.exit(0)
+ _stash('pip install pyaes')
+ import pyaes
+
class Crypt(object):
def __init__(self, in_filename, out_filename=None):
@@ -29,25 +32,33 @@ class Crypt(object):
def aes_encrypt(self, key=None, chunksize=64*1024):
self.out_filename = self.out_filename or self.in_filename + '.enc'
- aes = AesKey(key)
+ if key is None:
+ key = base64.b64encode(os.urandom(32))[:32]
+ aes = pyaes.AESModeOfOperationCTR(key)
with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
- outfile.write(aes.encrypt_raw(infile.read()))
-
+ pyaes.encrypt_stream(aes, infile, outfile)
+ return key
def aes_decrypt(self, key, chunksize=64*1024):
self.out_filename = self.out_filename or os.path.splitext(self.in_filename)[0]
- aes = AesKey(key)
+ aes = pyaes.AESModeOfOperationCTR(key)
with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
- outfile.write(aes.decrypt_raw(infile.read()))
+ pyaes.decrypt_stream(aes, infile, outfile)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
- ap.add_argument('-k','--key',action='store',default='',help='Encrypt/Decrypt Key.')
- ap.add_argument('-d','--decrypt',action='store_true',default=False,help='Flag to decrypt.')
+ ap.add_argument(
+ '-k', '--key', action='store', default=None,
+ help='Encrypt/Decrypt Key.',
+ )
+ ap.add_argument(
+ '-d', '--decrypt', action='store_true', default=False,
+ help='Flag to decrypt.',
+ )
#ap.add_argument('-t','--type',action='store',choices={'aes','rsa'},default='aes')
ap.add_argument('infile', action='store', help='File to encrypt/decrypt.')
ap.add_argument('outfile', action='store', nargs='?', help='Output file.')
@@ -56,5 +67,6 @@ if __name__=='__main__':
if args.decrypt:
crypt.aes_decrypt(args.key)
else:
- crypt.aes_encrypt(args.key)
-
+ nk = crypt.aes_encrypt(args.key)
+ if args.key is None:
+ print "Key: ", nk
