Advertisement
Guest User

How to Create an Encryption Program with Python

a guest
Sep 1st, 2015
10,544
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from Crypto.Hash import SHA256
  2. from Crypto.Cipher import AES
  3. import os, random, sys, pkg_resources
  4.  
  5. def encrypt(key, filename):
  6.     chunksize = 64 * 1024
  7.     outFile = os.path.join(os.path.dirname(filename), "(encrypted)"+os.path.basename(filename))
  8.     filesize = str(os.path.getsize(filename)).zfill(16)
  9.     IV = ''
  10.  
  11.     for i in range(16):
  12.         IV += chr(random.randint(0, 0xFF))
  13.    
  14.     encryptor = AES.new(key, AES.MODE_CBC, IV)
  15.  
  16.     with open(filename, "rb") as infile:
  17.         with open(outFile, "wb") as outfile:
  18.             outfile.write(filesize)
  19.             outfile.write(IV)
  20.             while True:
  21.                 chunk = infile.read(chunksize)
  22.                
  23.                 if len(chunk) == 0:
  24.                     break
  25.  
  26.                 elif len(chunk) % 16 !=0:
  27.                     chunk += ' ' *  (16 - (len(chunk) % 16))
  28.  
  29.                 outfile.write(encryptor.encrypt(chunk))
  30.  
  31.  
  32. def decrypt(key, filename):
  33.     outFile = os.path.join(os.path.dirname(filename), os.path.basename(filename[11:]))
  34.     chunksize = 64 * 1024
  35.     with open(filename, "rb") as infile:
  36.         filesize = infile.read(16)
  37.         IV = infile.read(16)
  38.  
  39.         decryptor = AES.new(key, AES.MODE_CBC, IV)
  40.        
  41.         with open(outFile, "wb") as outfile:
  42.             while True:
  43.                 chunk = infile.read(chunksize)
  44.                 if len(chunk) == 0:
  45.                     break
  46.  
  47.                 outfile.write(decryptor.decrypt(chunk))
  48.  
  49.             outfile.truncate(int(filesize))
  50.    
  51. def allfiles():
  52.     allFiles = []
  53.     for root, subfiles, files in os.walk(os.getcwd()):
  54.         for names in files:
  55.             allFiles.append(os.path.join(root, names))
  56.  
  57.     return allFiles
  58.  
  59.    
  60. choice = raw_input("Do you want to (E)ncrypt or (D)ecrypt? ")
  61. password = raw_input("Enter the password: ")
  62.  
  63. encFiles = allfiles()
  64.  
  65. if choice == "E":
  66.     for Tfiles in encFiles:
  67.         if os.path.basename(Tfiles).startswith("(encrypted)"):
  68.             print "%s is already encrypted" %str(Tfiles)
  69.             pass
  70.  
  71.         elif Tfiles == os.path.join(os.getcwd(), sys.argv[0]):
  72.             pass
  73.         else:
  74.             encrypt(SHA256.new(password).digest(), str(Tfiles))
  75.             print "Done encrypting %s" %str(Tfiles)
  76.             os.remove(Tfiles)
  77.  
  78.  
  79. elif choice == "D":
  80.     filename = raw_input("Enter the filename to decrypt: ")
  81.     if not os.path.exists(filename):
  82.         print "The file does not exist"
  83.         sys.exit(0)
  84.     elif not filename.startswith("(encrypted)"):
  85.         print "%s is already not encrypted" %filename
  86.         sys.exit()
  87.     else:
  88.         decrypt(SHA256.new(password).digest(), filename)
  89.         print "Done decrypting %s" %filename
  90.         os.remove(filename)
  91.  
  92. else:
  93.     print "Please choose a valid command."
  94.     sys.exit()
Advertisement
RAW Paste Data Copied
Advertisement