I am currently working on a homework assignment, and we have to build a function where we make a three step encryption/decryption program. One of the ciphers we have to build is a transposition/rail fence that takes a variable (n) as a number of "rails" that you'd like to have the message encrypted in. I've built the encryption, but I'm at a loss of the decryption method.
This is for an intro level class to python, so we don't know too much beyond the basics like the encryption code that is included below.
If you're not sure what I mean by transposition encrypt/rail fence is here's an example...
Message = abcdefg
n = 3
It'd end up being encrypted into 3 groups (as noted by n) and those groups would be "adg be cf" and from there the encryption recombines them into one string "adgbecf". My trouble is breaking them back down into the original three strings of "adg be cf" and then transposing them back to the original values.
Encryption:
def trans_encrypt(message, n):
cipher = ""
for i in range(n):
for j in range(i, len(message), n):
cipher = cipher + message[j]
return cipher
Current decryption (doesn't work):
def trans_decrypt(cipher, n):
length = len(cipher) // n
message = ''
for i in range(length):
for j in range(n):
letter = (i + j * length)
message = message + cipher[letter]
return message