1
00:00:00,009 --> 00:00:01,580
All right. So let's begin.

2
00:00:01,590 --> 00:00:03,829
And the very first thing we're going to do

3
00:00:03,839 --> 00:00:06,750
is we're going to import the required library.

4
00:00:07,010 --> 00:00:10,159
And that's of course, going to be the Cryptography dot fernet.

5
00:00:10,449 --> 00:00:10,960
So

6
00:00:11,189 --> 00:00:13,250
I'm gonna come in here and say from

7
00:00:13,479 --> 00:00:16,110
and then Cryptography

8
00:00:16,930 --> 00:00:20,079
does fnet now import

9
00:00:21,360 --> 00:00:22,530
fernet.

10
00:00:22,770 --> 00:00:23,520
OK.

11
00:00:24,250 --> 00:00:26,709
Now we want to write a function

12
00:00:26,959 --> 00:00:31,200
that we can use to generate and save our encryption key.

13
00:00:31,370 --> 00:00:32,560
So let's go ahead and create a function.

14
00:00:32,569 --> 00:00:36,709
I'm going to call this one define and let's say generate

15
00:00:36,810 --> 00:00:38,479
underscore key.

16
00:00:38,840 --> 00:00:39,590
And now in brackets.

17
00:00:39,599 --> 00:00:41,680
So this is gonna be the name of our function,

18
00:00:41,689 --> 00:00:44,590
which we're going to use to generate our encryption key.

19
00:00:44,869 --> 00:00:47,930
Now, the thing is whenever you're generating your keys,

20
00:00:47,939 --> 00:00:49,090
whenever you're using this function,

21
00:00:49,099 --> 00:00:52,270
we can actually add a particular kind of parameter

22
00:00:52,459 --> 00:00:56,250
which will be the file path, we can create the name of our file.

23
00:00:56,340 --> 00:00:58,759
So what I'm gonna do right now is I'm gonna say file

24
00:00:59,639 --> 00:01:01,369
and now underscore path,

25
00:01:02,630 --> 00:01:06,620
OK, will now be equal. And now we can add the name of the file in

26
00:01:06,849 --> 00:01:10,930
this case right now in codes, I'm going to say encryption

27
00:01:12,919 --> 00:01:14,510
underscore key

28
00:01:15,440 --> 00:01:17,099
dot key.

29
00:01:17,279 --> 00:01:21,400
So this is how you want to save the file for your encryption key.

30
00:01:21,410 --> 00:01:26,839
It's going to be the name and then dot key and I'm going to add my colon at the end, very,

31
00:01:26,849 --> 00:01:27,720
very important.

32
00:01:27,989 --> 00:01:31,900
So now to generate the actual key itself, you can see

33
00:01:32,050 --> 00:01:33,459
it's already given us the

34
00:01:33,769 --> 00:01:35,569
information I'm gonna say key

35
00:01:35,860 --> 00:01:37,580
equals and now fer

36
00:01:39,470 --> 00:01:39,959
A F

37
00:01:41,230 --> 00:01:42,519
and now dot

38
00:01:43,559 --> 00:01:44,400
generate

39
00:01:45,709 --> 00:01:48,830
underscore key and now in brackets. So

40
00:01:48,930 --> 00:01:52,180
this right here is what we're going to use to generate the actual key.

41
00:01:52,190 --> 00:01:53,839
We're going to use this method which is the fer

42
00:01:53,949 --> 00:01:55,019
to generate key

43
00:01:55,190 --> 00:01:58,059
and we're going to save it in a variable called key.

44
00:01:58,180 --> 00:02:02,139
So now let us create the actual file itself. So

45
00:02:02,319 --> 00:02:03,610
I'm gonna come in here right now

46
00:02:03,879 --> 00:02:06,449
and I'm gonna say with OK

47
00:02:06,910 --> 00:02:07,730
with

48
00:02:08,070 --> 00:02:12,229
and now open. So we're gonna create our file and now in brackets,

49
00:02:12,479 --> 00:02:14,490
I'm going to add the file path.

50
00:02:15,179 --> 00:02:16,639
OK? And now comma

51
00:02:17,119 --> 00:02:18,039
and now

52
00:02:18,339 --> 00:02:21,419
the right mode. So it's gonna be WB

53
00:02:21,529 --> 00:02:22,809
and now I'm gonna say

54
00:02:24,020 --> 00:02:24,990
key

55
00:02:25,110 --> 00:02:26,720
on the score file.

56
00:02:26,970 --> 00:02:32,320
So this right here will open up a file in what we call a right binary mode.

57
00:02:32,330 --> 00:02:35,399
Now, you may wonder what exactly is WB,

58
00:02:35,410 --> 00:02:38,009
it's similar to the right mode which will

59
00:02:38,020 --> 00:02:40,020
just be w which we're already familiar with.

60
00:02:40,369 --> 00:02:41,320
However,

61
00:02:41,550 --> 00:02:45,210
in WB, you will write data in a binary format

62
00:02:45,350 --> 00:02:50,389
instead of plain text. So basically this means it will write data as raw bytes

63
00:02:50,550 --> 00:02:53,529
rather than characters. Now you might be wondering. OK.

64
00:02:53,539 --> 00:02:57,339
So what exactly is the point? What is the advantage? Well,

