-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rail Fence Algorithm (E&D).py
65 lines (48 loc) · 992 Bytes
/
Rail Fence Algorithm (E&D).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
def encrypt(s,n):
fence = [[] for i in range(n)]
rail = 0
var = 1
for char in s:
fence[rail].append(char)
rail += var
if rail == n-1 or rail == 0:
var = -var
res = ''
for i in fence:
for j in i:
res += j
return res
def decrypt(s,n):
fence = [[] for i in range(n)]
rail = 0
var = 1
for char in s:
fence[rail].append(char)
rail += var
if rail == n-1 or rail == 0:
var = -var
rFence = [[] for i in range(n)]
i = 0
l = len(s)
s = list(s)
for r in fence:
for j in range(len(r)):
rFence[i].append(s[0])
s.remove(s[0])
i += 1
rail = 0
var = 1
r = ''
for i in range(l):
r += rFence[rail][0]
rFence[rail].remove(rFence[rail][0])
rail += var
if rail == n-1 or rail == 0:
var = -var
return r
plaintext=input("Enter PlainText: ")
depth = int(input("Enter Depth: "))
cyphertext = encrypt(plaintext, depth)
print("\nCipherText: ",cyphertext)
plaintext_rt = decrypt(cyphertext, depth)
print("\nPlainText: ",plaintext_rt)