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

The program should support the following features:

• Accept command-line arguments for the file path, encryption algorithm, and secret key.

• Encrypt the file using the specified encryption algorithm and secret key.

• Save the encrypted file with the original file name and a ".enc" extension.
parent d3f201cb
No related branches found
No related tags found
No related merge requests found
import argparse
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
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()
backend = default_backend()
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=backend
)
derived_key = kdf.derive(key.encode())
iv = os.urandom(16)
cipher = Cipher(algorithm=algorithms._dict_[algorithm](derived_key), mode=modes.CFB(iv), backend=backend)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
with open(file_path + '.enc', 'wb') as file:
file.write(salt + iv + ciphertext)
def main():
parser = argparse.ArgumentParser(description='Encrypt a file using specified encryption algorithm and secret key')
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')
parser.add_argument('--key', required=True, help='Secret key for encryption')
args = parser.parse_args()
encrypt_file(args.file, args.algorithm, args.key)
if _name_ == '_main_':
main()
\ 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