Advertisement
The_Defalt

crypto.py

Jun 3rd, 2016
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. from Crypto.Cipher import AES
  4. import hashlib
  5. import struct
  6. from random import randint
  7. from getpass import getpass
  8. import argparse
  9. import os
  10. import sys
  11.  
  12. #This is a basic script to encrypt/decrypt a file
  13. #Happy hacking! -Defalt
  14.  
  15. def parsing_func():
  16.     parser = argparse.ArgumentParser(description='Basic encryption/decryption tool')
  17.     parser.add_argument('-e', '--encrypt', help='Encrypt specified file', action='store_true', default=False, dest='encrypt')
  18.     parser.add_argument('-d', '--decrypt', help='Decrypt specified file', action='store_true', default=False, dest='decrypt')
  19.     parser.add_argument('-f', '--file', help='Specify file to encrypt/decrypt', action='store', default=False, dest='file')
  20.     parser.add_argument('-o', '--output', help='Name of output file', action='store', default=False, dest='outfile')
  21.     args = parser.parse_args()
  22.     if len(sys.argv) == 1:
  23.         parser.print_help()
  24.         sys.exit(1)
  25.     elif ((not args.encrypt) and (not args.decrypt)) or ((not not args.encrypt) and (not not args.decrypt)):
  26.         parser.error('invalid action specification')
  27.     elif not args.file:
  28.         parser.error('no file specified')
  29.     else:
  30.         return args
  31.  
  32. args = parsing_func()
  33.  
  34. def encrypt(infile=args.file, outfile=args.outfile):
  35.     if not os.path.isfile(infile):
  36.         print '[!] Failed to Detect Input File'
  37.         sys.exit(1)
  38.     elif not outfile:
  39.         try:
  40.             overwrite = raw_input('[*] No Output File Given, Overwrite Input File? [y/N] ').strip().lower()[0]
  41.         except KeyboardInterrupt:
  42.             print '[!] Aborting Encryption'
  43.             sys.exit(1)
  44.         if overwrite == 'y':
  45.             outfile = infile
  46.         else:
  47.             print '[!] Aborting Encryption'
  48.             sys.exit(1)
  49.     filesize = os.path.getsize(infile)
  50.     print '[*] Reading File to Encrypt'
  51.     try:
  52.         with open(infile, 'rb') as file:
  53.             to_encrypt = file.read()
  54.     except IOError:
  55.         print '[!] Failed to Read Input File'
  56.         sys.exit(1)
  57.     print '[*] Padding File Contents\n'
  58.     to_encrypt += 'password_is_correct'
  59.     while len(to_encrypt) % 16 != 0:
  60.         to_encrypt += ' '
  61.     try:
  62.         password = getpass('[*] Enter Password: ')
  63.         confirm_pass = getpass('[*] Confirm Password: ')
  64.     except KeyboardInterrupt:
  65.         print '[!] Aborting Encryption'
  66.         sys.exit(1)
  67.     if password != confirm_pass:
  68.         print '[!] Password Confirmation Failed'
  69.         sys.exit(1)
  70.     print '\n[*] Encrypting Contents'
  71.     ivector = ''.join(chr(randint(0, 0xFF)) for i in range(16))
  72.     encrypted_file = AES.new(hashlib.sha256(password).digest(), AES.MODE_CBC, ivector).encrypt(to_encrypt)
  73.     try:
  74.         with open(outfile, 'wb') as file:
  75.             file.write(struct.pack('<Q', filesize))
  76.             file.write(ivector)
  77.             file.write(encrypted_file)
  78.     except IOError:
  79.         print '[!] Failed to Encrypt File Contents'
  80.         sys.exit(1)
  81.     print '[*] Encryption Complete!'
  82.  
  83. def decrypt(infile=args.file, outfile=args.outfile):
  84.     if not os.path.isfile(infile):
  85.         print '[!] Failed to Detect Input File'
  86.         sys.exit(1)
  87.     elif not outfile:
  88.         try:
  89.             overwrite = raw_input('[*] No Output File Given, Overwrite Input File? [y/N] ').strip().lower()[0]
  90.         except KeyboardInterrupt:
  91.             print '[!] Aborting Encryption'
  92.             sys.exit(1)
  93.         if overwrite == 'y':
  94.             outfile = infile
  95.         else:
  96.             print '[!] Aborting Encryption'
  97.             sys.exit(1)
  98.     print '[*] Parsing File to Decrypt\n'
  99.     try:
  100.         with open(infile, 'rb') as file:
  101.             size = struct.unpack('<Q', file.read(8))
  102.             ivector = file.read(16)
  103.             to_decrypt = file.read()
  104.     except IOError:
  105.         print '[!] Failed to Read Input File'
  106.         sys.exit(1)
  107.     try:
  108.         password = getpass('[*] Enter Password: ')
  109.     except Exception:
  110.         print '[!] Aborting Decryption'
  111.         sys.exit(1)
  112.     print '\n[*] Attempting to Decrypt File Contents'
  113.     try:
  114.         decrypted = AES.new(hashlib.sha256(password).digest(), AES.MODE_CBC, ivector).decrypt(to_decrypt)
  115.         if not 'password_is_correct' in decrypted:
  116.             print '[!] Incorrect Password'
  117.             sys.exit(1)
  118.     except Exception:
  119.         print '[!] Failed to Decrypt File Contents'
  120.         sys.exit(1)
  121.     try:
  122.         with open(outfile, 'wb') as file:
  123.             file.write(decrypted)
  124.             file.truncate(size[0])
  125.     except IOError:
  126.         print '[!] Failed to Write Decrypted File'
  127.         sys.exit(1)
  128.     print '[*] Decryption Complete!'
  129.  
  130. if args.encrypt:
  131.     encrypt()
  132. else:
  133.     decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement