|
| 1 | +# File Encryptor and Decryptor |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The File Encryptor and Decryptor is a Python project that provides a simple yet secure way to encrypt and decrypt files. Using the `cryptography` library, this project allows users to generate a key, encrypt files with this key, and decrypt files using the same key. This ensures that sensitive data is protected from unauthorized access. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Key Generation**: Generate a secure encryption key and save it to a file. |
| 10 | +- **File Encryption**: Encrypt any file and save the encrypted data to a new file. |
| 11 | +- **File Decryption**: Decrypt an encrypted file using the saved key and save the decrypted data to a new file. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Before you begin, ensure you have met the following requirements: |
| 16 | + |
| 17 | +- Python 3.x installed on your computer. |
| 18 | +- `cryptography` library installed. You can install it using pip: |
| 19 | + |
| 20 | +bash |
| 21 | + |
| 22 | +Copy code |
| 23 | + |
| 24 | +`pip install cryptography` |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +1. **Clone the repository:** |
| 29 | + |
| 30 | +``` |
| 31 | +git clone https://github.com/JawadSher/Python-Projects-Beginner-to-Advance/tree/main/Project%2012%20-%20File%20Encryptor%20Decryptor |
| 32 | +``` |
| 33 | + |
| 34 | +2. **Navigate to the project directory:** |
| 35 | + |
| 36 | + |
| 37 | +`cd File-Encryptor-Decryptor` |
| 38 | + |
| 39 | +3. **Run the script:** |
| 40 | + |
| 41 | + |
| 42 | +`python encryptor_decryptor.py` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Generate a Key |
| 47 | + |
| 48 | +1. Run the script and choose option `1` to generate a key. |
| 49 | +2. Enter the desired filename to save the key (e.g., `mykey`). |
| 50 | +3. The key will be saved in a file named `mykey.key`. |
| 51 | + |
| 52 | +### Encrypt a File |
| 53 | + |
| 54 | +1. Run the script and choose option `2` to encrypt a file. |
| 55 | +2. Enter the name of the file you wish to encrypt. |
| 56 | +3. Enter the name of the file to save the encrypted data (e.g., `encrypted_data`). |
| 57 | +4. The encrypted data will be saved in a file named `encrypted_data.txt`. |
| 58 | + |
| 59 | +### Decrypt a File |
| 60 | + |
| 61 | +1. Run the script and choose option `3` to decrypt a file. |
| 62 | +2. Enter the name of the file you wish to decrypt. |
| 63 | +3. Enter the name of the file to save the decrypted data (e.g., `decrypted_data`). |
| 64 | +4. The decrypted data will be saved in a file named `decrypted_data.txt`. |
| 65 | + |
| 66 | +### Exit |
| 67 | + |
| 68 | +- Choose option `4` to exit the script. |
| 69 | + |
| 70 | +## Code Explanation |
| 71 | + |
| 72 | +### Key Generation |
| 73 | + |
| 74 | + |
| 75 | +``` |
| 76 | +def generate_key(): |
| 77 | + key = Fernet.generate_key() |
| 78 | + k_file = input("Enter file to save key : ") |
| 79 | + with open(f"{k_file}.key", 'wb') as key_file: |
| 80 | + key_file.write(key) |
| 81 | + print(f"Key saved successfully at file: {k_file}.key") |
| 82 | +``` |
| 83 | + |
| 84 | +- Generates a key using `Fernet.generate_key()`. |
| 85 | +- Saves the key to a specified file. |
| 86 | + |
| 87 | +### Load Key |
| 88 | + |
| 89 | + |
| 90 | +``` |
| 91 | +def load_key(): |
| 92 | + key_file = input("Enter the key file Name to load key: ") |
| 93 | + return open(f'{key_file}.key', 'rb').read() |
| 94 | +``` |
| 95 | + |
| 96 | +- Loads the key from a specified file. |
| 97 | + |
| 98 | +### Encrypt File |
| 99 | + |
| 100 | + |
| 101 | +``` |
| 102 | +def encrypt_file(f_name): |
| 103 | + key = load_key() |
| 104 | + fernet = Fernet(key) |
| 105 | + with open(f'{f_name}', 'rb') as file: |
| 106 | + f_data = file.read() |
| 107 | + encrypted_data = fernet.encrypt(f_data) |
| 108 | + f_name = input("Enter the file to save Encrypted Data : ") |
| 109 | + with open(f'{f_name}.txt', 'wb') as file: |
| 110 | + file.write(encrypted_data) |
| 111 | + print(f"Data Encrypted successfully at file: {f_name}") |
| 112 | +``` |
| 113 | + |
| 114 | +- Reads the file to be encrypted. |
| 115 | +- Encrypts the data using the loaded key. |
| 116 | +- Saves the encrypted data to a specified file. |
| 117 | + |
| 118 | +### Decrypt File |
| 119 | + |
| 120 | +``` |
| 121 | +def decrypt_file(f_name): |
| 122 | + key = load_key() |
| 123 | + fernet = Fernet(key) |
| 124 | + data = open(f"{f_name}", 'rb').read() |
| 125 | + decrypted_data = fernet.decrypt(data) |
| 126 | + n_file = input("Enter file name to save Decrypted Data : ") |
| 127 | + with open(f"{n_file}.txt", 'wb') as file: |
| 128 | + file.write(decrypted_data) |
| 129 | + print(f"Plain text data saved at file: {n_file}.txt") |
| 130 | +``` |
| 131 | + |
| 132 | +- Reads the encrypted file. |
| 133 | +- Decrypts the data using the loaded key. |
| 134 | +- Saves the decrypted data to a specified file. |
| 135 | + |
| 136 | +### Main Menu |
| 137 | + |
| 138 | +``` |
| 139 | +def main(): |
| 140 | + print('------> Welcome to File Encryptor and Decryptor <------') |
| 141 | + print() |
| 142 | + print("1. Generate Key") |
| 143 | + print("2. Encrypt File") |
| 144 | + print("3. Decrypt File") |
| 145 | + print("4. Exit") |
| 146 | + print() |
| 147 | + choice = input("Enter your choice : ") |
| 148 | + if choice == '1': |
| 149 | + generate_key() |
| 150 | + elif choice == '2': |
| 151 | + file_name = input("Enter the name of the file to encrypt: ") |
| 152 | + encrypt_file(file_name) |
| 153 | + print(f"{file_name} has been encrypted.") |
| 154 | + elif choice == '3': |
| 155 | + file_name = input("Enter the name of the file to decrypt: ") |
| 156 | + decrypt_file(file_name) |
| 157 | + print(f"{file_name} has been decrypted.") |
| 158 | + elif choice == '4': |
| 159 | + exit() |
| 160 | + else: |
| 161 | + print("Invalid choice. Please try again.") |
| 162 | +
|
| 163 | +if __name__ == '__main__': |
| 164 | + while True: |
| 165 | + main() |
| 166 | +``` |
| 167 | + |
| 168 | +- Provides a menu for the user to choose an action. |
| 169 | +- Executes the chosen action. |
| 170 | + |
| 171 | +## Execution Examples |
| 172 | + |
| 173 | +### Example 1: Generate a Key |
| 174 | + |
| 175 | +1. **Run the script** and choose option `1` to generate a key: |
| 176 | + |
| 177 | + |
| 178 | + `python encryptor_decryptor.py` |
| 179 | + |
| 180 | +2. **Select Option 1:** |
| 181 | + |
| 182 | + |
| 183 | + ``` |
| 184 | + ------> Welcome to File Encryptor and Decryptor <------ |
| 185 | + |
| 186 | + 1. Generate Key |
| 187 | + 2. Encrypt File |
| 188 | + 3. Decrypt File |
| 189 | + 4. Exit |
| 190 | + |
| 191 | + Enter your choice: 1 |
| 192 | + ``` |
| 193 | + |
| 194 | +3. **Enter the desired filename to save the key:** |
| 195 | + |
| 196 | + Copy code |
| 197 | + |
| 198 | + `Enter file to save key: mykey |
| 199 | + Key saved successfully at file: mykey.key` |
| 200 | + |
| 201 | +
|
| 202 | +### Example 2: Encrypt a File |
| 203 | +
|
| 204 | +1. **Run the script** and choose option `2` to encrypt a file: |
| 205 | + |
| 206 | +
|
| 207 | + |
| 208 | + `python encryptor_decryptor.py` |
| 209 | + |
| 210 | +2. **Select Option 2:** |
| 211 | + |
| 212 | + |
| 213 | + ``` |
| 214 | + ------> Welcome to File Encryptor and Decryptor <------ |
| 215 | + |
| 216 | + 1. Generate Key |
| 217 | + 2. Encrypt File |
| 218 | + 3. Decrypt File |
| 219 | + 4. Exit |
| 220 | + |
| 221 | + Enter your choice: 2 |
| 222 | + ``` |
| 223 | + |
| 224 | +3. **Enter the name of the file you wish to encrypt:** |
| 225 | + |
| 226 | + |
| 227 | + `Enter the name of the file to encrypt: plaintext.txt` |
| 228 | + |
| 229 | +4. **Enter the name of the file to save the encrypted data:** |
| 230 | + |
| 231 | + |
| 232 | + ``` |
| 233 | + Enter the file to save Encrypted Data: encrypted_data |
| 234 | + Data Encrypted successfully at file: encrypted_data |
| 235 | + ``` |
| 236 | + |
| 237 | +
|
| 238 | +### Example 3: Decrypt a File |
| 239 | +
|
| 240 | +1. **Run the script** and choose option `3` to decrypt a file: |
| 241 | + |
| 242 | + |
| 243 | + `python encryptor_decryptor.py` |
| 244 | + |
| 245 | +2. **Select Option 3:** |
| 246 | + |
| 247 | + |
| 248 | + ``` |
| 249 | + ------> Welcome to File Encryptor and Decryptor <------ |
| 250 | + |
| 251 | + 1. Generate Key |
| 252 | + 2. Encrypt File |
| 253 | + 3. Decrypt File |
| 254 | + 4. Exit |
| 255 | + |
| 256 | + Enter your choice: 3 |
| 257 | + ``` |
| 258 | + |
| 259 | +3. **Enter the name of the file you wish to decrypt:** |
| 260 | + |
| 261 | + `Enter the name of the file to decrypt: encrypted_data.txt` |
| 262 | + |
| 263 | +4. **Enter the name of the file to save the decrypted data:** |
| 264 | + |
| 265 | + |
| 266 | + `Enter file name to save Decrypted Data: decrypted_data |
| 267 | + Plain text data saved at file: decrypted_data.txt` |
| 268 | + |
| 269 | +
|
| 270 | +### Example 4: Exit the Script |
| 271 | +
|
| 272 | +1. **Run the script** and choose option `4` to exit: |
| 273 | + |
| 274 | + |
| 275 | + `python encryptor_decryptor.py` |
| 276 | + |
| 277 | +2. **Select Option 4:** |
| 278 | + |
| 279 | + |
| 280 | + ``` |
| 281 | + ------> Welcome to File Encryptor and Decryptor <------ |
| 282 | + |
| 283 | + 1. Generate Key |
| 284 | + 2. Encrypt File |
| 285 | + 3. Decrypt File |
| 286 | + 4. Exit |
| 287 | + |
| 288 | + Enter your choice: 4 |
| 289 | + ``` |
| 290 | + |
| 291 | +3. **Exit the script:** |
| 292 | + |
| 293 | + |
| 294 | + `Goodbye!` |
0 commit comments