-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.py
More file actions
70 lines (57 loc) · 2 KB
/
Copy pathencoder.py
File metadata and controls
70 lines (57 loc) · 2 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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Encoder -- Program 12 from "Marvel Super Heroes Computer Fun".
Faithful recreation of the type-in BASIC listing: a 5-row columnar
transposition cipher. Spaces become '*', the message is padded to a multiple
of 5 with '+-#$', then laid out in 5 rows of MM columns and read down each
column into 5-character blocks. Type one line of the message at a time
(uppercase) and press Enter; type STOP to finish.
python encoder.py
"""
import os
import sys
PAD = "+-#$"
GREEN = "\033[32m"
RESET = "\033[0m"
CLEAR = "\033[2J\033[H"
def enable_ansi():
if os.name == "nt":
os.system("")
def encode(M): # lines 190-370
MM = "".join("*" if ch == " " else ch for ch in M) # 200-220
mm = len(MM) // 5 # 240
if mm != len(MM) / 5: # 250
MM += PAD[:(mm + 1) * 5 - len(MM)] # 260
mm += 1 # 270
A = [""] * mm # 130 (per-message reset)
for I in range(5): # 290
for J in range(1, mm + 1): # 300
A[J - 1] += MM[J - 1 + I * mm] # 310
return " ".join(A) # 350-370
def play():
enable_ansi()
sys.stdout.write(CLEAR + GREEN)
print("ENTER SECRET MESSAGE")
print()
while True:
try:
line = input("? ").strip().upper()
except (EOFError, KeyboardInterrupt):
break
if line == "STOP": # 180
break
if not line:
continue
print()
print("THE CODED MESSAGE:")
print()
print(encode(line))
print()
print()
sys.stdout.write(RESET)
def main():
play()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.stdout.write(RESET + "\n")