Here’s a simple Python tool for file encryption and decryption using the cryptography library. This tool will allow you to encrypt a file with a password and later decrypt it using the same password.
Requirements:
First, install the required library:
File Encryption and Decryption Tool Code:
Explanation of the Code:
-
Key Derivation:
Thederive_keyfunction uses PBKDF2 to generate a secure key from the password and a random salt. This is essential for secure encryption. -
Encryption:
-
A random 16-byte salt is generated and stored with the encrypted file to ensure the encryption process is unique every time.
-
A random initialization vector (IV) is also generated for the AES cipher in CBC mode.
-
The file’s content is padded to make its size a multiple of the block size (128 bits) before encryption.
-
After encryption, the salt, IV, and encrypted content are saved into the output file.
-
-
Decryption:
-
The salt and IV are read from the encrypted file.
-
The key is derived from the password and the salt.
-
AES decryption is performed, followed by unpadding to restore the original file content.
-
How to Use:
-
Run the script.
-
Choose whether to encrypt or decrypt a file.
-
Provide the file path, password, and output file name.
The tool will either encrypt or decrypt the file as requested and save the result.
Let me know if you need help modifying or expanding the tool!