Hacking Secret Ciphers with Python

(Ann) #1

136 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


0 1 2 3


0 C


0


e
1

n
2

o
3
1 o
4

n
5

o
6

m
7
2 m
8

s
9

t
10

m
11
3 m
12

e
13

(s)
14

o
15
4 o
16

(s)
17

s
18

n
19
5 n
20

i
21

o
22

.


23


6 (s)
24

s
25

(s)
26
7 s
27

(s)
28

c
29

You can see that the shaded boxes are in the last column (whose index will be numOfColumns



  • 1 ) and rows 6 and 7. To have our program calculate which row indexes are shaded, we use the
    expression row >= numOfRows - numOfShadedBoxes. If this expression is True, and
    col is equal to numOfColumns - 1 , then we know that we want to reset col to 0 for the
    next iteration.


These two cases are why the condition on line 45 is (col == numOfColumns) or (col
== numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes). That
looks like a big, complicated expression but remember that you can break it down into smaller
parts. The block of code that executes will change col back to the first column by setting it to 0.
We will also increment the row variable.


transpositionDecrypt.py


  1. return ''.join(plaintext)


By the time the for loop on line 39 has finished looping over every character in message, the
plaintext list’s strings have been modified so that they are now in the decrypted order (if the
correct key was used, that is). The strings in the plaintext list are joined together (with a
blank string in between each string) by the join() string method. The string that this call to
join() returns will be the value that our decryptMessage() function returns.

Free download pdf