Last active
September 3, 2015 03:44
-
-
Save Barakat/43b641bff3b27c9e0af3 to your computer and use it in GitHub Desktop.
Caesar cipher implementation in Python
This file contains hidden or 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
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