Skip to content

Instantly share code, notes, and snippets.

@douglasrizzo
Created May 25, 2018 17:16
Show Gist options
  • Save douglasrizzo/d277c4c0ddeacc164628797e2659102d to your computer and use it in GitHub Desktop.
Save douglasrizzo/d277c4c0ddeacc164628797e2659102d to your computer and use it in GitHub Desktop.
Caesar Cipher
def caesar(plainText, shift):
"""Caesar Cipher implementation.
Upper and lower case letters are kept accordingly, non-alphabetic characters are ignored.
Implementation taken from here: https://stackoverflow.com/q/8886947/1245214
:param plainText: the text to be ciphered
:param shift: shift value for the cipher
:return: the ciphered text
"""
cipherText = ""
for ch in plainText:
finalLetter = ch
if ch.isalpha():
finalLetter = chr((ord(ch.lower()) - 97 + shift) % 26 + 97)
if ch.isupper():
finalLetter = finalLetter.upper()
cipherText += finalLetter
return cipherText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment