forked from to-sayana/File-encryption-decryption-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.py
More file actions
58 lines (47 loc) · 1.84 KB
/
src.py
File metadata and controls
58 lines (47 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
import argparse
from cryptography.fernet import Fernet
import os
def generate_key():
return Fernet.generate_key()
def encrypt_file(input_file, output_file):
key = generate_key()
cipher_suite = Fernet(key)
with open(input_file, 'rb') as file:
plaintext = file.read()
encrypted_data = cipher_suite.encrypt(plaintext)
with open(output_file, 'wb') as file:
file.write(encrypted_data)
print("File encrypted successfully.")
print("Encryption key:", key.decode())
def decrypt_file(input_file, output_file, key):
cipher_suite = Fernet(key)
with open(input_file, 'rb') as file:
encrypted_data = file.read()
decrypted_data = cipher_suite.decrypt(encrypted_data)
with open(output_file, 'wb') as file:
file.write(decrypted_data)
print("File decrypted successfully.")
def main():
parser = argparse.ArgumentParser(description="File Encryption and Decryption Tool")
parser.add_argument('action', choices=['encrypt', 'decrypt'], help="Action to perform: 'encrypt' or 'decrypt'")
parser.add_argument('input_file', help="Input file path")
parser.add_argument('--output_file', help="Output file path")
args = parser.parse_args()
if args.action == 'encrypt':
if not os.path.isfile(args.input_file):
print(f"Error: Input file '{args.input_file}' not found.")
return
if not args.output_file:
print("Error: Output file path required.")
return
encrypt_file(args.input_file, args.output_file)
elif args.action == 'decrypt':
if not args.output_file:
print("Error: Output file path required.")
return
key = input("Enter encryption key: ").encode()
decrypt_file(args.input_file, args.output_file, key)
if __name__ == "__main__":
main()
#test