Nov-21-2019, 07:27 AM
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
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()