Question: Part 2: Vigenere If youre trying to pass notes in the back of class with your best friend Suzie, the Caesar cipher would be fairly

Part 2: Vigenere

If youre trying to pass notes in the back of class with your best friend Suzie, the Caesar cipher would be fairly easy for a nosy outsider to decode. Lets implement a more complicated cipher algorithm.

https://www.youtube.com/watch?v=9zASwVoshiM&feature=youtu.be

Vigenere uses a word as the encryption key, rather than an integer. Your finished program will behave like this:

$ python vigenere.py Type a message: The crow flies at midnight! Encryption key: boom Uvs osck rmwse bh auebwsih! 

Above, the user entered a message of The crow flies at midnight! and an encryption key of boom, and the program printed Uvs osck rmwse bh auebwsih!.

How did we arrive at that result? Here is a detailed breakdown:

char from input string char from key rotation amount result char
T b 1 U
h o 14 v
e o 14 s
(space) n/a n/a (space)
c m 12 o
r b 1 s
o o 14 c
w o 14 k
(and so on )

Note

As with Caesar, the upper or lower case of each character should be preserved, and non-alphabetical characters like " " and "!" should not get encrypted.

Note

When we encounter a non-alphabetical character in the message, the encryption key does not use up another letter. For example, notice how the "m" in "boom" does not get wasted, so to speak, on the space character. Instead, it is saved for the next alphabetical character, the "c" in "crow".

Note

Whenever we reach the end of the encryption key (boom) before reaching the end of the message, the encryption key wraps back around to the beginning again (the letter b).

Note

You may assume that the encryption key (boom) will not contain any numbers or special characters. Letters only.

write a function alphabet_position(letter), which receives a letter (that is, a string with only one alphabetic character) and returns the 0-based numerical position of that letter within the alphabet.

Here are some example input parameter values, with the corresponding return values.

letter Return value
a 0
A 0
b 1
y 24
z 25
Z 25

Note

The function should be case-insensitive.

Note

You can assume that your input will definitely be a letter. Dont worry about what might happen if somebody tries to use your function with an input parameter that is something other than a single letter, like alphabet_position("grandpa22!")

Next, write another helper function rotate_character(char, rot) which receives a character char(that is, a string of length 1), and an integer rot. Your function should return a new string of length 1, the result of rotating char by rot number of places to the right.

Here are some example input values, with the corresponding return values.

char rot Return value
a 13 n
a 14 o
a 0 a
c 26 c
c 27 d
A 13 N
z 1 a
Z 2 B
z 37 k
! 37 !
6 13 6

Note

The upper or lower case of the letter should be preserved. For example, rotate_character("A", 13) results in "N", rather than "n"

Note

For non-alphabetical characters, you should ignore the rot argument and simply return charuntouched. For example, see "!" and "6" in the table above.

ENCRYPT

Now that you have your helper functions, go ahead and write a new version of the encrypt function which uses the Vigenere cipher rather than Caesar.

Your first step is to figure out what the function signature should say. How should it be different from the Caesar version, def encrypt(text, rot)?

As usual, dont move on until you have tested your function against a lot of different inputs and seen the results you expect.

Make It Interactive

Finally, add the appropriate print and input statements inside a main function so that your program behaves as specified:

$ python vigenere.py Type a message: The crow flies at midnight! Encryption key: boom Uvs osck rmwse bh auebwsih! 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!