Advertisement
mrScarlett

transpositional

Jan 5th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # Transposition Cipher Encryption
  2. # http://inventwithpython.com/hacking (BSD Licensed)
  3. import math
  4. global ciphertext
  5. def main():
  6.    
  7.     myMessage = input("Enter a message to encrypt")
  8.     myKey = int(input("Choose a key"))
  9.  
  10.     ciphertext = encryptMessage(myKey, myMessage)
  11.  
  12.     # Print the encrypted string in ciphertext to the screen, with
  13.     # a | (called "pipe" character) after it in case there are spaces at
  14.     # the end of the encrypted message.
  15.     print(ciphertext + '|')
  16.     decrypt(ciphertext)
  17.     # Copy the encrypted string in ciphertext to the clipboard.
  18.  
  19.  
  20.  
  21. def encryptMessage(key, message):
  22.     # Each string in ciphertext represents a column in the grid.
  23.     ciphertext = [''] * key
  24.  
  25.     # Loop through each column in ciphertext.
  26.     for col in range(key):
  27.         pointer = col
  28.  
  29.         # Keep looping until pointer goes past the length of the message.
  30.         while pointer < len(message):
  31.             # Place the character at pointer in message at the end of the
  32.             # current column in the ciphertext list.
  33.             ciphertext[col] += message[pointer]
  34.  
  35.             # move pointer over
  36.             pointer += key
  37.  
  38.     # Convert the ciphertext list into a single string value and return it.
  39.     return ''.join(ciphertext)
  40.    
  41.  
  42. def decrypt(ciphertext):
  43.     myKey = 1
  44.     for x in range (100):
  45.         plaintext = decryptMessage(myKey, ciphertext)
  46.         myKey+=1
  47.         print(plaintext + '|')
  48.    
  49. def decryptMessage(key, message):
  50.     # The transposition decrypt function will simulate the "columns" and
  51.     # "rows" of the grid that the plaintext is written on by using a list
  52.     # of strings. First, we need to calculate a few values.
  53.  
  54.     # The number of "columns" in our transposition grid:
  55.     numOfColumns = math.ceil(len(message) / key)
  56.     # The number of "rows" in our grid will need:
  57.     numOfRows = key
  58.     # The number of "shaded boxes" in the last "column" of the grid:
  59.     numOfShadedBoxes = (numOfColumns * numOfRows) - len(message)
  60.  
  61.     # Each string in plaintext represents a column in the grid.
  62.     plaintext = [''] * numOfColumns
  63.  
  64.     # The col and row variables point to where in the grid the next
  65.     # character in the encrypted message will go.
  66.     col = 0
  67.     row = 0
  68.  
  69.     for symbol in message:
  70.         plaintext[col] += symbol
  71.         col += 1 # point to next column
  72.  
  73.         # If there are no more columns OR we're at a shaded box, go back to
  74.         # the first column and the next row.
  75.         if (col == numOfColumns) or (col == numOfColumns - 1 and row >= numOfRows - numOfShadedBoxes):
  76.             col = 0
  77.             row += 1
  78.  
  79.     return ''.join(plaintext)
  80.  
  81. # If transpositionEncrypt.py is run (instead of imported as a module) call
  82. # the main() function.
  83. if __name__ == '__main__':
  84.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement