-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrail_fence_cipher.py
More file actions
98 lines (81 loc) · 1.56 KB
/
rail_fence_cipher.py
File metadata and controls
98 lines (81 loc) · 1.56 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def encode(message, rails):
message=message.replace(' ','')
encoded=''
next=0
for i in range(rails):
if i in [0,rails-1]:
suffix=rails*2-2
while next<len(message):
encoded+=message[next]
next+=suffix
next=i+1
else:
next=i
down=True
while next<len(message):
encoded+=message[next]
if down:
suffix=rails*2-2-2*i
else:
suffix=2*i
next+=suffix
down= not down
next=i+1
return encoded
def decode(encoded_message, rails):
#calculate the length of rails
length=[]
for i in range(rails):
if i==0:
length.append(len(encoded_message)//(2*rails-2))
elif i==rails-1:
length.append(length[0])
else:
length.append(length[0]*2)
remainder=len(encoded_message)%(2*rails-2)
down=True
i=0
while remainder>0:
length[i]+=1
remainder-=1
if down:
i+=1
else:
i-=1
if i==len(length)-1:
down=not down
#creating rails based on length
start=0
rail_list=[]
for width in length:
rail_list.append(list(encoded_message[start:start+width]))
start+=width
#decrypting the message from rails
down=True
decoded=''
i=0
while True:
decoded+=rail_list[i][0]
del(rail_list[i][0])
if i==0:
down=True
elif i==rails-1:
down=False
if down:
i+=1
else:
i-=1
#checking if entire message is decoded
is_empty=False
count=0
for j in range(rails):
if len(rail_list[j])==0:
count+=1
if count==rails:
is_empty=True
break
else:
break
if is_empty:
break
return decoded