A simple AES-based file encryption and decryption module in Python with password-based key generation. Supports encrypting and decrypting any file using a password entered from the command line.
- AES encryption (CFB mode)
- Password-based key derivation (PBKDF2HMAC + SHA256)
- Modular code:
utils.pyhandles encryption/decryption logic - CLI-based usage for files
- Supports any file type (images, documents, etc.)
File_Encryption_Decryption/
├── encryptor.py # CLI tool to encrypt files
├── decryptor.py # CLI tool to decrypt files
├── utils.py # Encryption/decryption helper functions
├── requirements.txt # Dependencies
└── sample.jpg # Test file
- Clone the repository:
git clone https://github.com/haragam22/Code_Script
cd Code_Script/Python/File_Encryption_Decryption- Create a virtual environment:
python -m venv venv- Activate the virtual environment:
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate- Install dependencies:
pip install -r requirements.txtpython encryptor.py <file_path>Example:
python encryptor.py sample.jpg- Enter a password when prompted.
- Creates an encrypted file:
sample.jpg.enc.
python decryptor.py <encrypted_file>Example:
python decryptor.py sample.jpg.enc- Enter the same password used for encryption.
- Restores the original file:
sample.jpg.
-
generate_key(password: str, salt: bytes = None) -> tuple[bytes, bytes]Generates a 256-bit AES key from a password using PBKDF2HMAC. Returns(key, salt). -
encrypt_data(key: bytes, plaintext: bytes) -> bytesEncrypts bytes using AES (CFB mode) and prepends IV. -
decrypt_data(key: bytes, ciphertext: bytes) -> bytesDecrypts bytes using AES (CFB mode). Expects IV prepended.