-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.py
executable file
·61 lines (47 loc) · 1.65 KB
/
encrypt.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
#!/usr/bin/python3
import argparse
import binascii
import aes
import des
from utils import hex
def main():
parser = argparse.ArgumentParser(description="Simple encryption")
parser.add_argument(
"--aes", help="AES encryption", default=False, action="store_true"
)
parser.add_argument(
"--des", help="DES encryption", default=False, action="store_true"
)
parser.add_argument("--key", help="Encryption key (hex)", type=str, required=True)
parser.add_argument("--plaintext", help="Plaintext (hex)", type=str)
parser.add_argument("--plaintext-file", help="File of plaintexts (hex)", type=str)
args = parser.parse_args()
if not args.des and not args.aes:
print("--des or --aes are required.")
return
if args.des and args.aes:
print("Can't combine --aes and --des.")
return
if not args.plaintext and not args.plaintext_file:
print("Need --plaintext or --plaintext-file")
return
tr = lambda x: binascii.unhexlify(x) if args.aes else int(x, 16)
k = tr(args.key)
if args.aes:
aes_cipher = aes.AES(k)
if args.plaintext:
p = tr(args.plaintext)
if args.aes:
print(f"{hex(aes_cipher.encrypt_block(p))}")
else:
print(f"{hex(des.DES(p, k))}")
if args.plaintext_file:
with open(args.plaintext_file, "r") as f:
for line in f.readlines():
p = tr(line.rstrip())
if args.aes:
print(f"{hex(aes_cipher.encrypt_block(p))}")
else:
print(f"{hex(des.DES(p, k))}")
if __name__ == "__main__":
main()