-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptogram.py
executable file
·152 lines (141 loc) · 4.45 KB
/
cryptogram.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
letters = "abcdefghijklmnopqrstuvwxyz"
english_frequency = "etaoinsrhldcumfpgwybvkxjqz"
UNASSIGNED = "?"
assignment = { x: UNASSIGNED for x in letters }
ciphertext = ""
def sort_by_frequency(x):
import collections
return [value for value,count in collections.Counter(x).most_common()]
def print_clear():
print("\u001b[2J\u001b[H")
def print_color(x, color="gray"):
COLORCODE = {
"black": "\u001b[30m",
"gray": "\u001b[30;1m", # "bright black"
"grey": "\u001b[30;1m",
"red": "\u001b[31m",
"green": "\u001b[32m",
"yellow": "\u001b[33m",
"blue": "\u001b[34m",
"magenta":"\u001b[35m",
"cyan": "\u001b[36m",
"white": "\u001b[37m",
}
print(COLORCODE[color], end='')
print(x, end='')
print("\u001b[0m", end='') # reset
def print_newline():
print()
def display(active_cipher=None):
print_clear()
print_color("substitution", "white")
print_newline()
for x in letters:
if x == active_cipher:
print_color(x, "blue")
elif assignment[x] == UNASSIGNED:
print_color(x)
else:
print_color(x, "green")
print_newline()
for x in letters:
if x == active_cipher:
print_color(x, "blue")
elif assignment[x] == UNASSIGNED:
print_color("?")
else:
print_color(assignment[x], "green")
print_newline()
print_newline()
print_color("frequency", "white")
print_newline()
ciphertext_frequency = "".join(sort_by_frequency([x for x in ciphertext.lower() if x in letters]))
for x in ciphertext_frequency:
if x == active_cipher:
print_color(x, "blue")
elif assignment[x] == UNASSIGNED:
print_color(x)
else:
print_color(x, "green")
print_newline()
for x in english_frequency:
count = len([y for y in letters if assignment[y] == x])
if active_cipher and x == assignment[active_cipher]:
print_color(x, "blue")
elif count > 1:
print_color(x, "red")
elif count == 1:
print_color(x, "green")
elif count == 0:
print_color(x)
print_newline()
print_newline()
print_color("cipher", "white")
print_newline()
for x in ciphertext:
if x.lower() not in letters:
print_color(x)
elif x.lower() == active_cipher:
print_color(x, "blue")
elif assignment[x.lower()] == UNASSIGNED:
print_color(x)
else:
print_color(x, "green")
print_newline()
for x in ciphertext:
if x.lower() not in letters:
print_color(x)
continue
if assignment[x.lower()] == UNASSIGNED:
plaintext = "?"
elif x in assignment and assignment[x] != UNASSIGNED:
plaintext = assignment[x]
else:
plaintext = assignment[x.lower()].upper()
count = len([y for y in letters if assignment[y] == assignment[x.lower()]])
if x.lower() == active_cipher:
print_color(plaintext, "blue")
elif plaintext == "?":
print_color("?")
elif count > 1:
print_color(plaintext, "red")
else:
print_color(plaintext, "green")
print_newline()
import sys, termios, tty
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def get_okch(ok, error):
while True:
c = getch().lower()
if c in ok:
return c
else:
print(repr(c), error)
if __name__ == "__main__":
assert len(sys.argv) == 2
ciphertext = sys.argv[1]
while True:
display()
c = get_okch(ciphertext.lower() + "\r\x7f", "not in ciphertext, or enter (exit), or backspace (clear all)")
if c == "\r":
sys.exit(1)
elif c == "\x7f":
assignment = { x: UNASSIGNED for x in letters }
continue
display(c)
p = None
p = get_okch(letters + " \r\x7f", "not an english letter, space (clear), or enter (exit)")
if p == " " or p == "\x7f":
assignment[c] = UNASSIGNED
elif p == "\r":
sys.exit(1)
else:
assignment[c] = p