This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrypt.py
executable file
·110 lines (86 loc) · 2.2 KB
/
crypt.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python3
import click
import aes
@click.command()
@click.option(
'--encrypt/--decrypt',
'-e/-d',
default=True,
)
@click.option(
'--input_file',
'-i',
required=True,
)
@click.option(
'--output_file',
'-o',
required=True,
)
@click.option(
'--block-cipher-mode',
'-m',
type=click.Choice(["ECB", "CBC", "CTR"]),
default="CTR",
)
@click.option(
'--key-length',
'-l',
type=click.Choice(["128", "192", "256"]),
default="128",
)
def main(encrypt, input_file, output_file, block_cipher_mode, key_length):
if encrypt:
key = aes.random_key_generator(int(key_length))
if key_length == "128":
AES = aes.AES(key, 128)
elif key_length == "192":
AES = aes.AES(key, 192)
elif key_length == "256":
AES = aes.AES(key, 256)
if block_cipher_mode == "ECB":
bcm = aes.ECB(AES)
elif block_cipher_mode == "CBC":
bcm = aes.CBC(AES, 16)
elif block_cipher_mode == "CTR":
bcm = aes.CTR(AES)
bcm.cipher(input_file, output_file)
print("Cipher Key:", key)
write_key(key)
else:
key = read_key()
if key == 1:
print("File key.txt doesn't exists! Can't decrypt without key")
exit(1)
key_length = len(key) * 4
if key_length == 128:
AES = aes.AES(key, 128)
elif key_length == 192:
AES = aes.AES(key, 192)
elif key_length == 256:
AES = aes.AES(key, 256)
else:
print("Key length not valid!")
exit(1)
if block_cipher_mode == "ECB":
bcm = aes.ECB(AES)
elif block_cipher_mode == "CBC":
bcm = aes.CBC(AES, 16)
elif block_cipher_mode == "CTR":
bcm = aes.CTR(AES)
bcm.decipher(input_file, output_file)
def read_key():
try:
f = open("key.txt", "r")
except IOError:
return 1
key = f.read()
f.close()
return key
def write_key(key):
with open("key.txt", "w") as f:
f.write(key)
f.close()
if __name__ == "__main__":
# pylint: disable=no-value-for-parameter
main()