>> ROB: Hi, I'm Rob, and let's decipher the Vigenere program.
So first thing we need to do is make sure the user entered what we expected
them to at the Command line.
So if argc is not 2 that means either the user did not enter the string we
want to be using as our encrypting string, or they
entered too many things.
And we don't know what to do with those other things.
>> So we tell them what they should have entered.
And we return.
Now, assuming that argc was 2, we can continue with the rest of the program.
>> We alias the name of argv[1]
into the variable keyword.
So that we don't have to use the name argv[1]
throughout the rest of the program.
And maybe we'll forget what that means and so on.
Keyword is a much nicer name.
And we'll immediately grab the length of our keyword right here.
>> OK, so now we want to check that our keyword is actually valid.
The keyword we use to encrypt strings should just be alphabetical
characters.
If the user entered non-alphabetical characters, we should say, keyword
must only contain A through Z and then return.
So this for loop iterates over all characters of our keyword, checking
that if one is not alphabetical then we need to print that warning.
>> Now, once we get to this point, we know that the string must be correct.
The keyword must be correct.
And now we need to get the message from the user that they want us to
encrypt with that key phrase.
So to get that message, we have a do while loop that's going to
continuously get a string from the user until they enter a valid string.
>> Continuing, we see here this variable, int nun_letters_seen.
We'll see why we need that in a second.
But this for loop is going to iterate from i equals 0 all the way up to i
equals n, which means we're iterating over all possible
characters in our message.
Because we want to encrypt all of the characters in our message.
So notice we do if (isalphamessage[I], because we do not want to encrypt
characters that are not alphabetical.
If there are symbols, spaces, or numbers, we don't
want to encrypt those.
>> Now, assuming that it is alphabetical, we first want to figure out what we
actually want to encrypt the message using.
So what do I mean by that?
>> Let's assume that the key phrase the user entered was abc.
That's what we're using to encrypt.
Now, naively, we think that means that we want to encrypt the first character
of our message by 0, since a means rotating the character by 0.
>> We want to encrypt the second character by 1, third character by 2,
the fourth character by 0, the fifth by 1, the sixth by 2, and so on.
But remember, that we want to skip spaces and symbols and numbers.
This means that if the user had entered hello world as the message
that they want to encrypt, then we want to encrypt the h by 0
corresponding to the a, the e by 1, the l by 2, the l by 0, the o by 1.
We want to skip the space, encrypted the w by 2, the o by 0, 1, 2, 0.
So notice, if we hadn't skipped the space, then we would have encrypted
the w by 0 and ended up with the incorrect string.
>> OK, this is what we need the variable num_letters_seen for.
If we were just going to encrypt using this method, which doesn't skip
symbols, spaces, and numbers, then we could just use the variable i as what
to index into our key phrase with.
We need to use num_letters_seen to keep track of the actual place in the
key phrase that we want to index.
So here, if the keyword we have, if num_letter_seen mod keyword_length, so
why do we need to mod by keyword length?
>> Well, hello world was a good example.
If the keyword was abc, then we need to continuously encrypt by a then b
then c, then wrap back around, a, b, c, a, b, c.
So we need to mod by keyword length in order to wrap back around.
>> So if this is an uppercase letter, then we want to encrypt by the
position of that letter in the alphabet, which we get by just
subtracting out capital A. And similarly, for lowercase letters, we
can get the key that we want by subtracting out lowercase a.
So regardless of whether the letter in the key phrase was a capital or
lowercase letter, we're going to encrypt by the same amount.
>> Now that we have our key, we see here, that if message i is an uppercase
character, then we want to calculate the position in the alphabet of that
character, add our key to it, wrap back around so that if we went past a
z we go back to a, b, c, and so on.
Then, finally, add back on capital A. So we shift back into the [? Ascii ?]
range of these characters instead of the numeric position in the alphabet
of these characters.
>> And we do the same thing for lower case characters.
Except we want to subtract out lowercase a and add it back on in the
end, lowercase a.
Notice that num_letter_seen is only incremented if message i was
alphabetical.
This is how we skip spaces, symbols, and numbers in our key phrase, since
num_letter_seen is what we're using to index into our keyword.
>> Finally, in the end, now that message i has been encrypted, we
print out message i.
And that's it.
My name is Rob.
And this is Vigenere.
>> [MUSIC PLAYING]