-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspositionCipher.py
More file actions
51 lines (39 loc) · 1.48 KB
/
transpositionCipher.py
File metadata and controls
51 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ===================================
# Columnar Transposition Cipher (CTC)
# ===================================
# Key Space <- {2,..,n/2}
# ^^^^^^^^^ where n is length of message
# Note: longer messages allow for more key choices
# ^^^^
# Example: Encrypting an entire book using CTC
# ^^^^^^^ would allow for thousands of possible keys
def main():
# message to be encrypted
plaintext = 'A day without sunshine, is like you know, night.'
# chosen key
key = 8
# encrypt message
ciphertext = encryptMessage(key, plaintext)
print(ciphertext)
# function to encrypt message using key and CTC
def encryptMessage(key, message):
# each string in ciphertext represents a column in the grid
# No. of columns = key
ciphertext = [''] * key
# loop through each column in the ciphertext
for column in range(key):
curIndex = column
# keep looping until curIndex goes past the message length
# at the point it's time to move to next column in CT
while curIndex < len(message):
# Place the character at curIndex in message at the
# end of the current column in the ciphertext list
ciphertext[column] += message[curIndex]
# Increment curIndex to next value (add key)
curIndex += key
# convert CT list into a single string value
# return new CT
return ''.join(ciphertext)
# if this file is not used as module, call main()
if __name__ == '__main__':
main()