-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rail Fence.py
78 lines (75 loc) · 1.87 KB
/
Rail Fence.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
def railencrypt(st,k):
c = 0
x = 0
m =[[0] * (len(st)) for i in range(k)]
for r in range(len(st)):
m[c][r] = st[r]
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1
result = []
for i in range(k):
for j in range(len(st)):
if m[i][j] != 0:
result.append(m[i][j])
print("CipherText:","" . join(result))
def raildecrypt(st,k):
c , x = 0 , 0
m =[[0] * (len(st)) for i in range(k)]
for r in range(len(st)):
m[c][r] = 1
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1
result = []
c , x = 0 , 0
for i in range(k):
for j in range(len(st)):
if m[i][j] == 1:
m[i][j] = st[x]
x += 1
for r in range(len(st)):
if m[c][r] != 0:
result.append(m[c][r])
if x == 0:
if c == (k-1):
x = 1
c -= 1
else:
c += 1
else:
if c == 0:
x = 0
c += 1
else:
c -= 1
print("PlainText:","" . join(result))
if __name__ == "__main__":
string = input("Enter the string:")
string = string.upper()
key = int(input("Enter the Key:"))
n = int(input("1.Encryption\n2.Decryption\nEnter Your choice:"))
if(n == 1):
railencrypt(string,key)
elif(n == 2):
raildecrypt(string,key)
else:
print("Wrong Choice")