-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrailfence_encryption.py
71 lines (58 loc) · 1.91 KB
/
railfence_encryption.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
# this function is to get the desired sequence
def sequence(n):
arr=[]
i=0
# creating the sequence required for
# implementing railfence cipher
# the sequence is stored in array
while(i<n-1):
arr.append(i)
i+=1
while(i>0):
arr.append(i)
i-=1
return(arr)
# this is to implement the logic
def railfence(s,n):
# converting into lower cases
s=s.lower()
# If you want to remove spaces,
# you can uncomment this
# s=s.replace(" ","")
# returning the sequence here
L=sequence(n)
print("The raw sequence of indices: ",L)
# storing L in temp for reducing additions in further steps
temp=L
# adjustments
while(len(s)>len(L)):
L=L+temp
# removing the extra last indices
for i in range(len(L)-len(s)):
L.pop()
print("The row indices of the characters in the given string: ",L)
print("Transformed message for encryption: ",s)
# converting into cipher text
num=0
cipher_text=""
while(num<n):
for i in range(L.count(num)):
# adding characters according to
# indices to get cipher text
cipher_text=cipher_text+s[L.index(num)]
L[L.index(num)]=n
num+=1
print("The cipher text is: ",cipher_text)
plain_text=input("Enter the string to be encrypted: ")
n=int(input("Enter the number of rails: "))
railfence(plain_text,n)
'''
----------OUTPUT----------
Enter the string to be encrypted: Hey everyone have good day
Enter the number of rails: 5
The raw sequence of indices: [0, 1, 2, 3, 4, 3, 2, 1]
The row indices of the characters in the given string: [0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1]
Transformed message for encryption: hey everyone have good day
The cipher text is: hyeaerov dyyenag vehode o
>>>
'''