1 00:00:05,530 --> 00:00:07,939 Hello! Welcome to the section 10 challenge. 2 00:00:08,349 --> 00:00:11,470 In this challenge, what I'm asking you to do is to create 3 00:00:11,470 --> 00:00:13,530 a substitution cipher program. 4 00:00:14,250 --> 00:00:17,139 This is really straightforward, but it'll give you some experience 5 00:00:17,160 --> 00:00:19,980 using the c++ string class. 6 00:00:20,290 --> 00:00:24,029 Now there's an easy way to do this and a not so easy way to do this. 7 00:00:24,050 --> 00:00:25,919 I'm not going to tell you what the easy way to do this is 8 00:00:25,950 --> 00:00:28,290 because I want you to learn and try to figure it out on your own. 9 00:00:29,020 --> 00:00:30,299 Let me talk about the challenge. 10 00:00:30,599 --> 00:00:35,460 A simple very old method of sending messages is the substitution cipher. 11 00:00:35,769 --> 00:00:38,120 I remember using this as a kid with some of my friends. 12 00:00:38,390 --> 00:00:40,850 We used to have something called a secret decoder ring. 13 00:00:41,200 --> 00:00:45,349 And the idea is you have a message and then you look at every character 14 00:00:45,349 --> 00:00:48,540 in that message and you replace it with the character on the ring. 15 00:00:49,000 --> 00:00:52,009 So for example, every time you see a lowercase a it gets replaced 16 00:00:52,010 --> 00:00:55,600 with a capital x or every time you see a lowercase b it gets replaced 17 00:00:55,600 --> 00:00:57,110 with a capital z and so forth. 18 00:00:57,360 --> 00:00:59,140 So it's called the substitution cipher. 19 00:00:59,140 --> 00:01:00,930 It's really a straightforward cipher. 20 00:01:01,170 --> 00:01:04,090 It's not a very secure cipher at all, but it's a fun cipher to do. 21 00:01:04,530 --> 00:01:05,780 So here's the idea. 22 00:01:05,830 --> 00:01:08,939 I want you to write a program that asks the user to enter a secret 23 00:01:08,940 --> 00:01:10,190 message, that'll be a string. 24 00:01:10,299 --> 00:01:13,520 And then I want you to encrypt that message using the substitution cipher 25 00:01:13,520 --> 00:01:15,310 and display the encrypted message. 26 00:01:15,710 --> 00:01:18,780 Then take that encrypted message and decrypt it back 27 00:01:18,780 --> 00:01:19,930 to the original message, 28 00:01:20,150 --> 00:01:23,479 okay? You can use two strings for your substitution. 29 00:01:23,490 --> 00:01:26,369 You can have one string for the alphabet. 30 00:01:26,389 --> 00:01:28,490 And let me write this here for you that way you maybe 31 00:01:28,490 --> 00:01:29,589 understand a little bit better. 32 00:01:29,930 --> 00:01:32,310 So let's say you have one string which is the alphabet. 33 00:01:34,490 --> 00:01:36,860 And this string contains all the characters that 34 00:01:36,860 --> 00:01:37,990 you want to substitute. 35 00:01:37,990 --> 00:01:42,050 So for example, let's say we've got a b c and so forth. 36 00:01:42,130 --> 00:01:44,760 And capital a capital b capital c. 37 00:01:45,330 --> 00:01:47,839 Okay, then I've got another string called the key. 38 00:01:49,100 --> 00:01:50,640 And you can name these anything you like. 39 00:01:51,620 --> 00:01:56,090 And the idea here is that for every character up in the alphabet string, 40 00:01:56,349 --> 00:01:59,639 you're going to have a character in the key string that'll substitute it. 41 00:01:59,860 --> 00:02:04,950 So if i want to substitute that lowercase a for an x, it'll be here. 42 00:02:05,309 --> 00:02:11,180 And let's say that the b gets a z and the c gets a q and then this 43 00:02:11,960 --> 00:02:17,820 uppercase a gets a lowercase, let's say, z, a lowercase q and 44 00:02:17,820 --> 00:02:21,110 a lowercase d, it doesn't really matter what you replace them with. 45 00:02:21,660 --> 00:02:24,470 So now the idea is really straightforward. 46 00:02:24,799 --> 00:02:28,900 Whenever you see the secret message, let's say, the secret message is 47 00:02:28,960 --> 00:02:32,730 a b c, you look in the alphabet. 48 00:02:33,090 --> 00:02:35,760 You look for that little a, you find it. 49 00:02:35,910 --> 00:02:37,480 You find it in position 0. 50 00:02:37,910 --> 00:02:40,899 Then you go to the key, look in position 0, whatever 51 00:02:41,079 --> 00:02:42,100 is there you replace it. 52 00:02:43,170 --> 00:02:45,030 You do the same thing with lowercase b. 53 00:02:45,530 --> 00:02:47,000 It's going to be replaced with the z. 54 00:02:47,940 --> 00:02:50,420 And you do the same thing with the lowercase c, it'll 55 00:02:50,429 --> 00:02:51,460 replace it with a q. 56 00:02:52,830 --> 00:02:55,949 Okay, obviously you just keep going down the string, whatever you find, 57 00:02:55,960 --> 00:02:59,510 you replace, so that would be my original message, this is now my 58 00:02:59,510 --> 00:03:02,850 encrypted message. How would i decrypt the message? Do the same 59 00:03:02,860 --> 00:03:04,200 thing just the other way around. 60 00:03:04,510 --> 00:03:06,230 You look in the key for an x. 61 00:03:06,570 --> 00:03:07,870 I find it at position 0. 62 00:03:08,080 --> 00:03:11,550 So I go to position zero in my alphabet, and I grab the little a. 63 00:03:12,590 --> 00:03:13,699 Again, I look for the z. 64 00:03:13,700 --> 00:03:14,900 It's in position one. 65 00:03:14,950 --> 00:03:16,490 There's my b in position one. 66 00:03:17,119 --> 00:03:21,490 And then my q is going to be in position three or sorry, position two. 67 00:03:22,780 --> 00:03:24,019 And it gives me my c. 68 00:03:24,410 --> 00:03:25,270 So there you go. 69 00:03:25,660 --> 00:03:27,430 Really, really straightforward to do. 70 00:03:27,790 --> 00:03:29,620 Make the cipher stronger if you wish. 71 00:03:29,620 --> 00:03:31,160 This is a really weak cipher. 72 00:03:31,980 --> 00:03:33,980 Right now it's only substituting the letters. 73 00:03:33,980 --> 00:03:36,250 And let me scroll down here just a little bit to show you what 74 00:03:36,250 --> 00:03:37,420 I'm giving you to start with. 75 00:03:38,700 --> 00:03:40,399 You can see what I'm giving you to start with. 76 00:03:40,400 --> 00:03:42,710 I'm giving you an alphabet, which is a string of all the 77 00:03:42,710 --> 00:03:44,450 lowercase and uppercase letters. 78 00:03:45,099 --> 00:03:47,489 I kept these in order just to make it easier to debug. 79 00:03:47,960 --> 00:03:48,970 And then you've got the key. 80 00:03:48,970 --> 00:03:50,210 Those are the substitutions. 81 00:03:50,210 --> 00:03:53,609 So for every one of these letters, there is an equivalent letter here 82 00:03:53,609 --> 00:03:54,830 that we're going to substitute. 83 00:03:54,910 --> 00:03:58,320 So for example whenever we see a lowercase a in our 84 00:03:58,320 --> 00:04:01,700 message, we're going to substitute it with a capital x. 85 00:04:02,550 --> 00:04:07,000 Whenever I see a capital z, I'm going to substitute it with a lowercase r. 86 00:04:08,160 --> 00:04:09,149 That's for encryption. 87 00:04:09,150 --> 00:04:11,890 And then for decryption, you just read the encrypted string 88 00:04:12,029 --> 00:04:13,150 and you go the other way around. 89 00:04:13,260 --> 00:04:17,910 So if you see a capital x in your encrypted string, you'll replace it 90 00:04:17,910 --> 00:04:19,940 with a lowercase a to decrypt it. 91 00:04:20,200 --> 00:04:21,439 That's it, really straightforward. 92 00:04:21,620 --> 00:04:25,630 As I said, I'm only using letters here, I'm not dealing 93 00:04:25,630 --> 00:04:27,050 with numbers or spaces. 94 00:04:27,059 --> 00:04:29,009 You can be as creative as you want with that. 95 00:04:29,819 --> 00:04:32,000 There's a lot of ways to make this a lot stronger 96 00:04:32,230 --> 00:04:33,440 and just have fun doing it. 97 00:04:33,510 --> 00:04:36,539 Remember, I'm writing this right here on line 28. 98 00:04:36,750 --> 00:04:39,700 Remember, the less code you write, the less code you have to test. 99 00:04:40,070 --> 00:04:42,810 You reuse existing functionality in the libraries. 100 00:04:43,320 --> 00:04:45,519 Use the code that's in the stream class. 101 00:04:45,549 --> 00:04:47,440 Don't try to create all this on your own. 102 00:04:48,049 --> 00:04:52,169 Remember, the kiss principle, keep this as simple as you can, reuse 103 00:04:52,170 --> 00:04:56,539 as much as you can and that makes your program very likely a lot 104 00:04:56,540 --> 00:04:59,830 more correct than it will be if you try to do everything on your own. 105 00:05:00,320 --> 00:05:03,610 Remember, those those library functions and methods that we've 106 00:05:03,610 --> 00:05:07,240 used, they've been tested, and they've been debugged for years and years. 107 00:05:07,270 --> 00:05:09,660 So they're pretty solid, so reuse those. 108 00:05:10,710 --> 00:05:14,479 So let me give you a sample run so that you can see what to expect. 109 00:05:14,550 --> 00:05:15,819 So I'm going to run this. 110 00:05:16,630 --> 00:05:18,810 And you can see here, enter your secret message. 111 00:05:18,810 --> 00:05:28,590 And I'll say I'll meet you at Billy's house at 10 a.m. 112 00:05:32,300 --> 00:05:33,410 Okay, now press enter. 113 00:05:34,270 --> 00:05:37,050 And it says encrypting message, and here's the encrypted message. 114 00:05:37,050 --> 00:05:39,190 You can see it's replacing characters. 115 00:05:39,639 --> 00:05:41,580 It's pretty obvious what's going on here, right. 116 00:05:41,869 --> 00:05:46,440 You can see the capital i in isle is being replaced with a lowercase j. 117 00:05:47,230 --> 00:05:50,190 The apostrophe is not being replaced at all because I'm 118 00:05:50,190 --> 00:05:51,540 only dealing with letters. 119 00:05:52,219 --> 00:05:56,720 The lowercase l is being replaced with the capital d, you can see here. 120 00:05:56,929 --> 00:05:59,759 So you can see going across that that's your encrypted message. 121 00:06:00,150 --> 00:06:03,480 Then I'm taking that encrypted message and decrypting it just doing the 122 00:06:03,480 --> 00:06:07,169 opposite I just did, and I got back the same message I started with. 123 00:06:07,750 --> 00:06:09,990 Okay, so you can see it's pretty easy. 124 00:06:10,380 --> 00:06:17,639 So my secret message would be this is my secret message. 125 00:06:19,599 --> 00:06:21,210 There's the encrypted message. 126 00:06:21,610 --> 00:06:25,040 Okay, you can see the third character here is an i, which is 127 00:06:25,040 --> 00:06:26,760 being replaced by a capital j. 128 00:06:27,290 --> 00:06:29,919 And you can see the i again right here in the is, and 129 00:06:29,920 --> 00:06:31,430 there's the capital j again. 130 00:06:32,670 --> 00:06:36,610 Okay, so have fun, like I said and make this stronger 131 00:06:36,650 --> 00:06:37,599 that's what I like to do. 132 00:06:37,700 --> 00:06:40,969 You know learn and play around with ideas that you may 133 00:06:40,970 --> 00:06:42,010 have to make this stronger. 134 00:06:42,010 --> 00:06:43,060 It's a lot of fun to try. 135 00:06:43,480 --> 00:06:46,530 Okay, so when you're done, I'll see you over in the solution, 136 00:06:46,830 --> 00:06:48,070 and I'll show you my solution.