1 00:00:00,000 --> 00:00:11,980 2 00:00:11,980 --> 00:00:16,980 >> ROB: Hi, I'm Rob, and let's decipher the Vigenere program. 3 00:00:16,980 --> 00:00:21,180 So first thing we need to do is make sure the user entered what we expected 4 00:00:21,180 --> 00:00:23,240 them to at the Command line. 5 00:00:23,240 --> 00:00:28,720 So if argc is not 2 that means either the user did not enter the string we 6 00:00:28,720 --> 00:00:31,780 want to be using as our encrypting string, or they 7 00:00:31,780 --> 00:00:32,890 entered too many things. 8 00:00:32,890 --> 00:00:35,130 And we don't know what to do with those other things. 9 00:00:35,130 --> 00:00:37,960 >> So we tell them what they should have entered. 10 00:00:37,960 --> 00:00:39,300 And we return. 11 00:00:39,300 --> 00:00:44,570 Now, assuming that argc was 2, we can continue with the rest of the program. 12 00:00:44,570 --> 00:00:47,890 >> We alias the name of argv[1] 13 00:00:47,890 --> 00:00:49,750 into the variable keyword. 14 00:00:49,750 --> 00:00:51,860 So that we don't have to use the name argv[1] 15 00:00:51,860 --> 00:00:53,050 throughout the rest of the program. 16 00:00:53,050 --> 00:00:55,570 And maybe we'll forget what that means and so on. 17 00:00:55,570 --> 00:00:57,830 Keyword is a much nicer name. 18 00:00:57,830 --> 00:01:01,982 And we'll immediately grab the length of our keyword right here. 19 00:01:01,982 --> 00:01:07,460 >> OK, so now we want to check that our keyword is actually valid. 20 00:01:07,460 --> 00:01:11,250 The keyword we use to encrypt strings should just be alphabetical 21 00:01:11,250 --> 00:01:12,400 characters. 22 00:01:12,400 --> 00:01:16,830 If the user entered non-alphabetical characters, we should say, keyword 23 00:01:16,830 --> 00:01:20,170 must only contain A through Z and then return. 24 00:01:20,170 --> 00:01:24,370 So this for loop iterates over all characters of our keyword, checking 25 00:01:24,370 --> 00:01:31,870 that if one is not alphabetical then we need to print that warning. 26 00:01:31,870 --> 00:01:36,285 >> Now, once we get to this point, we know that the string must be correct. 27 00:01:36,285 --> 00:01:38,230 The keyword must be correct. 28 00:01:38,230 --> 00:01:40,880 And now we need to get the message from the user that they want us to 29 00:01:40,880 --> 00:01:43,910 encrypt with that key phrase. 30 00:01:43,910 --> 00:01:46,780 So to get that message, we have a do while loop that's going to 31 00:01:46,780 --> 00:01:52,650 continuously get a string from the user until they enter a valid string. 32 00:01:52,650 --> 00:01:58,690 >> Continuing, we see here this variable, int nun_letters_seen. 33 00:01:58,690 --> 00:02:01,300 We'll see why we need that in a second. 34 00:02:01,300 --> 00:02:07,320 But this for loop is going to iterate from i equals 0 all the way up to i 35 00:02:07,320 --> 00:02:10,940 equals n, which means we're iterating over all possible 36 00:02:10,940 --> 00:02:13,020 characters in our message. 37 00:02:13,020 --> 00:02:17,370 Because we want to encrypt all of the characters in our message. 38 00:02:17,370 --> 00:02:22,970 So notice we do if (isalphamessage[I], because we do not want to encrypt 39 00:02:22,970 --> 00:02:25,660 characters that are not alphabetical. 40 00:02:25,660 --> 00:02:28,810 If there are symbols, spaces, or numbers, we don't 41 00:02:28,810 --> 00:02:30,730 want to encrypt those. 42 00:02:30,730 --> 00:02:37,220 >> Now, assuming that it is alphabetical, we first want to figure out what we 43 00:02:37,220 --> 00:02:40,890 actually want to encrypt the message using. 44 00:02:40,890 --> 00:02:42,710 So what do I mean by that? 45 00:02:42,710 --> 00:02:46,740 >> Let's assume that the key phrase the user entered was abc. 46 00:02:46,740 --> 00:02:49,070 That's what we're using to encrypt. 47 00:02:49,070 --> 00:02:54,850 Now, naively, we think that means that we want to encrypt the first character 48 00:02:54,850 --> 00:02:59,740 of our message by 0, since a means rotating the character by 0. 49 00:02:59,740 --> 00:03:04,395 >> We want to encrypt the second character by 1, third character by 2, 50 00:03:04,395 --> 00:03:09,170 the fourth character by 0, the fifth by 1, the sixth by 2, and so on. 51 00:03:09,170 --> 00:03:14,440 But remember, that we want to skip spaces and symbols and numbers. 52 00:03:14,440 --> 00:03:21,520 This means that if the user had entered hello world as the message 53 00:03:21,520 --> 00:03:26,590 that they want to encrypt, then we want to encrypt the h by 0 54 00:03:26,590 --> 00:03:32,680 corresponding to the a, the e by 1, the l by 2, the l by 0, the o by 1. 55 00:03:32,680 --> 00:03:41,050 We want to skip the space, encrypted the w by 2, the o by 0, 1, 2, 0. 56 00:03:41,050 --> 00:03:45,250 So notice, if we hadn't skipped the space, then we would have encrypted 57 00:03:45,250 --> 00:03:51,240 the w by 0 and ended up with the incorrect string. 58 00:03:51,240 --> 00:03:57,470 >> OK, this is what we need the variable num_letters_seen for. 59 00:03:57,470 --> 00:04:04,450 If we were just going to encrypt using this method, which doesn't skip 60 00:04:04,450 --> 00:04:09,860 symbols, spaces, and numbers, then we could just use the variable i as what 61 00:04:09,860 --> 00:04:12,540 to index into our key phrase with. 62 00:04:12,540 --> 00:04:17,620 We need to use num_letters_seen to keep track of the actual place in the 63 00:04:17,620 --> 00:04:21,146 key phrase that we want to index. 64 00:04:21,146 --> 00:04:32,240 So here, if the keyword we have, if num_letter_seen mod keyword_length, so 65 00:04:32,240 --> 00:04:34,570 why do we need to mod by keyword length? 66 00:04:34,570 --> 00:04:36,630 >> Well, hello world was a good example. 67 00:04:36,630 --> 00:04:42,310 If the keyword was abc, then we need to continuously encrypt by a then b 68 00:04:42,310 --> 00:04:45,740 then c, then wrap back around, a, b, c, a, b, c. 69 00:04:45,740 --> 00:04:50,110 So we need to mod by keyword length in order to wrap back around. 70 00:04:50,110 --> 00:04:57,280 >> So if this is an uppercase letter, then we want to encrypt by the 71 00:04:57,280 --> 00:05:01,450 position of that letter in the alphabet, which we get by just 72 00:05:01,450 --> 00:05:06,730 subtracting out capital A. And similarly, for lowercase letters, we 73 00:05:06,730 --> 00:05:13,000 can get the key that we want by subtracting out lowercase a. 74 00:05:13,000 --> 00:05:16,910 So regardless of whether the letter in the key phrase was a capital or 75 00:05:16,910 --> 00:05:21,640 lowercase letter, we're going to encrypt by the same amount. 76 00:05:21,640 --> 00:05:28,680 >> Now that we have our key, we see here, that if message i is an uppercase 77 00:05:28,680 --> 00:05:32,660 character, then we want to calculate the position in the alphabet of that 78 00:05:32,660 --> 00:05:39,460 character, add our key to it, wrap back around so that if we went past a 79 00:05:39,460 --> 00:05:43,170 z we go back to a, b, c, and so on. 80 00:05:43,170 --> 00:05:49,070 Then, finally, add back on capital A. So we shift back into the [? Ascii ?] 81 00:05:49,070 --> 00:05:52,010 range of these characters instead of the numeric position in the alphabet 82 00:05:52,010 --> 00:05:53,540 of these characters. 83 00:05:53,540 --> 00:05:56,610 >> And we do the same thing for lower case characters. 84 00:05:56,610 --> 00:06:00,070 Except we want to subtract out lowercase a and add it back on in the 85 00:06:00,070 --> 00:06:02,900 end, lowercase a. 86 00:06:02,900 --> 00:06:08,120 Notice that num_letter_seen is only incremented if message i was 87 00:06:08,120 --> 00:06:09,640 alphabetical. 88 00:06:09,640 --> 00:06:15,790 This is how we skip spaces, symbols, and numbers in our key phrase, since 89 00:06:15,790 --> 00:06:20,520 num_letter_seen is what we're using to index into our keyword. 90 00:06:20,520 --> 00:06:24,540 >> Finally, in the end, now that message i has been encrypted, we 91 00:06:24,540 --> 00:06:26,280 print out message i. 92 00:06:26,280 --> 00:06:27,890 And that's it. 93 00:06:27,890 --> 00:06:28,670 My name is Rob. 94 00:06:28,670 --> 00:06:31,020 And this is Vigenere. 95 00:06:31,020 --> 00:06:32,850 >> [MUSIC PLAYING] 96 00:06:32,850 --> 00:06:36,651