Welcome back to the second installment of my Python Cipher Series! My Python learning continues with the Atbash cipher and writing another basic encryption scripts. The script in this post is intended for beginners who want to learn / want to see a different way of doing things. I tried to optimize it as much as possible, but I’m certainly not the best. My main goal for this exercise is to make reusable functions that can be recycled for future scripts. If you have any questions or comments, please feel free to let me know down below! Now let’s jump into it!
Atbash Cipher
The Atbash cipher is a substitution cipher that has a specific key where each letter is flipped to it’s opposite value: A to Z becomes Z to A. For efficiency sake, we are going be working with ASCII values like we did in the last cipher. Making a list of each letter with the corresponding value is an option, but it would be less efficient than a computer doing a simple calculation. For the sake of simplicity, I will only be allowing alphabetic values. All numbers and special characters will be stripped from the initial string.
Steps of the encryption and decryption algorithm
- Format the input string to lowercase and strip spaces
- Split the said string into individual characters
- Convert each letter to its numerical value
- Flip the numerical value
- Convert flipped value to a letter
- Return the flipped value
Algorithm
The algorithm is very simple for this cipher, and we will use the same functions encryption and decryption.
Encryption and Decryption: Flipped Letter = |(Input ASCII -96) -27| + 96 = Convert ASCII
def flipChar(char): # Convert character to numeric 1 - 26 and flip value flip = abs((ord(char) - 96) - 27) # Convert back to letter if within range, else remove character return chr(flip + 96) if flip > 0 and flip <= 26 else ""
If that seems a bit confusing, have no fear! Start by taking the ASCII value of the character and subtract 96. Then you subtract 27 from the value of the previous step, and find the absolute value of it.
EX. Start with the letter “a”. ASCII “a” = 97. 97 – 96 = 1. 1 – 27 = -26. | -26 | = 26. 26 + 96 = 122. ASCII 122 = “z”
Full Code for the Atbash Cipher
import os # Only used to clear the command prompt def Atbash_crypt(cistring): # Atbash_crypt splits string to characters, converts to numeric, then flips value string = "" # Placeholder variable cistring = formatString(cistring) for x in range(0, len(cistring)): # Loop through each character of string string += flipChar(cistring[x]) # Run function to flip character return(string) # Return cipher string def formatString (string): fmtString = string.lower() # Format to Lowercase fmtString = "".join(fmtString.split()) # Remove spaces from string return fmtString def flipChar(char): # Convert character to numeric 1 - 26 and flip value flip = abs((ord(char) - 96) - 27) # Convert back to letter if within range, else remove character return chr(flip + 96) if flip > 0 and flip <= 26 else "" def Atbash(): os.system('cls') print("Atbash Cipher") print("-------------------------------") cistring = input("Please enter a text string below. All numbers will be stripped.\n") print("\nThe starting string is:") print (cistring,"\n") print("The Atbash encrypted string is:") print(Atbash_crypt(cistring),"\n") print("The Atbash decrypted string is:") print(Atbash_crypt(Atbash_crypt(cistring)),"\n") input("Press Enter to continue...") print(Atbash())
The full code can also be found on my GitHub here: https://github.com/michael-g-tgtm/tgtm-scripts/blob/master/python/ciphers/Atbash.py
Code Output for Atbash Cipher
Atbash Cipher
-------------------------------
Please enter a text string below. All numbers will be stripped.
Let's test this cipher!
The starting string is:
Let's test this cipher!
The A1Z26 encrypted string is:
ovghgvhggsrhxrksvi
The A1Z26 decrypted string is:
letstestthiscipher
Press Enter to continue...