Enter your text and adjust the number of rails to see the Rail Fence cipher in action.
Examples of Caesar Cipher implementation in various programming languages.
def railfence_encrypt(text, rails):
fence = [[] for _ in range(rails)]
rail = 0
var = 1
for char in text:
fence[rail].append(char)
rail += var
if rail == 0 or rail == rails - 1:
var = -var
return ''.join(''.join(row) for row in fence)
def railfence_decrypt(cipher, rails):
fence = [[] for _ in range(rails)]
rail = 0
var = 1
indices = [[] for _ in range(rails)]
for i in range(len(cipher)):
indices[rail].append(i)
rail += var
if rail == 0 or rail == rails - 1:
var = -var
idx = 0
for r in range(rails):
for i in indices[r]:
fence[r].append(cipher[idx])
idx += 1
result = []
rail = 0
var = 1
for _ in range(len(cipher)):
result.append(fence[rail].pop(0))
rail += var
if rail == 0 or rail == rails - 1:
var = -var
return ''.join(result)
# Example usage
plaintext = "HELLO WORLD"
rails = 3
encrypted = railfence_encrypt(plaintext.replace(" ", ""), rails)
decrypted = railfence_decrypt(encrypted, rails)
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")