Skip to content

Commit e774ab0

Browse files
authored
Merge pull request #169 from RishabhBhatnagar/master
Add RailFence Cipher in Cryptography
2 parents 0fb0501 + f46ef8b commit e774ab0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def fence(lst, height):
2+
_fence = [[None] * len(lst) for n in range(height)]
3+
rails = [*range(0, height - 1), *range(height - 1, 0, -1)] # sawtooth.
4+
for n, x in enumerate(lst):
5+
_fence[rails[n % len(rails)]][n] = x
6+
return [c for c in sum(_fence, []) if c is not None]
7+
8+
9+
def encode(text, h):
10+
return ''.join(fence(text, h))
11+
12+
13+
def decode(cipher, h):
14+
pos = fence(range(len(cipher)), h)
15+
return ''.join(cipher[pos.index(i)] for i in range(len(cipher)))
16+
17+
18+
if __name__ == '__main__':
19+
height = 3
20+
plain_text = "Hacktoberfest"
21+
cipher_text = encode(plain_text, height)
22+
decoded = decode(cipher_text, height)
23+
24+
assert decoded == plain_text
25+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/codezoned/ScriptsDump

0 commit comments

Comments
 (0)