├── License.txt ├── README.md └── script.py /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Muhammad Ali Zia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-File-Encryptor 2 | Encrypt and Decrypt files (AES using CBC Mode) in Python. For a detailed text/video based tutorial you might want to check out this [link](https://www.youtube.com/watch?v=UB2VX4vNUa0). 3 | 4 | ## Getting Started 5 | 6 | ### Installation 7 | * Python 3.x 8 | * pycrypto 9 | 10 | You can install the missing dependencies thorugh `pip` 11 | 12 | ## Contributing 13 | 14 | 1. Fork it 15 | 2. Create your feature branch: git checkout -b my-new-feature 16 | 3. Commit your changes: git commit -am 'Add some feature' 17 | 4. Push to the branch: git push origin my-new-feature 18 | 5. Submit a pull request 19 | 20 | ## Authors 21 | 22 | + Muhammad Ali Zia 23 | 24 | ## License 25 | 26 | This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/the-javapocalypse/Python-File-Encryptor/blob/master/License.txt) file for details 27 | -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | from Crypto import Random 4 | from Crypto.Cipher import AES 5 | import os 6 | import os.path 7 | from os import listdir 8 | from os.path import isfile, join 9 | import time 10 | 11 | 12 | class Encryptor: 13 | def __init__(self, key): 14 | self.key = key 15 | 16 | def pad(self, s): 17 | return s + b"\0" * (AES.block_size - len(s) % AES.block_size) 18 | 19 | def encrypt(self, message, key, key_size=256): 20 | message = self.pad(message) 21 | iv = Random.new().read(AES.block_size) 22 | cipher = AES.new(key, AES.MODE_CBC, iv) 23 | return iv + cipher.encrypt(message) 24 | 25 | def encrypt_file(self, file_name): 26 | with open(file_name, 'rb') as fo: 27 | plaintext = fo.read() 28 | enc = self.encrypt(plaintext, self.key) 29 | with open(file_name + ".enc", 'wb') as fo: 30 | fo.write(enc) 31 | os.remove(file_name) 32 | 33 | def decrypt(self, ciphertext, key): 34 | iv = ciphertext[:AES.block_size] 35 | cipher = AES.new(key, AES.MODE_CBC, iv) 36 | plaintext = cipher.decrypt(ciphertext[AES.block_size:]) 37 | return plaintext.rstrip(b"\0") 38 | 39 | def decrypt_file(self, file_name): 40 | with open(file_name, 'rb') as fo: 41 | ciphertext = fo.read() 42 | dec = self.decrypt(ciphertext, self.key) 43 | with open(file_name[:-4], 'wb') as fo: 44 | fo.write(dec) 45 | os.remove(file_name) 46 | 47 | def getAllFiles(self): 48 | dir_path = os.path.dirname(os.path.realpath(__file__)) 49 | dirs = [] 50 | for dirName, subdirList, fileList in os.walk(dir_path): 51 | for fname in fileList: 52 | if (fname != 'script.py' and fname != 'data.txt.enc'): 53 | dirs.append(dirName + "\\" + fname) 54 | return dirs 55 | 56 | def encrypt_all_files(self): 57 | dirs = self.getAllFiles() 58 | for file_name in dirs: 59 | self.encrypt_file(file_name) 60 | 61 | def decrypt_all_files(self): 62 | dirs = self.getAllFiles() 63 | for file_name in dirs: 64 | self.decrypt_file(file_name) 65 | 66 | 67 | key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e' 68 | enc = Encryptor(key) 69 | clear = lambda: os.system('cls') 70 | 71 | if os.path.isfile('data.txt.enc'): 72 | while True: 73 | password = str(input("Enter password: ")) 74 | enc.decrypt_file("data.txt.enc") 75 | p = '' 76 | with open("data.txt", "r") as f: 77 | p = f.readlines() 78 | if p[0] == password: 79 | enc.encrypt_file("data.txt") 80 | break 81 | 82 | while True: 83 | clear() 84 | choice = int(input( 85 | "1. Press '1' to encrypt file.\n2. Press '2' to decrypt file.\n3. Press '3' to Encrypt all files in the directory.\n4. Press '4' to decrypt all files in the directory.\n5. Press '5' to exit.\n")) 86 | clear() 87 | if choice == 1: 88 | enc.encrypt_file(str(input("Enter name of file to encrypt: "))) 89 | elif choice == 2: 90 | enc.decrypt_file(str(input("Enter name of file to decrypt: "))) 91 | elif choice == 3: 92 | enc.encrypt_all_files() 93 | elif choice == 4: 94 | enc.decrypt_all_files() 95 | elif choice == 5: 96 | exit() 97 | else: 98 | print("Please select a valid option!") 99 | 100 | else: 101 | while True: 102 | clear() 103 | password = str(input("Setting up stuff. Enter a password that will be used for decryption: ")) 104 | repassword = str(input("Confirm password: ")) 105 | if password == repassword: 106 | break 107 | else: 108 | print("Passwords Mismatched!") 109 | f = open("data.txt", "w+") 110 | f.write(password) 111 | f.close() 112 | enc.encrypt_file("data.txt") 113 | print("Please restart the program to complete the setup") 114 | time.sleep(15) 115 | 116 | 117 | 118 | --------------------------------------------------------------------------------