├── README.md └── encrypt.py /README.md: -------------------------------------------------------------------------------- 1 | # File-Encrypter 2 | This is a Simple encryption program using python. 3 | This program uses AES algorithm to encrypt any given file and it also decrypt files that has encrypted using AES algorithm 4 | .... 5 | -------------------------------------------------------------------------------- /encrypt.py: -------------------------------------------------------------------------------- 1 | import os, random 2 | 3 | from Crypto.Cipher import AES 4 | 5 | from Crypto.Hash import SHA256 6 | 7 | import getpass 8 | 9 | 10 | #encrypt Function 11 | def encrypt(key, filename): 12 | 13 | blocksize = 64*1024 14 | 15 | Ofilename = "encrypted-"+filename #Set Output File Name 16 | 17 | #Zfill pads left in the string with zeros to complete the width 18 | filesize = str(os.path.getsize(filename)).zfill(16) 19 | 20 | #Initialization Vector 21 | InitVector = '' 22 | 23 | 24 | 25 | for i in range(16): 26 | 27 | InitVector += chr(random.randint(0, 0xFF)) 28 | 29 | 30 | 31 | encryptor = AES.new(key, AES.MODE_CBC, InitVector) 32 | 33 | 34 | #open the file(rb-It reads the file in binary mode) 35 | with open(filename, 'rb') as infile: 36 | 37 | with open(Ofilename, 'wb') as outfile: #write the file in binary mode 38 | 39 | outfile.write(filesize) 40 | 41 | outfile.write(InitVector) 42 | 43 | while True: 44 | 45 | size = infile.read(blocksize) 46 | 47 | 48 | 49 | if len(size) == 0: 50 | 51 | break 52 | 53 | elif len(size) % 16 != 0: 54 | 55 | size += ' ' * (16 - (len(size) % 16)) 56 | 57 | outfile.write(encryptor.encrypt(size)) 58 | 59 | 60 | 61 | 62 | 63 | def decrypt(key, filename): 64 | 65 | blocksize = 64*1024 66 | 67 | Ofilename = "Decrypted-"+filename #Set Output File Name 68 | 69 | 70 | #open the file(rb-It reads the file in binary mode) 71 | with open(filename, 'rb') as infile: 72 | 73 | filesize = long(infile.read(16)) 74 | 75 | InitVector = infile.read(16) 76 | 77 | 78 | 79 | decryptor = AES.new(key, AES.MODE_CBC, InitVector) 80 | 81 | 82 | 83 | with open(Ofilename, 'wb') as outfile: 84 | 85 | while True: 86 | 87 | size = infile.read(blocksize) 88 | 89 | 90 | 91 | if len(size) == 0: 92 | 93 | break 94 | 95 | 96 | 97 | outfile.write(decryptor.decrypt(size)) 98 | 99 | outfile.truncate(filesize) 100 | 101 | 102 | 103 | 104 | 105 | def getKey(password): 106 | 107 | hasher = SHA256.new(password) 108 | 109 | return hasher.digest() 110 | 111 | 112 | 113 | def Main(): 114 | print "***Advanced Encryption Standard (AES) is use to encrypt all files in this program***" 115 | print "*Enter E to Encrypt the File" 116 | print "*Enter D to Decrypt the File" 117 | choice = raw_input("Do you want to Encrypt or Decrypt?: ") 118 | 119 | 120 | 121 | if choice == 'E': 122 | 123 | filename = raw_input("Enter the File name: ") 124 | #print "Password :" 125 | 126 | #getting password without showing characters entered to the program 127 | password = getpass.getpass() 128 | print "Encrypting ..." 129 | 130 | encrypt(getKey(password), filename) 131 | 132 | print "Encryption Done." 133 | 134 | elif choice == 'D': 135 | 136 | filename = raw_input("Enter the File name: ") 137 | 138 | password = getpass.getpass() 139 | 140 | decrypt(getKey(password), filename) 141 | 142 | print "Decryption Done." 143 | 144 | else: 145 | 146 | print "Wrong Input!!!" 147 | 148 | 149 | 150 | if __name__ == '__main__': 151 | 152 | Main() 153 | 154 | 155 | 156 | 157 | 158 | 159 | --------------------------------------------------------------------------------