Python Cipher Series: A1Z26 Cipher - LabCrasher (2024)

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 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:

Python Cipher Series: A1Z26 Cipher - LabCrasher (1)

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...

Python Cipher Series: A1Z26 Cipher - LabCrasher (2)

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

Advertisem*nt

Python Cipher Series: A1Z26 Cipher - LabCrasher (2024)

References

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6305

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.