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:
- Format string to lowercase and strip spaces between words
- Split the string into characters
- Convert each letter to its numerical value
- Return the ciphertext
Steps of the decryption algorithm:
- Split the string at each ” ” (space character)
- Convert each number to the corresponding letter
- Return decrypted text
Algorithms:
Encrypt: Number = Letter ASCII – 96 = 1 through 26
def A1Z26_encrypt(cistring):# Encrypt string by converting each letter to a numberstring = ""# Placeholder variablecistring = cistring.lower()# Format to Lowercasecistring = "".join(cistring.split())# Remove spaces from stringfor x in range(0, len(cistring)):# Loop through each character of stringchar = ord(cistring[x]) - 96 # Convert character to numeric 1 - 26if char > 0 and char <= 26 : string += str(char) + " "# Store value in 'string' variablereturn(string)# Return cipher string
Decrypt: Letter = Number + 96 = Letter ASCII
def A1Z26_decrypt(cistring):# Decrypt string by converting each number to a letterstring = ""# Placeholder variabledata = cistring.split()# Split string at " "for char in data:# Loop through each characterchar = chr(int(char) + 96)# Convert number to letterstring += char# Add character to stringreturn(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 promptdef A1Z26_encrypt(cistring):# Encrypt string by converting each letter to a numberstring = ""# Placeholder variablecistring = cistring.lower()# Format to Lowercasecistring = "".join(cistring.split())# Remove spaces from stringfor x in range(0, len(cistring)):# Loop through each character of stringchar = ord(cistring[x]) - 96 # Convert character to numeric 1 - 26if char > 0 and char <= 26 : string += str(char) + " "# Store value in 'string' variablereturn(string)# Return cipher stringdef A1Z26_decrypt(cistring):# Decrypt string by converting each number to a letterstring = ""# Placeholder variabledata = cistring.split()# Split string at " "for char in data:# Loop through each characterchar = chr(int(char) + 96)# Convert number to letterstring += char# Add character to stringreturn(string)# Return cipher stringdef A1Z26():# This code prompts the user for input and runs the other functionsos.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 cipherThe starting string is:
Let's test this cipherThe A1Z26 encrypted string is:
12 5 20 19 20 5 19 20 20 8 9 19 3 9 16 8 5 18The A1Z26 decrypted string is:
letstestthiscipherPress Enter to continue...
Post Views: 11,048
Related Posts
PowerShell Script Utility GUI
At my current place of business, a need arose for...
Michael GarrisonMar 27, 2019Feb 23, 2022
Python Cipher Series: Atbash Cipher
Welcome back to the second installment of my Python Cipher...
Michael GarrisonDec 14, 2017Feb 23, 2022
Leave a Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Author
Welcome to LabCrasher! The author, Michael Garrison, is a Systems Engineer based out of the New Jersey / New York area. He focuses on VMware Software Defined Datacenters (SDDC), Azure cloud architecture, PowerShell scripting, and various networking technologies.
Categories
- Coding
- How-To's
- Projects
- Security
- Uncategorized
Most Recent Posts
- Foundations: An Introduction to Active Directory
- vCenter Failed to Start File System Check on Disk (VMware KB2149838)
- Geo-Blocking: What is it, who to block, and how
- Configure SSH on Cisco IOS Devices
- Configure Static IP on a Raspberry Pi before booting