Skip to content
Snippets Groups Projects
Commit 762eeb66 authored by Someshwaran R's avatar Someshwaran R :ghost:
Browse files

$ python file_encrypt.py --file path/to/file.txt --algorithm AES --key mysecretpassword

parent 36c22cb8
No related branches found
No related tags found
No related merge requests found
import argparse
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
def encrypt_file(file_path, algorithm, key):
with open(file_path, 'rb') as file:
plaintext = file.read()
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(key.encode())
cipher = Cipher(algorithm(key), modes.CFB8(os.urandom(16)), backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
encrypted_file_path = file_path + '.enc'
with open(encrypted_file_path, 'wb') as encrypted_file:
encrypted_file.write(salt + ciphertext)
print(f'File encrypted successfully as {encrypted_file_path}')
if _name_ == '_main_':
parser = argparse.ArgumentParser(description='File Encryption Tool')
parser.add_argument('--file', required=True, help='Path to the file to be encrypted')
parser.add_argument('--algorithm', required=True, choices=['AES', 'DES', '3DES'], help='Encryption algorithm (AES, DES, or 3DES)')
parser.add_argument('--key', required=True, help='Secret key for encryption')
args = parser.parse_args()
algorithms_map = {
'AES': algorithms.AES,
'DES': algorithms.DES,
'3DES': algorithms.TripleDES
}
if args.algorithm not in algorithms_map:
print('Invalid algorithm')
exit(1)
if not os.path.exists(args.file):
print('File does not exist')
exit(1)
encrypt_file(args.file, algorithms_map[args.algorithm], args.key)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment