Guest User

Caesar Cipher 4 reddit

a guest
Mar 7th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. '''This function takes a message string, and encrypts it based on a
  2. Ceaser Shift to the nth position'''
  3.  
  4. def rot(msg,n):`
  5.  
  6.     #Set the alphabet, include spaces
  7.     alphabet = "abcdefghijklmnopqrstuvwxyz "
  8.  
  9.     #Create a key - Here, I am splitting the original string at the nth index,
  10.     #then tacking on the rest of it to the end
  11.     rotation=alphabet[n:]+alphabet[:n]
  12.  
  13.     #Initialize your cipertext variable as blank string
  14.     newstring=''
  15.  
  16.     #Iterate through message string, character by character
  17.     for i in msg:
  18.  
  19.         #If the character is a space, we will simply add a space instead of shifting
  20.         if i == ' ':
  21.             newstring+=i
  22.  
  23.         #If the character is not a space, take the
  24.         #character's index in the alphabet, and add its correspondent to the cipher string
  25.         else:
  26.             newstring+=rotation[alphabet.index(i)]
  27.    
  28.     return newstring
  29.  
  30. #Testing...
  31. def testfunctions():
  32.     msg= input('Message: ')
  33.  
  34.     #Needs to take an integer value.
  35.     #Input makes it a string, so need int() to convert
  36.     n = int(input('Rotation: '))
  37.     print('Rot',n,': ',rot(msg,n))
Add Comment
Please, Sign In to add comment