65
00:02:57,440 --> 00:02:58,309
if you write binary

66
00:02:58,470 --> 00:03:01,889
data using your regular text mode, which will be w

67
00:03:02,169 --> 00:03:04,839
the program will attempt to interpret the bytes

68
00:03:04,850 --> 00:03:07,380
as characters which can very often result in

69
00:03:07,589 --> 00:03:10,139
encoding errors, data corruption,

70
00:03:10,240 --> 00:03:13,050
or maybe even loss of data integrity. So

71
00:03:13,229 --> 00:03:17,369
using WB is always recommended whenever you're working with encryption,

72
00:03:17,610 --> 00:03:21,100
image files or any non text data to ensure that

73
00:03:21,160 --> 00:03:25,059
the data will be correctly stored in its original byte format

74
00:03:25,169 --> 00:03:27,389
without any alteration.

75
00:03:27,740 --> 00:03:31,830
All right. So now we'll have to write the key to the file.

76
00:03:32,080 --> 00:03:33,580
So I'm going to come over here right now

77
00:03:34,410 --> 00:03:36,389
and I'm gonna say key

78
00:03:38,250 --> 00:03:42,309
underscore file and now the method writes

79
00:03:42,779 --> 00:03:46,410
and now what are we passing in? We're passing in the actual key itself.

80
00:03:46,740 --> 00:03:49,380
So this right here will write the key to the file

81
00:03:49,630 --> 00:03:53,259
and then it's done, but we can also just write something to print out.

82
00:03:53,270 --> 00:03:54,149
So let's just say

83
00:03:54,619 --> 00:03:56,350
print.

84
00:03:56,990 --> 00:03:59,089
OK. And now in brackets,

85
00:03:59,339 --> 00:04:01,080
we can use our string

86
00:04:01,929 --> 00:04:06,190
and I'm gonna say codes, let's say encryption

87
00:04:07,220 --> 00:04:09,720
encryption key saved

88
00:04:09,919 --> 00:04:10,720
two.

89
00:04:11,020 --> 00:04:13,339
And now we can add the file path

90
00:04:15,179 --> 00:04:16,428
underscore path

91
00:04:17,630 --> 00:04:20,690
and there it is. So

92
00:04:20,820 --> 00:04:27,540
we have successfully created the function for generating our encryption key.

93
00:04:27,769 --> 00:04:33,970
But now we'll also write another function to load the encryption key from the file.

94
00:04:33,980 --> 00:04:34,369
So

95
00:04:34,570 --> 00:04:35,769
I'm going to come in here,

96
00:04:36,140 --> 00:04:39,809
I'm going to say, define load underscore key.

97
00:04:40,859 --> 00:04:45,179
And now in brackets, I'm going to add our file path

98
00:04:45,809 --> 00:04:48,359
and now this will be equal of course to encryption key,

99
00:04:48,769 --> 00:04:51,859
encryption underscore key key.

100
00:04:52,000 --> 00:04:53,239
So now from here

101
00:04:54,070 --> 00:04:54,989
I'm going to say

102
00:04:55,779 --> 00:04:57,079
uh with

103
00:04:58,660 --> 00:05:00,029
and now open

104
00:05:01,660 --> 00:05:04,839
can I add the file on the score path?

105
00:05:05,609 --> 00:05:09,589
And now we want to read, right? We want to read from the file.

106
00:05:09,600 --> 00:05:13,070
So what do you think we're going to use, we're going to use RB

107
00:05:13,320 --> 00:05:15,279
as opposed to WB. So

108
00:05:15,450 --> 00:05:17,329
it's not going to be in codes,

109
00:05:17,519 --> 00:05:19,869
it's going to be RB.

110
00:05:20,250 --> 00:05:21,790
Let me add my comma

111
00:05:22,299 --> 00:05:23,670
right there

112
00:05:24,489 --> 00:05:28,190
and then close the bracket. And now as key

113
00:05:28,350 --> 00:05:30,190
underscore file,

114
00:05:31,950 --> 00:05:33,000
add a colon.

115
00:05:33,260 --> 00:05:38,369
And now finally to with the key from the file, we can simply say key equals

116
00:05:39,059 --> 00:05:40,070
and then key

117
00:05:40,829 --> 00:05:43,130
underscore file dot the

118
00:05:43,250 --> 00:05:44,010
read method

119
00:05:44,459 --> 00:05:45,910
and now in brackets

120
00:05:46,230 --> 00:05:48,920
and now we can simply return

121
00:05:50,790 --> 00:05:50,809
our

122
00:05:51,010 --> 00:05:51,489
key.

123
00:05:51,500 --> 00:05:53,239
So the function we've just created right here,

124
00:05:53,250 --> 00:05:56,570
the load underscore key will simply read and

125
00:05:56,579 --> 00:05:58,839
return the decryption key stored in the file.

126
00:05:58,920 --> 00:05:59,410
And of course,

127
00:05:59,420 --> 00:06:03,399
this is going to be the key that will be needed for both encryption and decryption.

128
00:06:03,750 --> 00:06:05,920
So join me next video where we're going to go ahead right now

129
00:06:05,929 --> 00:06:10,839
to write functions to encrypt a file and also to decrypt the file.

