Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
debck committed May 20, 2018
1 parent 0d1ed30 commit c0b860a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions rail-fence-cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
def encrypt(clearText, key):
result = ""
matrix = [["" for x in range(len(clearText))] for y in range(key)]
increment = 1
row = 0
col = 0
for c in clearText:
if row + increment < 0 or row + increment >= len(matrix):
increment = increment * -1
matrix[row][col] = c
row += increment
col += 1
for mlist in matrix:
result += "".join(mlist)
return result


def decrypt(cipherText, key):
result = ""
matrix = [["" for x in range(len(cipherText))] for y in range(key)]
idx = 0
increment = 1
for selectedRow in range(0, len(matrix)):
row = 0
for col in range(0, len(matrix[row])):
if row + increment < 0 or row + increment >= len(matrix):
increment = increment * -1
if row == selectedRow:
matrix[row][col] += cipherText[idx]
idx += 1
row += increment
matrix = transpose(matrix)
for klist in matrix:
result += "".join(klist)
return result


def transpose(m):
result = [[0 for y in range(len(m))] for x in range(len(m[0]))]
for i in enumerate(m):
for j in enumerate(m[0]):
result[j][i] = m[i][j]
return result


def main():
clearText = "i am the king"
print("Original Text: " + clearText)
key = 3
cipherText = encrypt(clearText, key)
print("Encrypted Text: {0}".format(cipherText))
decipherText = decrypt(cipherText, key)
print("Decrypted Text: {0}".format(decipherText))
return


if __name__ == '__main__':
main()

0 comments on commit c0b860a

Please sign in to comment.