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.
Vigenere Cipher
Cryptography is the science of hiding secrets. A typical cryptographic protocol takes some plaintext message and some key, and produces a ciphertext that is unreadable by anyone that does not know the key.
We observed a very simplistic encryption scheme: Caesar cipher. This encryption scheme has been in use since Roman times, and is very easily breakable. As such, we likely want a stronger encryption scheme to make our messages even more secure.
The key in caesar is 3. To encrypt, you chose the letter 3 spaces further in the alphabet of the entered character. In Vigenere, the key is a word. For each character in the plaintext, you use the corresponding letter in the key to determine how much to shift that character by.
Consider the plaintext "hello", with a key of "hi". This means to encrypt the "h" in hello, you use the letter "h" as the shift (Which is a shift of 7, since h is the 7th character of the alphabet). Then, for the letter "e" in hello, you would use the letter "i" as the shift (which is a shift of 8). But, now we have run out of letters in the key. In this case, you wrap back around to the beginning of the key string, using "h" as the key for the first "l" in hello.
Details
Create the function encode_vigenere(plaintext, key)
that encodes text using the vigenere cipher. Both
parameters plaintext and key are strings of
lowercase characters. The function should return a string of
lowercase characters, which is equivalent to the plaintext with
each character shifted according to the key.
Test Cases
print('Input: "hello", "hi"\t\tActual: ', encode_vigenere("hello", "hi"), '\t\tExpected: omstv') print('Input: "goodbye", "hello"\tActual: ', encode_vigenere("goodbye", "hello"), '\tExpected: nszopfi')
Hint
- The actual encryption of this will be very similar to the caesar cipher, with a change to the shift that occurs.
-
In order to account for the wrapping around of the key, use
the
%
operator. You will likely want to mod by the length of your key.
Challenge
The vigenere cipher is possibly the strongest classical cipher
you will encounter. Which means that writing a decryption
function is of the utmost importance. Write the function
decode_vigenere(ciphertext, key)
that decrypts the
ciphertext with the associated key.