Python Cipher Series: A1Z26 Cipher

Welcome to the first installment of my Python Cipher Series!  My goal for this series is to track my progress as I learn Python.  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.  If you have any questions or comments, please feel free to let me know down below!  Now let’s jump into it!

A1Z26 Cipher

The first cipher is the most basic, the A1Z26 cipher.  All you have to do is convert each letter into it’s numeric value, meaning A=1, B=2, C=3, … Z=26.  There are several ways to accomplish this, but the most efficient way I could think of is to convert the letter to it’s ASCII value and subtract 96 to get the desired value.  For the sake of simplicity, I will only be allowing alphabetic values.  All numbers will be stripped from the initial string.

Steps of the encryption algorithm:

  1. Format string to lowercase and strip spaces between words
  2. Split the string into characters
  3. Convert each letter to its numerical value
  4. Return the ciphertext

Steps of the decryption algorithm:

  1. Split the string at each ” ” (space character)
  2. Convert each number to the corresponding letter
  3. Return decrypted text

Algorithms:

Encrypt: Number = Letter ASCII – 96 = 1 through 26

def A1Z26_encrypt(cistring):
# Encrypt string by converting each letter to a number
	string = ""		# Placeholder variable
	cistring = cistring.lower()		# Format to Lowercase
	cistring = "".join(cistring.split())	# Remove spaces from string
	for x in range(0, len(cistring)):	# Loop through each character of string
		char = ord(cistring[x]) - 96    # Convert character to numeric 1 - 26
		if char > 0 and char <= 26 : string += str(char) + " "	# Store value in 'string' variable
	return(string)		# Return cipher string


Decrypt:
Letter = Number + 96 = Letter ASCII

def A1Z26_decrypt(cistring):
# Decrypt string by converting each number to a letter
	string = ""		# Placeholder variable
	data = cistring.split()	# Split string at " "

	for char in data:	# Loop through each character
		char = chr(int(char) + 96)	# Convert number to letter
		string += char	# Add character to string
	return(string)		# Return cipher string

The reason I went with ASCII values is because there are built functions to convert letters into their ASCII value.  Alternatively, I could have created an array with the corresponding number value for each letter, but that would be extremely inefficient.  By simply subtracting 96 from the ASCII value, we get an integer between 1 and 26, which is the desired outcome.  Please note that if it detects that the ASCII value is outside the expected range (a – z) it will strip the character.

Full Code for the A1Z26 Cipher:

import os   # Only used to clear command prompt

def A1Z26_encrypt(cistring):
# Encrypt string by converting each letter to a number
	string = ""		# Placeholder variable
	cistring = cistring.lower()		# Format to Lowercase
	cistring = "".join(cistring.split())	# Remove spaces from string
	for x in range(0, len(cistring)):	# Loop through each character of string
		char = ord(cistring[x]) - 96    # Convert character to numeric 1 - 26
		if char > 0 and char <= 26 : string += str(char) + " "	# Store value in 'string' variable
	return(string)		# Return cipher string

def A1Z26_decrypt(cistring):
# Decrypt string by converting each number to a letter
	string = ""		# Placeholder variable
	data = cistring.split()	# Split string at " "

	for char in data:	# Loop through each character
		char = chr(int(char) + 96)	# Convert number to letter
		string += char	# Add character to string
	return(string)		# Return cipher string

def A1Z26():
# This code prompts the user for input and runs the other functions
	os.system('cls')
	print("A1Z26 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 A1Z26 encrypted string is:")
	print(A1Z26_encrypt(cistring),"\n")
	print("The A1Z26 decrypted string is:")
	print(A1Z26_decrypt(A1Z26_encrypt(cistring)),"\n")
	input("Press Enter to continue...")

A1Z26()

The full code can also be found on my GitHub here: https://github.com/michael-g-tgtm/tgtm-scripts/blob/master/python/ciphers/A1Z26.py

Code Output:

A1Z26 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:
12 5 20 19 20 5 19 20 20 8 9 19 3 9 16 8 5 18 The A1Z26 decrypted string is:
letstestthiscipher

Press Enter to continue...

Leave a Reply

Your email address will not be published. Required fields are marked *

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