Skip to content

Commit 473022a

Browse files
committed
Cryptography Algorithm
1 parent 1794366 commit 473022a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Transposition Cipher.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import math
2+
3+
def main():
4+
message = input('Enter message: ')
5+
key = int(input('Enter key [2-%s]: ' % (len(message) - 1)))
6+
mode = input('Encryption/Decryption [e/d]: ')
7+
8+
if mode.lower().startswith('e'):
9+
text = encryptMessage(key, message)
10+
elif mode.lower().startswith('d'):
11+
text = decryptMessage(key, message)
12+
13+
# Append pipe symbol (vertical bar) to identify spaces at the end.
14+
print('Output:\n%s' %(text + '|'))
15+
16+
def encryptMessage(key, message):
17+
cipherText = [''] * key
18+
for col in range(key):
19+
pointer = col
20+
while pointer < len(message):
21+
cipherText[col] += message[pointer]
22+
pointer += key
23+
return ''.join(cipherText)
24+
25+
def decryptMessage(key, message):
26+
numCols = math.ceil(len(message) / key)
27+
numRows = key
28+
numShadedBoxes = (numCols * numRows) - len(message)
29+
plainText = [""] * numCols
30+
col = 0; row = 0;
31+
32+
for symbol in message:
33+
plainText[col] += symbol
34+
col += 1
35+
36+
if (col == numCols) or (col == numCols - 1) and (row >= numRows - numShadedBoxes):
37+
col = 0
38+
row += 1
39+
40+
return "".join(plainText)
41+
42+
if __name__ == '__main__':
43+
main()

0 commit comments

Comments
 (0)