This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
kse-02/benchmark/railfence_cipher.py

73 lines
1.7 KiB
Python
Raw Permalink Normal View History

2023-11-13 12:47:53 +00:00
# Based on https://github.com/AllAlgorithms, python/algorithms/math/railfence_cipher.py
def railencrypt(st: str, k: int) -> str:
assert k > 1
c = 0
x = 0
m =[[0] * (len(st)) for i in range(k)]
for r in range(len(st)):
m[c][r] = ord(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(chr(m[i][j]))
return ''.join(result)
def raildecrypt(st: str, k: int) -> str:
assert k > 1
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] = ord(st[x])
x += 1
for r in range(len(st)):
if m[c][r] != 0:
result.append(chr(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
return ''.join(result)