Skip to content

Instantly share code, notes, and snippets.

@hyhilman
Last active September 11, 2017 16:43
Show Gist options
  • Save hyhilman/b6deb0d3852d505dbd61b8f0e7712087 to your computer and use it in GitHub Desktop.
Save hyhilman/b6deb0d3852d505dbd61b8f0e7712087 to your computer and use it in GitHub Desktop.
Caisar Cipher Python
from CaisarCipher import CaisarCipher
print CaisarCipher('landika').encode(1)
print CaisarCipher(CaisarCipher('landika').encode(1)).decode(1)
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