Caesar Cipher (Python)

I’m having a play in Python today and looking at the Caesar Cipher.

The Caesar Cipher (Caesar Shift, Caesar’s Code) is a simple, easy to implement substitution cipher. The cipher is named after Julius Caesar. The cipher works by taking the letters of the message and then shifts the letter a defined space along the alphabet.

For example a shift of 1 character would make a=b, b=c, c=d etc.

I’m making a program with Python that can encrypt or decrypt a text message using the Caesar Cipher.

——

#Caesar Cipher

#GeekTechStuff

letters=”ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ”

encrypt=input(“Do you want to encrypt a message? (Y/N):”)

encrypt=encrypt.upper()

if encrypt == “Y”:

    stringtoencrypt=input(“Please enter A-Z characters to encrypt:”)

    stringtoencrypt=stringtoencrypt.upper()

    ciphershift=input(“Please enter a number between 1 and 25 to be your cipher key: “)

    ciphershift=int(ciphershift)

    stringencrypted=””

    for character in stringtoencrypt:

        position = letters.find(character)

        newposition = position+ciphershift

        if character in letters:

            stringencrypted = stringencrypted + letters[newposition]

        else:

            stringencrypted = stringencrypted + character

            

    ciphershift=str(ciphershift)

    print(“You used a cipher shift of “+ciphershift)

    print(“Your encrypted message reads:”)

    print(stringencrypted)

    

    

if encrypt == “N”:

    stringtodecrypt=input(“Please enter A-Z characters to dencrypt:”)

    stringtodecrypt=stringtodecrypt.upper()

    ciphershift=input(“Please enter a number between 1 and 25 to be your cipher key: “)

    ciphershift=int(ciphershift)

    stringdecrypted=””

    for character in stringtodecrypt:

        position = letters.find(character)

        newposition = position-ciphershift

        if character in letters:

            stringdecrypted = stringdecrypted + letters[newposition]

        else:

            stringdecrypted = stringdecrypted + character

            

    ciphershift=str(ciphershift)

    print(“You used a cipher shift of “+ciphershift)

    print(“Your decrypted message reads:”)

    print(stringdecrypted)

    

    

else:

    print(“Ending Caesar Cipher”)

3 responses to “Caesar Cipher (Python)”

  1. Caesar Cipher With GUI (Python) – Geek Tech Stuff Avatar

    […] have been learning more about Tkinter recently and decided to revisit the Caesar Cipher I did back at the beginning of the year to see if I could add a GUI (Graphical User Interface) to […]

    Like

  2. Caesar Cipher (Java) – Geek Tech Stuff Avatar

    […] of the earlier programs I wrote in Python was a Caesar Cipher (later updating it to add a GUI). As part of my recent Java learning I have created a Caesar Cipher […]

    Like

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect