-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_encrypt.py
More file actions
61 lines (39 loc) · 1.59 KB
/
file_encrypt.py
File metadata and controls
61 lines (39 loc) · 1.59 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
56
57
58
59
60
61
import argparse
import caesar_cipher
import file_utils
def main():
parser = argparse.ArgumentParser(description='Takes a file as argument and encrypts it')
parser.add_argument('file', metavar='file to be encrypted', type=str)
parser.add_argument('key', metavar='key to encrypt',type=int)
parser.add_argument('mode', metavar='Select the mode (encrypt, decrypt)', type=str)
args = parser.parse_args()
try:
eval(args.mode)(args.file, args.key)
except NameError:
print(f'The mode {args.mode} does not exists')
def encrypt(file, key):
try:
file_content = file_utils.get_file_content(file)
if file_content:
new_file = file_utils.store_file_content(caesar_cipher.encrypt(file_content, key), 'encriptado.txt')
print(f'content stored on {new_file}')
else:
print(f'The file {file} is empty')
except FileNotFoundError:
print(f'The file {file} does not exists')
except ValueError:
print('The selected key does not exists in the alphabet')
def decrypt(file, key):
try:
file_content = file_utils.get_file_content(file)
if file_content:
new_file = file_utils.store_file_content(caesar_cipher.decrypt(file_content, key), 'decripted.txt')
print(f'Content stored on {new_file}')
else:
print(f'The file {file} is empty')
except FileNotFoundError:
print(f'The file {file} does not exists')
except ValueError:
print('The selected key does not exists in the alphabet')
if __name__ == "__main__":
main()