JDpaste

Caesar Cipher - shift cipher

Sep 10th, 2021 (edited)
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. def caesarDecipher( cipherText = "" ):
  2.     """ Caesar cipher is sometimes called a shift cipher. Decipher given ciphertext."""
  3.     print( '>> ' + cipherText )     # DEBUG output
  4.     ordA = ord('A')                 # ord() gets codepoint for Unicode char
  5.     cipherText = cipherText.upper() # force UPPERCASE ascii
  6.     # assume UPPERCASE or <space ' '>
  7.     for k in range( 1, 26 ) :
  8.         plainTxt = ''
  9.         for token in cipherText :
  10.             if token == ' ':  # space chars pass thru ..
  11.                 plainChar = ' '
  12.             else:             # map char using shift ..
  13.                 plainOrd = ord( token ) - k
  14.                 if plainOrd < ordA : plainOrd += 26
  15.                
  16.                 plainChar = chr( plainOrd )
  17.             #
  18.             plainTxt += plainChar  # append each char to build 'plaintext'
  19.         #
  20.        
  21.         # DEBUG: show result for each key in 1..25
  22.         if k <= 9 :
  23.             key = ' ' + str(k) # space pad for single digit .. pretty print !
  24.         else:
  25.             key = str(k)
  26.         #
  27.         outTxt = key +' '+ plainTxt
  28.         print( outTxt )
  29.     #
  30. #--------------------
  31.  
  32. def caesarEncipher( plainText = "" ):
  33.     """ Caesar cipher is sometimes called a shift cipher. Encipher given plaintext."""
  34.     print( '>> ' + plainText )      # DEBUG output
  35.     ordZ = ord('Z')                 # ord() gets codepoint for Unicode char
  36.     plainText = plainText.upper()   # force UPPERCASE ascii
  37.     # assume UPPERCASE or <space ' '>
  38.     for k in range( 1, 26 ) :
  39.         cipherText = ''
  40.         for token in plainText :
  41.             if token == ' ':   # space chars pass thru ..
  42.                 cypherChar = ' '
  43.             else:              # map char using shift ..
  44.                 cypherOrd = ord( token ) + k
  45.                 if cypherOrd > ordZ : cypherOrd -= 26
  46.                
  47.                 cypherChar = chr( cypherOrd )
  48.             #
  49.             cipherText += cypherChar  # append each char to build 'cyphertext'
  50.         #
  51.        
  52.         # DEBUG: show result for each key in 1..25
  53.         if k <= 9 :
  54.             key = ' ' + str(k) # space pad for single digit .. pretty print !
  55.         else:
  56.             key = str(k)
  57.         #
  58.         outTxt = key +' '+ cipherText
  59.         print( outTxt )
  60.     #
  61. #-------------------------
  62. # TEST calls ..
  63. print( '\n*** DECIPHER ***\n' )
  64. caesarDecipher('PM FVB JHU NBLZZ AOL RLF PA PZ LHZF AV KLJYFWA AOL TLZZHNL')
  65.  
  66. print( '\n*** ENCIPHER ***\n' )
  67. caesarEncipher('IF YOU CAN GUESS THE KEY IT IS EASY TO DECRYPT THE MESSAGE')
  68.  
  69. print( '\n*** DECIPHER another ... ***\n' )
  70. caesarDecipher('WJYZWS YT WTRJ') # .. RETURN TO ROME
  71.  
  72.  
Add Comment
Please, Sign In to add comment