Skip to content
Newer
Older
100644 55 lines (40 sloc) 1.71 KB
f16b361 @asweigart Initial commit.
authored
1 # Transposition Cipher Decryption
e9fb124 @asweigart Various changes.
authored
2 # http://inventwithpython.com/hacking (BSD Licensed)
f16b361 @asweigart Initial commit.
authored
3
4 import math, pyperclip
5
6 def main():
7 myMessage = 'Cenoonommstmme oo snnio. s s c'
8 myKey = 8
9
e9fb124 @asweigart Various changes.
authored
10 plaintext = decryptMessage(myKey, myMessage)
f16b361 @asweigart Initial commit.
authored
11
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
12 # Print with a | (called "pipe" character) after it in case
13 # there are spaces at the end of the decrypted message.
e9fb124 @asweigart Various changes.
authored
14 print(plaintext + '|')
f16b361 @asweigart Initial commit.
authored
15
e9fb124 @asweigart Various changes.
authored
16 pyperclip.copy(plaintext)
f16b361 @asweigart Initial commit.
authored
17
18
19 def decryptMessage(key, message):
20 # The transposition decrypt function will simulate the "columns" and
29eac1b @asweigart Whole buncha changes. (Worst log message ever, I know.)
authored
21 # "rows" of the grid that the plaintext is written on by using a list
22 # of strings. First, we need to calculate a few values.
f16b361 @asweigart Initial commit.
authored
23
26494d6 @asweigart Updates from Ari's edits and consistency checks.
authored
24 # The number of "columns" in our transposition grid:
f16b361 @asweigart Initial commit.
authored
25 numOfColumns = math.ceil(len(message) / key)
26494d6 @asweigart Updates from Ari's edits and consistency checks.
authored
26 # The number of "rows" in our grid will need:
f16b361 @asweigart Initial commit.
authored
27 numOfRows = key
28 # The number of "shaded boxes" in the last "column" of the grid:
29 numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)
30
24fc561 @asweigart Changes from unit testing.
authored
31 # Each string in plaintext represents a column in the grid.
f16b361 @asweigart Initial commit.
authored
32 plaintext = [''] * numOfColumns
33
24fc561 @asweigart Changes from unit testing.
authored
34 # The col and row variables point to where in the grid the next
35 # character in the encrypted message will go.
f16b361 @asweigart Initial commit.
authored
36 col = 0
37 row = 0
38
29eac1b @asweigart Whole buncha changes. (Worst log message ever, I know.)
authored
39 for symbol in message:
40 plaintext[col] += symbol
f16b361 @asweigart Initial commit.
authored
41 col += 1 # point to next column
42
43 # If there are no more columns OR we're at a shaded box, go back to
44 # the first column and the next row.
45 if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes):
46 col = 0
47 row += 1
48
49 return ''.join(plaintext)
50
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
51
f16b361 @asweigart Initial commit.
authored
52 # If transpositionDecrypt.py is run (instead of imported as a module) call
53 # the main() function.
54 if __name__ == '__main__':
91f8abb @asweigart First round of personal fixes. This is an unhelpful log message.
authored
55 main()
Something went wrong with that request. Please try again.