8000 Rail Fence cipher in python by shivansh2310 · Pull Request #121 · AllAlgorithms/python · GitHub
[go: up one dir, main page]

Skip to content

Rail Fence cipher in python #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Railfence Cipher implementation in cipher
  • Loading branch information
shivansh2310 authored Oct 21, 2020
commit dcbc0ea14a967941785d4e19a9b8962fbeee2920
78 changes: 78 additions & 0 deletions algorithms/cryptography/railfence_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 Message:")
string = string.upper()
key = int(input("Enter the Key:"))
n = int(input("1.Encryption\n2.Decryption\nInput Your choice:"))
if(n == 1):
railencrypt(string,key)
elif(n == 2):
raildecrypt(string,key)
else:
print("Error")
0