Atbash Cipher as an Algorhithm

Atbash Cipher as an Algorhithm

Today I realized Atbash can done as an algorithm, rather than looking up a hard-coded dictionary of an alphabet to its reversed alphabet.  You could encrypt using the alphabet’s array index as: -i +25

It’s that simple.

Example:  

cipherCharacter = alphabet[ -alphabet.index(i) + 25 ] 

Assuming you have a list of an alphabet, index(i) will get the plaintext chacter’s position.  Then negate it and add 25, and now you have the index position of the Atbash character.  Slick!

Complete Code:

import string

text = "No more rhymes now I mean it"

print(text)

alphabet = list(string.ascii_uppercase)

result = ''
for i in list(text.upper()):
    if i in alphabet:
       result += alphabet[ -alphabet.index(i) + 25 ]
    else:
       result += i

print(result)

Output:

No more rhymes now, I mean it.
ML NLIV ISBNVH MLD, R NVZM RG.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.