-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub.py
executable file
·75 lines (64 loc) · 1.78 KB
/
sub.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
#!/usr/bin/env python3
"""A program to demo substitution as a crypto technique."""
import argparse
from collections import defaultdict
key = {
"a": "q",
"b": "w",
"c": "e",
"d": "r",
"e": "t",
"f": "y",
"g": "u",
"h": "i",
"i": "o",
"j": "p",
"k": "a",
"l": "s",
"m": "d",
"n": "f",
"o": "g",
"p": "h",
"q": "j",
"r": "k",
"s": "l",
"t": "z",
"u": "x",
"v": "c",
"w": "v",
"x": "b",
"y": "n",
"z": "m",
}
rev_key = {value: key for key, value in key.items()}
def encrypt(string: str) -> str:
"""Sub using key."""
return "".join([key[c] for c in string])
def decrypt(string: str) -> str:
"""Sub using reverse key."""
return "".join([rev_key[c] for c in string])
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Substitute characters in string using key."
)
parser.add_argument(
"words", metavar="word", type=str, nargs="+", help="Words to encrypt/decrypt"
)
parser.add_argument("--decrypt", action="store_true", help="decrypt string")
parser.add_argument("--stats", action="store_true", help="print stats of string")
args = parser.parse_args()
if args.decrypt:
crypto_text = " ".join([decrypt(word.lower()) for word in args.words])
else:
crypto_text = " ".join([encrypt(word.lower()) for word in args.words])
print(crypto_text)
if args.stats:
print("\n")
print("Stats:")
counts = defaultdict(int)
for letter in crypto_text:
counts[letter] += 1
sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)
for letter, count in sorted_counts:
if letter != " ":
print(f"\t{letter} = {count}")