Hacking Secret Ciphers with Python

(Ann) #1
Chapter 9 – The Transposition Cipher, Decrypting 135

Order of Operations for Boolean Operators


Just like the math operators have an order of operations, the and, or, and not operators also
have an order of operations: first not, then and, and then or. Try typing the following into the
interactive shell:





not False and False # not False evaluates first
False
not (False and False) # (False and False) evaluates first
True





Back to the Code


transpositionDecrypt.py



  1. If there are no more columns OR we're at a shaded box, go back to




  2. the first column and the next row.



  3. if (col == numOfColumns) or (col == numOfColumns - 1 and row >=
    numOfRows - numOfShadedBoxes):

  4. col = 0

  5. row += 1


There are two cases where we want to reset col back to 0 (so that on the next iteration of the
loop, symbol is added to the first string in the list in plaintext). The first is if we have
incremented col past the last index in plaintext. In this case, the value in col will be equal
to numOfColumns. (Remember that the last index in plaintext will be numOfColumns
minus one. So when col is equal to numOfColumns, it is already past the last index.)


The second case is if both col is at the last index and the row variable is pointing to a row that
has a shaded box in the last column. Here’s the complete decryption grid with the column indexes
along the top and the row indexes down the side:

Free download pdf