Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Created October 13, 2016 05:01
Show Gist options
  • Save CleverProgrammer/f8f6a7f9f91a499401239ce9a7386607 to your computer and use it in GitHub Desktop.
Save CleverProgrammer/f8f6a7f9f91a499401239ce9a7386607 to your computer and use it in GitHub Desktop.
Using the idea of caesar cipher, we create our own custom caesar cipher.
"""
Author: Rafeh Qazi
Program: Encryption and Decryption
Resource: http://www.practicalcryptography.com/ciphers/caesar-cipher/
"""
KEY = 17
def caesar_encryption(letter, key):
"""
Letter in the first 256 ascii is shifted 'key' positions.
:param letter: str
:param key: int
:return: int
"""
# mod 256 to create a world of [0,255] integers.
return (ord(letter) + key) % 256
def caesar_decryption(encrypted_letter, key):
"""
Used to reverse the destruction caused by caesar_encryption.
:param letter: int
:param key: int
:return: str
"""
return chr((encrypted_letter - key) % 256)
# decrypt(encrypt(x)) should give you back x.
assert caesar_decryption(caesar_encryption('e', 20), 20) == 'e'
encrypted_message = []
for letter in 'bananas':
encrypted_message.append(caesar_encryption(letter, KEY))
print(encrypted_message)
decrypted_message = []
for encrypted_letter in encrypted_message:
decrypted_message.append(caesar_decryption(encrypted_letter, KEY))
print(''.join(decrypted_message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment