Skip to content

Instantly share code, notes, and snippets.

@kieransnapes
Forked from EdwinRozario/atbash.md
Last active June 20, 2018 22:33
Show Gist options
  • Save kieransnapes/6c63680853212f464fdc339f6ce70bfb to your computer and use it in GitHub Desktop.
Save kieransnapes/6c63680853212f464fdc339f6ce70bfb to your computer and use it in GitHub Desktop.

Atbash is a simple substitution cipher possible with any known alphabet. It emerged around 500-600 BCE. It works by substituting the letters of an alphabet with another.

Assumptions

CONSTANT = 'abcdefghijklmnopqrstuvwxyz'

A random shuffle of the constant is the cipher.

Example cipher: zyxwvutsrqponmlkjihgfedcba

Using the cipher, strings can be encrypted. When the cipher and encrypted string is given it is possible to decrypt the encrypted text.

To run: python atbash_test.py

This will run the atbash.py and print the decoded message

Requires python3

CONSTANT = 'abcdefghijklmnopqrstuvwxyz'
def buildDecoder(cipher):
#builds a dictionary to decrypt the code using the cipher - uses dictionary comprehension
dictionary = {cipher[i] : CONSTANT[i] for i in range(len(CONSTANT))}
return dictionary
def decrypt(text, cipher):
#loops over the string and matches each char with its true value
cipher_dict = buildDecoder(cipher)
decrypted_text = "";
for char in text:
if char in cipher_dict:
decrypted_text += cipher_dict[char]
else:
decrypted_text += char
print(decrypted_text)
return decrypted_text
import unittest
from atbash import buildDecoder, decrypt
class AtbashTest(unittest.TestCase):
def test_decrypt(self):
self.assertEqual("houston, we have a problem", decrypt("knlfgnb, sj koqj o yvnewju", "oephjizkxdawubnytvfglqsrcm"))
def test_numbers_and_special(self):
self.assertEqual("1337 why so serious?", decrypt("1337 skd qj qucbjvq?", "xipmuzfkbrlwotjancqgveshdy"))
def test_decrypt_short(self):
self.assertEqual("car", decrypt("dzs", "zodvqukgwefbyitmrsplhacxnj"))
def test_decrypt_fail(self):
self.assertNotEqual("why so unserious?", decrypt("sdf so serious","xipmuzfkbrlwotjancqgveshdy"))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment