Last active
December 18, 2015 07:29
-
-
Save gen1us2k/5747099 to your computer and use it in GitHub Desktop.
Caesar cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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