Skip to content

Instantly share code, notes, and snippets.

@Barakat
Last active September 3, 2015 03:44
Show Gist options
  • Save Barakat/43b641bff3b27c9e0af3 to your computer and use it in GitHub Desktop.
Save Barakat/43b641bff3b27c9e0af3 to your computer and use it in GitHub Desktop.
Caesar cipher implementation in Python
def caesar_enc(plain, key):
plain = plain.encode('utf-8')
cipher = bytearray(plain)
for i, c in enumerate(plain):
cipher[i] = (c + key) & 0xff
return bytes(cipher)
def caesar_dec(cipher, key):
plain = bytearray(len(cipher)) # at most, len(plain) <= len(cipher)
for i, c in enumerate(cipher):
plain[i] = (c - key) & 0xff
return plain.decode('utf-8')
message = 'السلام عليكم'
key = 1436
assert caesar_dec(caesar_enc(message, key), key) == message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment