Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dcbc0ea

Browse filesBrowse files
authored
Railfence Cipher implementation in cipher
1 parent e743b42 commit dcbc0ea
Copy full SHA for dcbc0ea

File tree

1 file changed

+78
-0
lines changed
Filter options

1 file changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
def railencrypt(st,k):
2+
c = 0
3+
x = 0
4+
m =[[0] * (len(st)) for i in range(k)]
5+
for r in range(len(st)):
6+
m[c][r] = st[r]
7+
if x == 0:
8+
if c == (k-1):
9+
x = 1
10+
c -= 1
11+
else:
12+
c += 1
13+
else:
14+
if c == 0:
15+
x = 0
16+
c += 1
17+
else:
18+
c -= 1
19+
20+
result = []
21+
for i in range(k):
22+
for j in range(len(st)):
23+
if m[i][j] != 0:
24+
result.append(m[i][j])
25+
print("CipherText:","" . join(result))
26+
27+
def raildecrypt(st,k):
28+
c , x = 0 , 0
29+
m =[[0] * (len(st)) for i in range(k)]
30+
for r in range(len(st)):
31+
m[c][r] = 1
32+
if x == 0:
33+
if c == (k-1):
34+
x = 1
35+
c -= 1
36+
else:
37+
c += 1
38+
else:
39+
if c == 0:
40+
x = 0
41+
c += 1
42+
else:
43+
c -= 1
44+
result = []
45+
c , x = 0 , 0
46+
for i in range(k):
47+
for j in range(len(st)):
48+
if m[i][j] == 1:
49+
m[i][j] = st[x]
50+
x += 1
51+
for r in range(len(st)):
52+
if m[c][r] != 0:
53+
result.append(m[c][r])
54+
if x == 0:
55+
if c == (k-1):
56+
x = 1
57+
c -= 1
58+
else:
59+
c += 1
60+
else:
61+
if c == 0:
62+
x = 0
63+
c += 1
64+
else:
65+
c -= 1
66+
print("PlainText:","" . join(result))
67+
68+
if __name__ == "__main__":
69+
string = input("Enter the Message:")
70+
string = string.upper()
71+
key = int(input("Enter the Key:"))
72+
n = int(input("1.Encryption\n2.Decryption\nInput Your choice:"))
73+
if(n == 1):
74+
railencrypt(string,key)
75+
elif(n == 2):
76+
raildecrypt(string,key)
77+
else:
78+
print("Error")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.