Advertisement
Guest User

transposition_encrypt.py

a guest
Nov 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def main():
  2.     myMessage = "Common sense is not so common."
  3.     myKey = 8
  4.  
  5.     ciphertext = encryptMessage(myKey, myMessage)
  6.  
  7.     print('"' + ciphertext + '"')
  8.  
  9.  
  10. def encryptMessage(key, message):
  11.     # Each string in ciphertext represents a column in the grid.
  12.     ciphertext = [''] * key
  13.  
  14.     # Loop through each column in ciphertext.
  15.     for col in range(key):
  16.         pointer = col
  17.  
  18.         # Keep looping until pointer goes past the length of the message.
  19.         while pointer < len(message):
  20.             # Place the character at pointer in message at the end of the
  21.             # current column in the ciphertext list.
  22.             ciphertext[col] += message[pointer]
  23.  
  24.             # move pointer over
  25.             pointer += key
  26.  
  27.     # Convert the ciphertext list into a single string value and return it.
  28.     return ''.join(ciphertext)
  29.  
  30.  
  31. # If transpositionEncrypt.py is run (instead of imported as a module) call
  32. # the main() function.
  33. if __name__ == '__main__':
  34.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement