Advertisement
Guest User

transposition_decrypt.py

a guest
Nov 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import math
  2.  
  3. def main():
  4.     myMessage = 'Cenoonommstmme oo snnio. s s c'
  5.     myKey = 8
  6.  
  7.     plaintext = decryptMessage(myKey, myMessage)
  8.  
  9.     # Print with a | (called "pipe" character) after it in case
  10.     # there are spaces at the end of the decrypted message.
  11.     print(plaintext + '|')
  12.  
  13.  
  14. def decryptMessage(key, message):
  15.     # The transposition decrypt function will simulate the "columns" and
  16.     # "rows" of the grid that the plaintext is written on by using a list
  17.     # of strings. First, we need to calculate a few values.
  18.  
  19.     # The number of "columns" in our transposition grid:
  20.     numOfColumns = math.ceil(len(message) / key)
  21.     # The number of "rows" in our grid will need:
  22.     numOfRows = key
  23.     # The number of "shaded boxes" in the last "column" of the grid:
  24.     numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)
  25.  
  26.     # Each string in plaintext represents a column in the grid.
  27.     plaintext = [''] * numOfColumns
  28.  
  29.     # The col and row variables point to where in the grid the next
  30.     # character in the encrypted message will go.
  31.     col = 0
  32.     row = 0
  33.  
  34.     for symbol in message:
  35.         plaintext[col] += symbol
  36.         col += 1 # point to next column
  37.  
  38.         # If there are no more columns OR we're at a shaded box, go back to
  39.         # the first column and the next row.
  40.         if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes):
  41.             col = 0
  42.             row += 1
  43.  
  44.     return ''.join(plaintext)
  45.  
  46.  
  47. # If transpositionDecrypt.py is run (instead of imported as a module) call
  48. # the main() function.
  49. if __name__ == '__main__':
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement