Advertisement
mrScarlett

Caesar Cipher Python

Dec 7th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. for x in range (2):
  2. # the string to be encrypted/decrypted
  3.     message =input( 'Enter a message')
  4.     translated = ''
  5.     # the encryption/decryption key
  6.     key = int(input("Enter Key"))
  7.  
  8.     # tells the program to encrypt or decrypt
  9.     mode = input('encrypt or decrypt') .lower()# set to 'encrypt' or 'decrypt'
  10.  
  11.     # every possible symbol that can be encrypted
  12.     LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  13.  
  14.     # capitalize the string in message
  15.     message = message.upper()
  16.  
  17.     # run the encryption/decryption code on each symbol in the message string
  18.     for symbol in message:
  19.         if symbol in LETTERS:
  20.             # get the encrypted (or decrypted) number for this symbol
  21.             num = LETTERS.find(symbol) # get the number of the symbol
  22.             if mode == 'encrypt':
  23.                 num = num + key
  24.             elif mode == 'decrypt':
  25.                 num = num - key
  26.                
  27.             # add encrypted/decrypted number's symbol at the end of translated
  28.             translated = translated + LETTERS[num]
  29.  
  30.         else:
  31.             # just add the symbol without encrypting/decrypting
  32.             translated = translated + symbol
  33.  
  34.     # print the encrypted/decrypted string to the screen
  35.     print(translated)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement