1 00:00:00,009 --> 00:00:05,070 Welcome back. So in the previous video, we successfully created the encryption key 2 00:00:05,389 --> 00:00:09,279 and we give it the file path of encryption underscore key dot key. 3 00:00:09,460 --> 00:00:14,590 And we also wrote a simple function to load the key. So now 4 00:00:14,739 --> 00:00:17,670 let us write the functions which we're going to use 5 00:00:17,860 --> 00:00:19,389 to actually 6 00:00:19,760 --> 00:00:24,309 encrypt our files but also decrypt them. 7 00:00:24,620 --> 00:00:25,450 And by the way, 8 00:00:25,459 --> 00:00:31,190 I just noticed over here on line nine that I did not add my colon at the end. 9 00:00:31,200 --> 00:00:32,729 So please, if you haven't done that, 10 00:00:33,009 --> 00:00:34,889 I'll be sure to do that as well. 11 00:00:34,900 --> 00:00:41,360 And then also on line five, I missed another column as well. So please 12 00:00:41,709 --> 00:00:47,680 do add the columns to line five and line nine as well. OK. So 13 00:00:48,150 --> 00:00:53,360 we are going to write a function to encrypt our file. 14 00:00:53,779 --> 00:00:56,930 So I'm gonna come down here and let's say, define and 15 00:00:57,310 --> 00:00:59,880 let's call the function encrypt, 16 00:01:00,500 --> 00:01:02,139 encrypt 17 00:01:02,470 --> 00:01:03,909 underscore file. 18 00:01:04,050 --> 00:01:04,559 OK. 19 00:01:04,839 --> 00:01:05,599 Now 20 00:01:05,830 --> 00:01:07,199 think about this. OK. 21 00:01:07,830 --> 00:01:10,069 Our function because we're going to encrypt the file, 22 00:01:10,080 --> 00:01:12,930 we're going to require three different parameters. 23 00:01:12,940 --> 00:01:13,319 OK? 24 00:01:13,669 --> 00:01:16,379 The first parameter will be the actual file itself, 25 00:01:16,389 --> 00:01:18,029 which we're going to be encrypted. 26 00:01:18,040 --> 00:01:19,870 So let's call that file 27 00:01:20,160 --> 00:01:21,699 the input 28 00:01:22,300 --> 00:01:23,790 underscore file 29 00:01:24,000 --> 00:01:28,059 OK. Now, we're also going to need to create a file 30 00:01:28,160 --> 00:01:33,760 that will contain the encrypted version. So let's call that file the output 31 00:01:35,110 --> 00:01:38,000 underscore file. OK? And then finally, 32 00:01:38,169 --> 00:01:40,550 we're going to need the actual key, 33 00:01:40,559 --> 00:01:44,889 which we're going to use to encrypt the input file, right? 34 00:01:45,169 --> 00:01:47,120 So let me add my colon at the end. 35 00:01:47,620 --> 00:01:49,629 So the first thing we're going to do right now is 36 00:01:49,639 --> 00:01:53,089 we're going to create our fret object with the provided key. 37 00:01:53,099 --> 00:01:53,839 So I'm gonna say fer 38 00:01:55,190 --> 00:01:56,720 now equals 39 00:01:56,830 --> 00:01:57,889 capital 40 00:01:58,500 --> 00:01:59,180 F 41 00:01:59,379 --> 00:01:59,910 fret. 42 00:02:01,099 --> 00:02:02,680 And now in brackets, 43 00:02:03,650 --> 00:02:05,239 we're going to add our key. 44 00:02:05,610 --> 00:02:10,869 OK? So from here we want to read the input file. 45 00:02:10,970 --> 00:02:11,369 OK? 46 00:02:11,380 --> 00:02:12,199 That will be obviously, 47 00:02:12,210 --> 00:02:15,740 the next step will be to actually open up the input file that we want to encrypt. 48 00:02:15,750 --> 00:02:15,990 So 49 00:02:16,330 --> 00:02:19,789 I'm gonna say with open and now in brackets, 50 00:02:20,160 --> 00:02:23,339 I'm gonna say the inputs underscore file 51 00:02:23,759 --> 00:02:24,990 and now what mode 52 00:02:25,160 --> 00:02:28,710 we're gonna go with the read, binary mode. So RB, 53 00:02:29,029 --> 00:02:29,589 OK? 54 00:02:30,380 --> 00:02:33,199 And now I'm going to say as file 55 00:02:33,779 --> 00:02:34,779 and now 56 00:02:35,070 --> 00:02:37,559 what we're gonna do is we're going to read the contents of 57 00:02:37,570 --> 00:02:41,679 the file and we're going to pass those contents to another variable. 58 00:02:41,690 --> 00:02:45,229 So let's say let's call the variable original because I'm 59 00:02:45,240 --> 00:02:48,179 gonna say original right now will be equal to what 60 00:02:48,410 --> 00:02:53,070 the file dot read because we want to read the contents of the file 61 00:02:53,259 --> 00:02:55,479 and let's add our buckets right there. 62 00:02:55,660 --> 00:03:00,190 OK? We're doing great. We're doing great. Now, we need to encrypt the file. 63 00:03:00,770 --> 00:03:02,500 Ok? And by the way, I forgot to add 64 00:03:02,850 --> 00:03:06,270 the column in here on line 16. So let's add a column right there. 65 00:03:06,899 --> 00:03:11,490 Ok. So now that we've opened up the file, ok, we now want to encrypt it. 66 00:03:11,500 --> 00:03:12,350 So I'm gonna say, 67 00:03:13,059 --> 00:03:14,910 let's call this one encrypted. Ok. 68 00:03:14,919 --> 00:03:17,279 This will be the variable to store the actual encrypted version. 69 00:03:17,289 --> 00:03:18,919 So I'm gonna say encrypted will be equal to. 70 00:03:19,419 --> 00:03:20,070 And now fer 71 00:03:20,960 --> 00:03:23,490 dot encrypt 72 00:03:23,750 --> 00:03:28,770 and now in brackets because we've passed file dot readd into the variable original. 73 00:03:28,779 --> 00:03:29,960 So I'm gonna come in here right now 74 00:03:30,279 --> 00:03:31,429 and simply say, or 75 00:03:31,830 --> 00:03:32,199 region. 76 00:03:33,529 --> 00:03:34,160 OK? 77 00:03:34,520 --> 00:03:37,149 And then what is the last thing we're going to do? 78 00:03:37,350 --> 00:03:41,649 We would have to write the encrypted content to a file. 79 00:03:41,830 --> 00:03:42,419 So 80 00:03:42,630 --> 00:03:44,220 I'm gonna say with 81 00:03:44,679 --> 00:03:45,500 open 82 00:03:46,729 --> 00:03:49,550 and now in brackets the output 83 00:03:50,899 --> 00:03:52,190 underscore file 84 00:03:53,399 --> 00:03:57,850 and now the mode will be wide binary. So WB 85 00:03:58,750 --> 00:04:01,169 and now I'm gonna see as file 86 00:04:01,270 --> 00:04:01,300 at 87 00:04:01,740 --> 00:04:01,759 our 88 00:04:01,880 --> 00:04:03,130 colon right there. 89 00:04:04,259 --> 00:04:08,399 And all we have to do right now is to say file dot Right, 90 00:04:08,559 --> 00:04:11,410 because we're creating our file and now in brackets, what's it going to be, 91 00:04:11,419 --> 00:04:13,600 it's going to be encrypted. 92 00:04:13,610 --> 00:04:14,940 So I'm gonna say encrypted. 93 00:04:16,108 --> 00:04:18,170 So this will write the encrypted content of the file. 94 00:04:18,488 --> 00:04:22,690 And of course, we can simply add a print statement, we can say print 95 00:04:23,339 --> 00:04:28,049 and uh yep, we can go with this one, prints f file, encrypted and saved to 96 00:04:28,149 --> 00:04:30,040 the output file. 97 00:04:30,329 --> 00:04:31,480 So from here, 98 00:04:31,489 --> 00:04:37,209 what we have to do right now will be to create the function that we can use to decrypt 99 00:04:37,600 --> 00:04:41,109 the file. So it's basically going to be like a reverse of what we've we've just done. 100 00:04:41,119 --> 00:04:43,209 So check this out. Like I'm gonna come over here 101 00:04:43,670 --> 00:04:47,279 and let's call our function decrypt 102 00:04:48,179 --> 00:04:49,640 underscore file. 103 00:04:49,809 --> 00:04:52,369 And then just like with encryption, we're going to need three parameters, 104 00:04:52,380 --> 00:04:55,670 the input file, the output file as well as the key. 105 00:04:55,679 --> 00:04:56,119 So 106 00:04:56,480 --> 00:05:00,739 I'm going to add those as you can see, input file, output file and then key. 107 00:05:01,149 --> 00:05:03,350 And now, as usual, I'm gonna say fer 108 00:05:03,700 --> 00:05:03,980 net 109 00:05:04,859 --> 00:05:09,059 equals and then capital fernet and then in brackets key. 110 00:05:09,329 --> 00:05:12,670 So this will be the key that we're gonna use to decrypt our file. 111 00:05:12,769 --> 00:05:14,489 So now what we want to do, 112 00:05:14,630 --> 00:05:19,869 we want to read the contents of the encrypted file. So 113 00:05:20,269 --> 00:05:22,070 I'm gonna come in here on a new line, 114 00:05:22,350 --> 00:05:24,029 I'm gonna say with open 115 00:05:24,500 --> 00:05:27,399 and now what are we opening, we're opening up the input on the 116 00:05:27,500 --> 00:05:28,420 core file. 117 00:05:28,799 --> 00:05:32,149 And now what mode are B 118 00:05:32,630 --> 00:05:33,470 I'm gonna say 119 00:05:34,540 --> 00:05:35,839 uh as file 120 00:05:36,170 --> 00:05:37,290 and a colon. 121 00:05:37,529 --> 00:05:42,519 So now what do we want to do? We want to decrypt. So I'm gonna say the encrypted 122 00:05:44,429 --> 00:05:45,609 encrypted here 123 00:05:45,809 --> 00:05:47,170 equals to file 124 00:05:47,309 --> 00:05:48,570 dot read. OK. 125 00:05:48,869 --> 00:05:53,250 So we're passing the contents of the encrypted file to a variable called encrypted 126 00:05:53,559 --> 00:05:56,190 because now what we're gonna do is we're 127 00:05:56,200 --> 00:05:59,890 going to create another variable called decrypted. 128 00:06:00,660 --> 00:06:02,640 And now this will be equal to fernet 129 00:06:03,700 --> 00:06:06,279 and now I'm going to pass the crypt 130 00:06:07,820 --> 00:06:08,980 in brackets 131 00:06:09,480 --> 00:06:10,429 encrypted. 132 00:06:10,760 --> 00:06:15,529 So this right here is how we're going to decrypt the contents of our file. 133 00:06:15,799 --> 00:06:22,130 And now what is the last step we want to save the decrypted information to a new file. 134 00:06:22,220 --> 00:06:24,920 So now I'm simply going to say with open 135 00:06:25,269 --> 00:06:27,880 and now in brackets the output 136 00:06:28,859 --> 00:06:30,109 underscore file 137 00:06:31,239 --> 00:06:33,149 and now WB 138 00:06:34,369 --> 00:06:35,579 as file 139 00:06:35,709 --> 00:06:36,720 colon 140 00:06:37,160 --> 00:06:40,149 and now simply file dot Right. 141 00:06:41,350 --> 00:06:42,609 And now decrypted. 142 00:06:44,040 --> 00:06:44,970 So we're gonna write the 143 00:06:45,149 --> 00:06:46,450 code to the file. 144 00:06:46,660 --> 00:06:50,279 And then finally, we can just add the usual print statement. 145 00:06:50,290 --> 00:06:51,779 So we can say something like print 146 00:06:52,440 --> 00:06:55,619 our brackets F colon. 147 00:06:56,339 --> 00:06:58,140 And let us say 148 00:06:58,429 --> 00:07:01,019 file, decrypted and save to the 149 00:07:01,209 --> 00:07:03,440 output file. I think, I think this is fine. 150 00:07:04,359 --> 00:07:06,940 And there it is 151 00:07:08,130 --> 00:07:11,850 actually let me improve on this one. I can say 152 00:07:12,269 --> 00:07:13,859 a file, 153 00:07:15,200 --> 00:07:15,790 OK? 154 00:07:16,660 --> 00:07:19,190 And now call braces and the input file. 155 00:07:21,410 --> 00:07:24,109 OK? And now 156 00:07:25,290 --> 00:07:26,720 I would say decrypted 157 00:07:27,690 --> 00:07:31,329 and saved to the output file. I think this is better 158 00:07:31,730 --> 00:07:33,959 and there it is. So 159 00:07:34,100 --> 00:07:38,299 once again, we created functions to both encrypt and decrypt our files. 160 00:07:38,309 --> 00:07:41,470 For the encryption function. We need three parameters, 161 00:07:41,769 --> 00:07:43,869 the input file, which we're going to encrypt 162 00:07:44,130 --> 00:07:47,609 the file that we're going to create that will store the encrypted information, 163 00:07:47,619 --> 00:07:48,750 which is output file 164 00:07:48,880 --> 00:07:50,829 and then the key for the actual encryption. 165 00:07:51,079 --> 00:07:54,799 So we created a key right here by infinite equals finite and then brackets key. 166 00:07:55,000 --> 00:07:57,500 And now we opened up the original file 167 00:07:57,950 --> 00:08:00,519 and we store the contents of the file into our 168 00:08:00,529 --> 00:08:03,890 verbal called original by saying file dot read equals original. 169 00:08:04,239 --> 00:08:08,769 And now to do the actual encryption itself, we set encrypted equals F 170 00:08:08,910 --> 00:08:10,809 dot encrypt. And now in brackets original 171 00:08:11,140 --> 00:08:13,410 and now with open because we want to store 172 00:08:13,579 --> 00:08:16,730 the encrypted information on the new file, the output file, 173 00:08:16,940 --> 00:08:20,809 we created the file right here, lines 1920 21. 174 00:08:21,130 --> 00:08:23,470 And then we did pretty much the exact opposite with 175 00:08:23,730 --> 00:08:24,779 the decryption 176 00:08:24,880 --> 00:08:25,950 function. So 177 00:08:26,149 --> 00:08:28,309 join me in the very next video where 178 00:08:28,839 --> 00:08:33,750 we're gonna go ahead and now create the main function for the user interaction.