Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assingment-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Someshwaran R
Assingment-1
Commits
762eeb66
Commit
762eeb66
authored
10 months ago
by
Someshwaran R
Browse files
Options
Downloads
Patches
Plain Diff
$ python file_encrypt.py --file path/to/file.txt --algorithm AES --key mysecretpassword
parent
36c22cb8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
file_encrypt.py.txt
+54
-0
54 additions, 0 deletions
file_encrypt.py.txt
with
54 additions
and
0 deletions
file_encrypt.py.txt
0 → 100644
+
54
−
0
View file @
762eeb66
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment