Skip to content

Instantly share code, notes, and snippets.

@gen1us2k
Last active December 18, 2015 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gen1us2k/5747099 to your computer and use it in GitHub Desktop.
Save gen1us2k/5747099 to your computer and use it in GitHub Desktop.
Caesar cipher
import string
class CesarCipher(object):
shift = None
alphabet = None
def setAlphabet(self, alphabet):
self.alphabet = alphabet
def setShift(self, shift):
# self.shift = shift % 26 # only 26 chars in alphabet
self.shift = shift
def encode(self, plainText):
shifted = self.alphabet[self.shift:] + self.alphabet[:self.shift]
table = string.maketrans(self.alphabet, shifted)
return plainText.translate(table)
if __name__ == '__main__':
caesar = CesarCipher()
caesar.setAlphabet("ABCD")
caesar.setShift(3)
encoded = caesar.encode("AACBDEAA")
print encoded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment