-
Notifications
You must be signed in to change notification settings - Fork 2
/
zigzag_cipher.py
36 lines (27 loc) · 953 Bytes
/
zigzag_cipher.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
# rail - fence cipher decode
# ---VAD3R---
def fence(width, numrails):
fence = [[None] * len(width) for n in range(numrails)]
rails = range(numrails - 1) + range(numrails - 1, 0, -1)
for n, x in enumerate(width):
fence[rails[n % len(rails)]][n] = x
if 0: # debug rails > 3
for rail in fence: # StackOverflow/George
print (''.join('.' if c is None else str(c) for c in rail))
return [c for rail in fence for c in rail if c is not None]
def encode(text, n):
return ''.join(fence(text, n))
def decode(text, n):
rng = range(len(text))
pos = fence(rng, n)
return ''.join(text[pos.index(n)] for n in rng)
z = encode('ATTACK.AT.DAWN', 3)
print (z) # ACTWTAKA.ANT.D
y = decode(z, 3)
print (y) # ATTACK.AT.DAWN
'''
# n - rails
for i in range(3,15):
y = decode(z, i)
print y # ATTACK.AT.DAWN
'''