Last active
September 11, 2017 16:43
-
-
Save hyhilman/b6deb0d3852d505dbd61b8f0e7712087 to your computer and use it in GitHub Desktop.
Caisar Cipher Python
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
from CaisarCipher import CaisarCipher | |
print CaisarCipher('landika').encode(1) | |
print CaisarCipher(CaisarCipher('landika').encode(1)).decode(1) |
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
class CaisarCipher: | |
def __init__(self, plain): | |
self.plain = plain | |
def encode(self, key): | |
cipher = '' | |
for c in self.plain: | |
decimalstring = self.hexToDecimal(self.charToHex (c)) | |
decimalstring = ( decimalstring + key ) % 256 | |
cipher = cipher + self.hexToChar(self.decimalToHex(decimalstring)) | |
return cipher | |
def decode(self, key): | |
self.cipher = self.plain | |
plain = '' | |
for c in self.cipher: | |
decimalstring = self.hexToDecimal(self.charToHex (c)) | |
decimalstring = (decimalstring - key) % 256 | |
plain = plain + self.hexToChar(self.decimalToHex(decimalstring)) | |
return plain | |
def hexToDecimal(self, p1): | |
return int(p1, 16) | |
def charToHex(self, p1): | |
return p1.encode('hex') | |
def decimalToHex(self, p1): | |
return hex(p1).split('x')[-1] | |
def hexToChar(self, p1): | |
return p1.decode('hex') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment