>> [MUSIC PLAYING]
>> ZAMYLA CHAN: Let's implement Vigenere, a slightly more
secure cipher than Caesar.
The plain text is enciphered using a string instead of an integer.
Each alphabetical character in plain text is shifted by a
letter in the keyword.
>> In this example, the keyword ohai, O corresponds to a shift of 14; H to a
shift of 7; A, shift of 0; and I a shift of 8.
If you've successfully implemented your Caesar cipher, it'll be a nice
framework from which you can implement Vigenere.
As you can see, running a Vigenere cipher with a single character as a
keyword is the same thing as a Caesar cipher.
>> The same steps apply to Vigenere as they did in Caesar.
The keyword is the second command line argument, so you access it with argv1.
Then you need to verify that the key word is indeed all alphabetical.
Here is where is alpha can come in handy.
If you have a valid keyword, you get the strength from the user, and then
you're ready to encipher.
>> The Vigenere cipher formula is similar to Caesar formula, except now k
becomes k subscript j, indicating the j-th letter of the keyword.
Let's step through this process.
Say you wanted to send a message to your crash, I like you, but you don't
want everyone to know.
So you use a Vigenere cipher with the keyword panda, because, well, you also
like pandas.
>> The first letter, I, will be shifted by p, giving x, 15 letters after I,
because 15 p is the 16th letter of the alphabet.
The next letter in the plain text is a space, so that won't be shifted.
And the index of the keyword won't change.
>> Then the next letter in plain text is l, shifted by a, which doesn't shift
the plain text letter at all, because a is the 0th letter of the alphabet.
The process continues, advancing the keyword character every time there's a
letter in the plain text.
Once the last letter in the keyword is reached, the keyword wraps around and
shifts to the next plain text letter by p.
X lvne noh.
How romantic.
>> So given a character, how do you convert that into the corresponding
cipher shift?
Try comparing the ASCII values to the shift.
Maybe you can find a relationship between the letters and their
alphabetical index using ASCII math.
Can you add or subtract one character from another to get
you the desired result?
>> Remember that the shifts for uppercase and lowercase letters are the same.
So perhaps you'll need to identify two similar formulas to represent the
shift, one for an uppercase keyword character, and one
for a lowercase one.
>> Next, remember that the keyword advances only if the character in
plain text is a letter and that the case of the plain
text must be preserved.
So if we look at the formula for the Vigenere shift, there are two index
variables, i and j.
One keeps track of the position in plain text, and the other the position
in the keyword.
But your plain text may be much longer than your keyword, in which case your
keyword index needs to wrap around back to the beginning of the keyword.
>> How do you do this?
Let's look back at the modulo operator.
Modulo is defined is the remainder of dividing two numbers.
But what's an actual practical use of modulo?
>> Well, say you have a large group of people, and you need to divide into
three groups.
One way to divide people into groups is to have them count off.
You number the groups group number 1, 2, and 3.
The first person will say 1, the next 2, the next 3.
The person after that will say 1, because there isn't a group 4, and the
count starts over from there.
>> You can use modulo to do the same thing.
This time, the groups will be group 0, 1, and 2.
The first person, number 1 modulo 3, is 1.
Person 2 modulo 3 is 2.
Person 3 modulo 3 is 0.
Person 4 modulo 3 gives 1, and so the groups can wrap around.
>> So if you take an index and modulo that index by a maximum size, the
result will never be greater than or equal to the size, meaning that you
can increase the index as much as you'd like.
And as long as you modulo the index by some number, you won't get a number
larger than that.
So we have 10 people instead of 5, and they would all get assigned to groups
number 0, 1, or 2.
>> Try to apply this to wrapping over the keyword, except instead of sorting
people into group numbers you want the index of the keyword so that you can
get the right character for the shift without exceeding the
length of the string.
With that, you have your Vigenere cipher.
My name is Zamyla, and this is CS50.