Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cipher Caesar
#1
A cipher is a type of secret code, where you swap the letters around so that no-one can read your message.

You’ll be using one of the oldest and most famous ciphers, the Caesar cipher, which is named after Julius Caesar.

How does it work?

Let’s start by encrypting the letter ‘a’. To do this, we can draw the alphabet in a circle, like this:



To make a secret encrypted letter from a normal one, you need to have a secret key. Let’s use the number 3 as the key (but you can use any number you like).

To encrypt the letter ‘a’, you just move 3 letters clockwise, which will give you the letter ‘d’:

circle2-1.jpg

You can use what you’ve learnt to encrypt an entire word. For example, ‘hello’ encrypted is ‘khoor’. Try it yourself.
h + 3 = k
e + 3 = h
l + 3 = o
l + 3 = o
o + 3 = r

Getting text back to normal is called decryption. To decrypt a word, just subtract the key instead of adding it:
k - 3 = h
h - 3 = e
o - 3 = l
o - 3 = l
r - 3 = o

Finally, you should have following interaction at the end:

Example 1:

Please enter a message: Arman
Do you want to encrypt or decrypt (e or d)?: e
Enter a key (1-26): 3
Your new message is: Aupdq

Example 2:

Please enter a message: Aupdq
Do you want to encrypt or decrypt (e or d)?: d
Enter a key (1-26): 3
Your new message is: Arman

Example 3:

Please enter a message: Aupdq
Do you want to encrypt or decrypt (e or d)?: 34535fgdfgdgf
Sorry, I didn't get it, do you want to encrypt or decrypt (e or d)?: d
Enter a key (1-26): 3
Your new message is: Arman


key = 3
 
def wub():
    def choice():
        choice = input("Do you wish to Encrypt of Decrypt?")
        choice = choice.lower()
        if choice == "e" or "encrypt":
            return choice
        elif choice == "d" or "decrypt":
            return choice
        else:
            print("Invalid response, please try again.")
            choice()
 
    def message():
        user = input("Enter your message: ")
        return user
 
    def waffle(choice, message, key):
        translated = ""
        if choice == "e" or "encrypt":
            for character in message:
                num = ord(character)
                num += key
                translated += chr(num)
 
                derek = open('Encrypted.txt', 'w')
                derek.write(translated)
            derek.close()
            return translated
        else:
            for character in message:
                num = ord(character)
                num -= key
                translated += chr(num)
            return translated
 
    choice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
    message = message() #Run function for user to enter message. Saves message.
    final = waffle(choice, message, key) #Runs function to translate message, using the choice, message and key variables)
    print("\n Operation complete~    print(final)
 
wub()
Reply
#2
Did you have a question?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No idea how to use the Caesar Cypher in my code celtickodiak 5 3,082 Oct-08-2019, 03:29 AM
Last Post: stullis
  Monoalphabetic cipher pawlo392 1 12,794 Apr-01-2019, 08:51 PM
Last Post: ichabod801
  Caesar cipher Drone4four 19 10,732 Nov-11-2018, 04:07 AM
Last Post: nilamo
  Vigenere and Caesar Cipher sammy2938 1 5,746 Jul-29-2017, 01:32 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020