1 00:00:00,009 --> 00:00:02,670 So welcome to the final part of the program where 2 00:00:02,680 --> 00:00:06,860 we are going to create the main user interaction. 3 00:00:07,130 --> 00:00:10,390 We're going to provide the user with four different options. 4 00:00:10,399 --> 00:00:14,220 Maybe they would like to generate a key encrypted file, the crypto file, 5 00:00:14,229 --> 00:00:15,579 maybe even just exit. 6 00:00:16,000 --> 00:00:22,200 And then depending on which option they choose, we are going to have to do something. 7 00:00:22,209 --> 00:00:23,569 So let's begin. 8 00:00:23,680 --> 00:00:25,260 All right. So I'm going to say, 9 00:00:25,500 --> 00:00:28,860 define main. This will be the main, the name of our 10 00:00:29,040 --> 00:00:29,979 interface 11 00:00:30,569 --> 00:00:34,900 and let us print out a general welcome message. This isn't necessary. 12 00:00:34,909 --> 00:00:39,849 But hey, let's just do that. Welcome to the encryption decryption tool. 13 00:00:41,049 --> 00:00:41,590 OK? 14 00:00:42,680 --> 00:00:43,310 OK. 15 00:00:43,700 --> 00:00:48,430 Now we're gonna create the while loop that's going to go through 16 00:00:48,630 --> 00:00:51,509 the four options. I'm gonna say while and now true 17 00:00:53,169 --> 00:00:53,709 call on 18 00:00:54,560 --> 00:00:57,180 and now let us print out the state 19 00:00:57,310 --> 00:00:59,299 the options. OK? So I'm gonna go print 20 00:01:00,599 --> 00:01:02,900 and now number one generates 21 00:01:03,709 --> 00:01:05,169 and encryption 22 00:01:06,160 --> 00:01:06,699 key. 23 00:01:07,699 --> 00:01:08,269 OK? 24 00:01:08,940 --> 00:01:13,519 And now let's go with option number two, which would be to simply encrypt, 25 00:01:14,639 --> 00:01:16,669 encrypt, a file. 26 00:01:18,720 --> 00:01:20,940 Uh Option number three 27 00:01:21,660 --> 00:01:22,959 will be what's 28 00:01:23,209 --> 00:01:24,160 decrypts 29 00:01:26,040 --> 00:01:26,900 file. 30 00:01:28,360 --> 00:01:30,660 And now the final option, 31 00:01:31,010 --> 00:01:32,739 which is the most boring option, 32 00:01:33,699 --> 00:01:35,519 exit the program. 33 00:01:36,830 --> 00:01:37,290 OK? 34 00:01:38,010 --> 00:01:38,980 There it is. Now, 35 00:01:40,360 --> 00:01:41,169 since we've, 36 00:01:41,949 --> 00:01:45,279 since we now have the four options, let us prompt the user 37 00:01:45,510 --> 00:01:49,370 to provide us with which option they would like to go with. 38 00:01:49,629 --> 00:01:54,470 So I'm gonna come over here right now and let's go with the variable option. 39 00:01:55,160 --> 00:01:56,459 So I'm gonna say option 40 00:01:57,339 --> 00:02:00,959 and our equals and our input. So let's prompt the user to tell us 41 00:02:02,059 --> 00:02:02,650 what 42 00:02:02,839 --> 00:02:03,470 option 43 00:02:05,239 --> 00:02:09,630 would you like to go with? Question mark? OK. 44 00:02:09,639 --> 00:02:12,179 So let's ask the user, hey, what would you like to do 45 00:02:12,529 --> 00:02:17,300 now? We'll have to create the el statements for each particular option. So 46 00:02:17,570 --> 00:02:18,500 let us go 47 00:02:19,369 --> 00:02:21,910 with the first one in here. So I'm gonna say if 48 00:02:22,490 --> 00:02:23,750 the option 49 00:02:24,669 --> 00:02:27,059 is equal to one, 50 00:02:28,279 --> 00:02:29,500 what do we want to do? 51 00:02:29,509 --> 00:02:35,800 Very simply, we can generate our key so I can come over here right now and simply say 52 00:02:36,330 --> 00:02:37,419 generate 53 00:02:37,779 --> 00:02:41,279 underscore key. And now in brackets, what's the name of the key 54 00:02:41,639 --> 00:02:43,899 that is encryption 55 00:02:44,449 --> 00:02:45,820 underscore key 56 00:02:46,100 --> 00:02:47,300 dot key. 57 00:02:47,669 --> 00:02:52,279 So we can run the function immediately, right? And this will work perfectly fine. 58 00:02:52,660 --> 00:02:53,580 However, 59 00:02:54,139 --> 00:02:56,500 I want to show you something this isn't necessary. 60 00:02:56,699 --> 00:03:00,190 But what if we wanted to provide the user 61 00:03:00,830 --> 00:03:05,250 with the option of actually using their own custom key 62 00:03:05,259 --> 00:03:07,729 and not just the default key that we've already created. 63 00:03:08,259 --> 00:03:09,720 So we give the user two options. 64 00:03:09,759 --> 00:03:13,039 You can either use the default key which we already have or 65 00:03:13,050 --> 00:03:17,740 you can provide us with the path to your own encryption key. 66 00:03:17,750 --> 00:03:19,880 Now, how would we do this? Well, 67 00:03:20,169 --> 00:03:22,490 I've already written the line right here. 68 00:03:22,649 --> 00:03:25,320 So let me just grab this cut, 69 00:03:25,990 --> 00:03:28,690 gonna come down in here and then paste and 70 00:03:28,699 --> 00:03:30,490 let me explain to you what's happening in here. 71 00:03:30,500 --> 00:03:30,850 So 72 00:03:31,119 --> 00:03:35,559 we're gonna have to pass this into the variable file underscore path 73 00:03:36,059 --> 00:03:41,190 and now input. So let's ask the user that, hey, enter the file path to save the key. 74 00:03:41,360 --> 00:03:44,039 Now over here where it says default encryption on 75 00:03:44,050 --> 00:03:46,309 key dot key is still part of the string. 76 00:03:46,669 --> 00:03:48,889 I am just indicating to the user that hey, 77 00:03:49,570 --> 00:03:53,240 if you don't provide us with your own custom key, we're going to use the default key, 78 00:03:53,250 --> 00:03:55,020 which is encryption on the key key. 79 00:03:55,289 --> 00:03:59,190 Now, this is very important where it says or 80 00:03:59,410 --> 00:04:02,270 encryption on the key key. Now, right here 81 00:04:02,470 --> 00:04:04,729 we are telling Python that hey, 82 00:04:04,949 --> 00:04:06,110 shooting case, 83 00:04:06,119 --> 00:04:10,470 even though the user has been provided with the option to use their own custom key. 84 00:04:10,710 --> 00:04:14,050 If they want to use our own default key, 85 00:04:14,199 --> 00:04:16,899 then use it. So here we said 86 00:04:17,000 --> 00:04:17,260 all 87 00:04:17,720 --> 00:04:20,010 and now the name of a default key. 88 00:04:20,149 --> 00:04:22,609 So all we need to do right here is just to remove 89 00:04:23,390 --> 00:04:24,709 the default option 90 00:04:24,899 --> 00:04:28,429 and then simply pass in the file underscore path 91 00:04:29,029 --> 00:04:32,929 and there you go. So that's option number one, 92 00:04:33,029 --> 00:04:34,239 all laid out. 93 00:04:34,399 --> 00:04:36,170 Let's move on to option number two. 94 00:04:37,040 --> 00:04:38,059 I'm going to come in here 95 00:04:38,929 --> 00:04:41,350 and say if 96 00:04:42,269 --> 00:04:43,190 choice 97 00:04:44,049 --> 00:04:45,279 equals 98 00:04:45,700 --> 00:04:46,059 two 99 00:04:47,059 --> 00:04:47,640 call on. 100 00:04:48,420 --> 00:04:49,459 So in here 101 00:04:49,559 --> 00:04:51,519 we would like to encrypt the file. 102 00:04:51,649 --> 00:04:55,170 Now, remember to encrypt the file, we need three parameters. 103 00:04:55,179 --> 00:04:57,489 First of all, we need to ask the user to tell us 104 00:04:57,700 --> 00:05:01,420 the file, they would like to encrypt which will be the input file. 105 00:05:01,809 --> 00:05:03,220 And then also 106 00:05:03,700 --> 00:05:06,600 we would like to store the encrypted content. 107 00:05:06,609 --> 00:05:08,760 What's going to be the name of the output file? 108 00:05:08,970 --> 00:05:14,119 And then third, which key would they like to use to do the encryption? So 109 00:05:14,320 --> 00:05:15,880 I'm gonna come over here right now. 110 00:05:16,609 --> 00:05:20,290 Let's pass the variable input underscore choice. Oh 111 00:05:20,720 --> 00:05:22,019 Do forgive me. It should be 112 00:05:22,149 --> 00:05:23,309 if option, 113 00:05:23,679 --> 00:05:26,350 not choice, I got carried away. Option, not choice. So 114 00:05:26,839 --> 00:05:27,690 input 115 00:05:28,750 --> 00:05:29,040 uh 116 00:05:29,309 --> 00:05:30,299 input file 117 00:05:30,769 --> 00:05:32,500 to forgive me, input underscore file. 118 00:05:33,170 --> 00:05:34,600 I'm gonna say equals 119 00:05:35,070 --> 00:05:37,380 and now let us prompt the user to tell us 120 00:05:38,179 --> 00:05:43,500 what's the name of the file. So I'm gonna say enter the path of the file to encrypt, 121 00:05:44,600 --> 00:05:46,510 let me remove this message. So 122 00:05:46,660 --> 00:05:50,899 enter the path of the file to encrypt. OK? So let's prompt the user to do that 123 00:05:51,649 --> 00:05:52,609 and then 124 00:05:53,119 --> 00:05:54,230 outputs 125 00:05:55,010 --> 00:06:00,989 underscore file is going to be equal to input. And now let us prompt the user to tell us 126 00:06:02,209 --> 00:06:05,269 the name of the Apple. First, I'm gonna say enter the name 127 00:06:05,679 --> 00:06:06,510 of the 128 00:06:07,299 --> 00:06:11,040 of the output file 129 00:06:12,209 --> 00:06:12,809 call on. 130 00:06:13,660 --> 00:06:16,890 And now finally, what about the key? 131 00:06:17,059 --> 00:06:20,399 So I'm gonna say key underscore path, 132 00:06:20,839 --> 00:06:23,869 OK? It's going to be equal to input 133 00:06:25,040 --> 00:06:25,820 and now 134 00:06:26,260 --> 00:06:29,920 we can say enter the path 135 00:06:30,500 --> 00:06:32,420 of the encryption 136 00:06:33,500 --> 00:06:35,839 uh encryption key. 137 00:06:37,260 --> 00:06:38,119 Otherwise 138 00:06:40,679 --> 00:06:42,089 the defaults 139 00:06:42,920 --> 00:06:44,769 will be used. 140 00:06:44,880 --> 00:06:45,420 OK? 141 00:06:45,549 --> 00:06:48,970 So I'm going to simplify the string statement in here. 142 00:06:48,980 --> 00:06:52,390 I'm not going to indicate the name of the default K. I don't think that's necessary. 143 00:06:52,869 --> 00:06:56,690 So all we have to do at this point right now is just to also say the or 144 00:06:57,779 --> 00:06:58,529 encryption 145 00:07:00,850 --> 00:07:02,630 uh underscore key, 146 00:07:03,709 --> 00:07:04,279 the key 147 00:07:05,290 --> 00:07:08,269 and there you go. Alright. So 148 00:07:09,019 --> 00:07:13,339 one thing we need to do right here is we need to account for errors. 149 00:07:13,440 --> 00:07:18,320 OK? What if the user, what if maybe they provided the wrong input file 150 00:07:18,540 --> 00:07:22,019 or the name of the output file is incorrect or maybe even the key 151 00:07:22,170 --> 00:07:23,339 isn't correct? 152 00:07:23,619 --> 00:07:27,429 What should happen? So let us provide the option right now to cater for that. 153 00:07:27,440 --> 00:07:29,040 So I'm gonna say try 154 00:07:29,790 --> 00:07:31,170 remember that. And now 155 00:07:31,399 --> 00:07:32,329 key 156 00:07:32,609 --> 00:07:35,510 equals load underscore key 157 00:07:35,850 --> 00:07:37,130 and now in brackets 158 00:07:37,570 --> 00:07:38,570 the key path, 159 00:07:39,470 --> 00:07:40,850 OK? And now 160 00:07:41,100 --> 00:07:43,000 the most important parts encrypt 161 00:07:50,160 --> 00:07:51,339 underscore file 162 00:07:52,299 --> 00:07:54,820 and now in brackets, what are the three parameters? 163 00:07:55,200 --> 00:07:59,070 We have the input underscore file, we have the output 164 00:08:00,010 --> 00:08:01,190 underscore file 165 00:08:01,679 --> 00:08:05,559 and now we have the key as well. So these are the three parameters 166 00:08:05,679 --> 00:08:06,750 and if there's any 167 00:08:07,040 --> 00:08:10,570 error, let us generate. So I'm gonna say except 168 00:08:12,940 --> 00:08:13,929 Exception, 169 00:08:14,839 --> 00:08:15,459 OK? 170 00:08:16,359 --> 00:08:17,940 As E 171 00:08:18,730 --> 00:08:20,660 and now we can print 172 00:08:21,089 --> 00:08:23,339 our error message. So I'm going to use the F string 173 00:08:23,869 --> 00:08:25,510 and then I will say something like 174 00:08:26,440 --> 00:08:27,559 uh during 175 00:08:29,869 --> 00:08:30,890 encryption, 176 00:08:32,780 --> 00:08:36,200 OK? Call on and now we can use our call braces to indicate 177 00:08:36,549 --> 00:08:40,219 what the particular error is. And There you go. 178 00:08:40,380 --> 00:08:44,650 So we have successfully created option number two with a 179 00:08:44,840 --> 00:08:46,830 user to provide the path of the file. 180 00:08:46,840 --> 00:08:51,960 They want to encrypt the name of the output file and then also the key as well. 181 00:08:51,969 --> 00:08:54,989 And we've also added some error handling. Fantastic. 182 00:08:55,349 --> 00:08:55,960 Now, 183 00:08:56,270 --> 00:08:57,320 all I'm going to do 184 00:08:58,289 --> 00:09:00,650 is I'm just gonna grab this block of code 185 00:09:01,559 --> 00:09:04,859 and I'm simply going to paste it because think about it, the encryption 186 00:09:05,039 --> 00:09:08,840 and decryption process is almost the exact same thing is just opposite, right? So 187 00:09:09,010 --> 00:09:11,030 I'm gonna copy all the block of code 188 00:09:11,320 --> 00:09:12,750 and I'm gonna come over here 189 00:09:13,409 --> 00:09:15,390 and now I am going to paste it. 190 00:09:15,940 --> 00:09:16,580 OK? 191 00:09:16,789 --> 00:09:21,710 Now notice though that the indentation isn't correct. So make sure 192 00:09:22,210 --> 00:09:24,099 that when you do the exact same thing as I did, 193 00:09:24,109 --> 00:09:26,669 make sure that your indentation is correct, 194 00:09:27,140 --> 00:09:30,989 make sure everything is aligned properly and this should be fine. OK? 195 00:09:31,419 --> 00:09:34,140 All we need to do right now is to change the options in here. 196 00:09:34,150 --> 00:09:35,849 So option number three to number two 197 00:09:36,250 --> 00:09:40,460 input file, I'm gonna say enter the path of the file to what to decrypt. 198 00:09:41,070 --> 00:09:41,669 OK? 199 00:09:42,989 --> 00:09:44,299 An output file, 200 00:09:45,669 --> 00:09:47,770 enter the name of the app file, that's fine. 201 00:09:47,960 --> 00:09:50,820 Enter the path of the encryption key otherwise. OK? 202 00:09:50,830 --> 00:09:54,909 Keep in mind that the encryption key is the exact same thing as the decryption key. 203 00:09:54,919 --> 00:09:56,989 So we don't need to change the text in here. 204 00:09:57,039 --> 00:09:59,530 You could change it if you want to, but I'm just gonna leave it as it is. 205 00:10:00,090 --> 00:10:04,650 And finally, OK, over here as well, we just need to change this 206 00:10:05,909 --> 00:10:09,549 and there you go, error handling is intact. 207 00:10:10,260 --> 00:10:12,799 And one more thing we need to change is going 208 00:10:12,809 --> 00:10:15,729 to be over here where it says the encrypt file 209 00:10:15,890 --> 00:10:18,849 over here. It should be a decrypt 210 00:10:19,460 --> 00:10:22,200 file. OK? So you want to make sure 211 00:10:22,830 --> 00:10:26,609 that whenever you copy and paste code and you need to alter some text, 212 00:10:26,830 --> 00:10:30,210 make sure that you go through it properly. Otherwise 213 00:10:30,419 --> 00:10:32,900 you may end up having some issues. 214 00:10:33,150 --> 00:10:33,729 Ok? 215 00:10:33,919 --> 00:10:38,010 All we have to do right now is to add the fourth option. So 216 00:10:39,159 --> 00:10:40,739 I'm going to come in here and say 217 00:10:41,000 --> 00:10:42,340 option equals four, 218 00:10:42,609 --> 00:10:43,419 we can print in 219 00:10:43,630 --> 00:10:44,460 the program 220 00:10:44,729 --> 00:10:48,780 and now we're going to have the break statement as well because remember 221 00:10:49,210 --> 00:10:52,380 that even though we provided four options to the user, 222 00:10:52,580 --> 00:10:55,489 they might become quite silly and decide to choose option 223 00:10:55,500 --> 00:10:58,359 number five or six or seven or any other option 224 00:10:58,489 --> 00:11:02,539 that doesn't even exist. So we need to account for that as well. 225 00:11:02,909 --> 00:11:05,289 I'm gonna say break and then 226 00:11:05,390 --> 00:11:09,460 we're going to provide the final else statement 227 00:11:10,380 --> 00:11:12,820 and now we can simply say print and then 228 00:11:13,150 --> 00:11:14,359 tell the user, 229 00:11:14,859 --> 00:11:19,130 please choose a valid option 230 00:11:19,929 --> 00:11:20,950 and there you go 231 00:11:22,809 --> 00:11:29,150 and there it is. And now all we got to do is to run 232 00:11:30,250 --> 00:11:31,539 the main function 233 00:11:32,070 --> 00:11:34,719 and there it is. So 234 00:11:34,989 --> 00:11:38,309 hopefully the program should work. 235 00:11:39,030 --> 00:11:39,049 Uh 236 00:11:39,179 --> 00:11:41,960 let me go ahead right now and 237 00:11:42,729 --> 00:11:44,400 run the program. 238 00:11:44,640 --> 00:11:47,039 And there you go. Ok? So 239 00:11:47,330 --> 00:11:50,840 let us test this out. I'm gonna go, first of all with option number one 240 00:11:51,000 --> 00:11:52,210 press enter, 241 00:11:52,760 --> 00:11:56,190 I'm gonna go with the default. So I'm gonna press enter again 242 00:11:56,789 --> 00:11:57,909 and there you go. Awesome. 243 00:11:57,919 --> 00:12:01,809 So you can see right now we do have the encryption key that's been created 244 00:12:02,000 --> 00:12:03,369 by the program. 245 00:12:03,500 --> 00:12:07,010 So now let us try to encrypt a file. 246 00:12:07,020 --> 00:12:10,960 So I'm gonna come over here right now and let's create a sample 247 00:12:11,840 --> 00:12:18,010 dot TXT file. Let's add some text in here. This is some sample text 248 00:12:18,340 --> 00:12:24,140 which we are going to encrypt 249 00:12:25,000 --> 00:12:26,729 and also decrypt. 250 00:12:27,960 --> 00:12:33,179 OK? So let's see. I'm gonna go ahead right now and go to the program. 251 00:12:33,570 --> 00:12:35,789 Let's go with option number two right now. 252 00:12:36,000 --> 00:12:39,539 So let us try to encrypt our sample 253 00:12:40,000 --> 00:12:41,700 dot TXT file. 254 00:12:41,950 --> 00:12:43,299 I'm gonna press enter 255 00:12:43,760 --> 00:12:48,299 and now what's going to be the name of the output file. Let's call this the encrypted 256 00:12:48,530 --> 00:12:50,960 underscore file dot TXT. 257 00:12:51,679 --> 00:12:54,880 Let's go. We're gonna use the default key. So I'm gonna press enter 258 00:12:55,359 --> 00:12:59,099 and there you go. So let's take a look. We have the encrypted underscore 259 00:12:59,200 --> 00:13:00,090 FDO TXT 260 00:13:00,270 --> 00:13:02,780 and there it is, this is the encrypted version 261 00:13:03,549 --> 00:13:09,659 of our sample text file. So now let us try to decrypt the encrypted file 262 00:13:10,000 --> 00:13:13,299 and see if we'll get back the same text. I'm gonna go with option number three, 263 00:13:13,619 --> 00:13:17,059 enter the path of the file to decrypt is going to be encrypted 264 00:13:18,609 --> 00:13:20,150 underscore file 265 00:13:20,609 --> 00:13:21,809 dot TXT. 266 00:13:22,479 --> 00:13:23,729 And that will be the name of the 267 00:13:23,890 --> 00:13:26,099 output file. We can call it decrypted 268 00:13:26,309 --> 00:13:27,549 underscore file 269 00:13:28,309 --> 00:13:29,570 dot TXT. 270 00:13:29,719 --> 00:13:31,440 We're gonna go with the default key 271 00:13:31,890 --> 00:13:37,210 and the 0000, I'm sorry. We have an error message that says 272 00:13:37,429 --> 00:13:39,080 no such file. The director encrypted 273 00:13:39,380 --> 00:13:41,690 file dot TXT. What did I do wrong? 274 00:13:42,690 --> 00:13:44,299 Uh We do have, oh. 275 00:13:45,590 --> 00:13:45,619 Hm. 276 00:13:47,099 --> 00:13:48,190 Do you see what I did? 277 00:13:49,119 --> 00:13:52,190 My file is called encrypted, not encrypted. 278 00:13:53,849 --> 00:13:56,909 I do apologize. I need to get myself a new pair of glasses. 279 00:13:57,210 --> 00:13:58,690 I do apologize. 280 00:13:58,820 --> 00:14:01,429 Let's try one of that again. So option number three. 281 00:14:02,239 --> 00:14:04,340 So the file here is encrypted 282 00:14:05,309 --> 00:14:08,130 uh underscore file dot TXT. 283 00:14:08,780 --> 00:14:10,890 And now the name of the output file, 284 00:14:11,460 --> 00:14:13,080 I'm gonna go with decrypted 285 00:14:13,809 --> 00:14:15,030 and let's go file 286 00:14:15,429 --> 00:14:16,289 dot txt 287 00:14:17,169 --> 00:14:22,070 default key and there you go. Ok. So now let's take a look at the decrypted file 288 00:14:22,609 --> 00:14:26,190 and there you can see it work. 289 00:14:26,419 --> 00:14:29,270 So first of all, we encrypted sample dot TXT. 290 00:14:29,609 --> 00:14:34,260 We got our encrypted file which you can see right now and then we decrypted this file 291 00:14:34,450 --> 00:14:39,109 to get back the exact same text. So you can see right now it works perfectly fine. 292 00:14:39,419 --> 00:14:45,669 Let's try option number four to exit the program. Ok. It exited the program. 293 00:14:46,099 --> 00:14:49,840 Let's run the program one more time and let's go with option number seven. 294 00:14:50,369 --> 00:14:54,789 And it says please choose a valid option and there you go. So 295 00:14:55,340 --> 00:14:55,679 well, 296 00:14:56,190 --> 00:15:01,250 we successfully created ourselves an encryption and decryption tool. 297 00:15:01,919 --> 00:15:04,000 It's been quite a lot. But hopefully 298 00:15:04,780 --> 00:15:07,599 you've learned some new concepts such as using the 299 00:15:08,219 --> 00:15:10,780 right binary and read binary modes. 300 00:15:11,010 --> 00:15:13,340 You've also learned how you can provide the option 301 00:15:13,349 --> 00:15:16,270 for the user to use their own custom key or 302 00:15:16,460 --> 00:15:17,789 use the default key. 303 00:15:18,140 --> 00:15:20,859 And hopefully you learned a lot in this money project. 304 00:15:20,869 --> 00:15:24,270 Thank you so much for watching and of course, I will see you in the next class.