Caesar Cipher
Most of us like to keep secrets. For example, I don't want you to know my credit card number. I'd rather you not be able to buy things with my money. However, communication over the Internet is like trying to shout in a crowded room. Everyone can hear what you are saying. So, to protect our valuable secrets, we rely on encryption schemes. Today, you are going to implement a simplistic encryption scheme known since Roman times: Caesar cipher.
Encryption in the Caesar schema relies on shifting letters in the alphabet. By default, this shift is 3 letters. So, a becomes d, b becomes e, etc. The only issue with this is at the end of the alphabet. The end of the alphabet wraps around to the front. So, x becomes a, y becomes b, and z becomes c.
Details
Create the function encode_caesar(plaintext)
that
encodes text using the Caesar cipher. The
parameter plaintext is a string of lowercase
characters. The function should return a string of lowercase
characters, which is equivalent to the plaintext with each
character shifted by 3.
Test Cases
print('Input: "hello"\tActual: ', encode_caesar("hello"), '\tExpected: khoor') print('Input: "goodbye"\tActual: ', encode_caesar("goodbye"), '\tExpected: jrrgebh')
Hint
-
The
ord
function gives you the numeric representation of a character. Subtract theord('a')
from this, to get the position in alphabet a character is. -
Use addition to shift the numeric representation of the
character and use the
chr
function to convert the numeric value back to a character. -
The numeric representation may be too large after the shift.
You can use the
%
operator to wrap the number around to the beginning.
Challenge
Encryption for Caesar is useful, but in order to talk to anyone
they need to be able to decrypt the Caesar encryption. Write
the function decode_caesar(ciphertext)
that
decrypts the ciphertext.
Substitution Cipher
Another common technique for encryption is known as the Substitution Cipher. Instead of defining a linear equation between the plaintext and ciphertext, you can define a map that translates characters using a specified order. Crypograms use this technique, for example.
The key in the substitution cipher is a string of characters in
the range [a,z]. For example, the
string "abcdefghijklmnopqrstuvwxyz"
is one possible
key in the substitution cipher. It maps the letter 'a' to the
letter 'a', the letter 'b' to 'b', and so on. A more
interesting example would be "zyxwvutsrqponmlkjihgfedcba", which
maps 'a' to 'z', 'b' to 'y', and so on. For example, "hello
world" would become "svool dliow" under this key. Notice that
characters that are not alphabetic are not encoded, and their
normal value is included.
Details
Create the function encode_substitution(plaintext,
key)
that encodes text using the substitution cipher.
The parameter plaintext is a string of lowercase
characters and the parameter key is a string that
contains all of the letters of the alphabet, in lowercase, in
any order. The funciton should return a string of lowercase
characters, which is equivalent to the plaintext with each
character substituted with its corresponding letter.
Test Cases
print('Input: "hello", "zyxwvutsrqponmlkjihgfedcba"\tActual: ', encode_substitution("hello", "zyxwvutsrqponmlkjihgfedcba"), '\t\tExpected: svool') print('Input: "goodbye", "acegikmoqsuwybdfhjlnprtvxz"\tActual: ', encode_substitution("goodbye", "acegikmoqsuwybdfhjlnprtvxz"), '\tExpected: mddgcxi')
Hint
-
Given a character, compute its position in the alphabet by using the
index
method onstring.ascii_lowercase
. This returns an integer, the position the character exists in the string. You will use this as your index into the key string.
Challenge
Write the function decode_substitution(ciphertext,
key)
that decrypts the ciphertext with the associated
key.