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 thoughts on “Caesar Cipher (Python)

Comments are closed.