1
00:00:00,009 --> 00:00:03,509
To round up this section on libraries and modules.

2
00:00:03,519 --> 00:00:08,289
I need to show you how you can work with an external library.

3
00:00:08,850 --> 00:00:12,789
And the one we're going to be working with is going to be a very,

4
00:00:12,800 --> 00:00:17,010
very powerful library and it's going to be the Cryptography

5
00:00:17,200 --> 00:00:21,430
library. And in fact, let me drag over the documentation.

6
00:00:22,319 --> 00:00:27,110
So this particular library is developed and maintained by the Python community.

7
00:00:27,360 --> 00:00:30,860
You can go to Cryptography dot I or if you want to learn more.

8
00:00:31,129 --> 00:00:33,430
But right here, they provide

9
00:00:33,540 --> 00:00:37,139
us with ideas on how to actually import

10
00:00:37,630 --> 00:00:40,119
the library and specifically

11
00:00:40,470 --> 00:00:42,560
a particular function, which is the

12
00:00:43,099 --> 00:00:46,459
fernet function. OK. This is what is mostly used for

13
00:00:46,700 --> 00:00:49,680
encrypting and also decrypting messages.

14
00:00:50,029 --> 00:00:50,580
So

15
00:00:50,909 --> 00:00:52,020
let me show you

16
00:00:52,759 --> 00:00:54,689
how we're going to work with it.

17
00:00:54,700 --> 00:00:56,970
And I've added some notes in here because it's

18
00:00:56,979 --> 00:01:00,209
very important for you to understand step by step

19
00:01:00,389 --> 00:01:04,069
how our program is actually going to work. Because to be honest,

20
00:01:04,440 --> 00:01:07,769
it can get quite confusing. OK? I'm not gonna lie.

21
00:01:08,209 --> 00:01:11,190
Remember that in encryption,

22
00:01:11,389 --> 00:01:13,589
we can encrypt a key.

23
00:01:13,629 --> 00:01:17,029
All right, we're going to use a cipher that we can then use to encrypt our key.

24
00:01:17,309 --> 00:01:21,230
But then we can also decrypt the message that has been encrypted.

25
00:01:21,339 --> 00:01:21,849
So

26
00:01:22,139 --> 00:01:24,370
the way it works is that first of all,

27
00:01:24,599 --> 00:01:25,690
by using the F

28
00:01:25,940 --> 00:01:28,349
function, we'll have to generate the key

29
00:01:28,519 --> 00:01:31,440
that we're going to use for both encrypting and decrypting.

30
00:01:31,889 --> 00:01:36,730
And then we can pass that key to our cipher.

31
00:01:36,989 --> 00:01:39,569
Remember in the world of encryption, we're gonna use ciphers

32
00:01:40,199 --> 00:01:41,620
that combines an algorithm

33
00:01:41,750 --> 00:01:45,830
and a key for the actual encryption and decryption. So it's basically four steps.

34
00:01:45,839 --> 00:01:47,790
First of all, we generate our key.

35
00:01:47,919 --> 00:01:48,580
So we say

36
00:01:48,839 --> 00:01:50,949
to generate key, this will generate the key

37
00:01:51,129 --> 00:01:53,860
and then we can pass the key to a cipher

38
00:01:54,180 --> 00:01:57,019
and now to encrypt the actual message,

39
00:01:57,169 --> 00:02:02,419
we will say cipher because now it's holding the key and then dot encrypt

40
00:02:02,949 --> 00:02:06,180
and then in brackets are function message dot encode

41
00:02:06,779 --> 00:02:08,899
and then the opposite to decrypt,

42
00:02:09,000 --> 00:02:12,550
we can say decrypted message would be equal to cipher dot decrypt.

43
00:02:13,000 --> 00:02:17,789
And now in brackets because we're decrypted what the encrypted message.

44
00:02:17,800 --> 00:02:19,199
So we're gonna pass that in

45
00:02:19,440 --> 00:02:22,440
and then add the last method dot decode

46
00:02:22,580 --> 00:02:23,699
to decrypt

47
00:02:24,110 --> 00:02:25,119
a message.

48
00:02:25,490 --> 00:02:26,199
So

49
00:02:26,539 --> 00:02:30,119
let us start from the very beginning. OK.

50
00:02:30,500 --> 00:02:31,679
The first thing I'm gonna do

51
00:02:32,800 --> 00:02:35,100
is to import, so I'm gonna say from

52
00:02:35,970 --> 00:02:36,919
clip to

53
00:02:37,440 --> 00:02:37,479
graph

54
00:02:37,679 --> 00:02:38,440
feed

55
00:02:39,639 --> 00:02:40,509
dot

56
00:02:40,850 --> 00:02:41,199
fernet,

57
00:02:42,259 --> 00:02:43,910
we're going to import

58
00:02:44,479 --> 00:02:46,419
the class of fernet.

59
00:02:46,690 --> 00:02:47,259
OK?

60
00:02:47,889 --> 00:02:49,630
That's the very first step.

61
00:02:50,110 --> 00:02:53,399
Now step one remember is to generate the key.

62
00:02:53,559 --> 00:02:55,020
So I'm gonna say key

63
00:02:56,199 --> 00:02:57,089
equals

64
00:02:57,979 --> 00:02:58,770
and now fer

65
00:02:58,979 --> 00:03:01,320
generate underscore key.

66
00:03:01,710 --> 00:03:02,259
This will create a

67
00:03:02,369 --> 00:03:02,850
key

68
00:03:03,089 --> 00:03:05,110
and now we can say cipher

69
00:03:05,559 --> 00:03:07,240
will be equal to

70
00:03:07,520 --> 00:03:07,779
fernet

71
00:03:08,720 --> 00:03:09,550
and then key.

72
00:03:10,809 --> 00:03:14,460
At this point in time, I would like us to first of all print

73
00:03:15,300 --> 00:03:18,039
the generated key that we've created. So

74
00:03:18,479 --> 00:03:21,559
I can come in here right now and say print and then in brackets,

75
00:03:22,309 --> 00:03:25,839
let's say uh generated key. OK. So J

76
00:03:26,570 --> 00:03:27,889
narrated

77
00:03:28,679 --> 00:03:29,809
and then key

78
00:03:30,300 --> 00:03:31,539
and add my colon

79
00:03:32,669 --> 00:03:34,690
and then a comma

80
00:03:35,190 --> 00:03:36,380
and then key

81
00:03:38,130 --> 00:03:38,869
dot

82
00:03:39,089 --> 00:03:39,820
the code.

83
00:03:40,820 --> 00:03:41,440
All right.

84
00:03:41,830 --> 00:03:45,360
So this right here, this particular function here is what we're going to use

85
00:03:45,500 --> 00:03:48,520
to show the key as a string.

86
00:03:48,750 --> 00:03:49,360
Now,

87
00:03:49,520 --> 00:03:53,639
next step will be to actually encrypt the message.

88
00:03:54,029 --> 00:03:55,199
So what do we do?

89
00:03:56,089 --> 00:03:57,539
We can say message

90
00:03:58,610 --> 00:04:00,389
equals, let's add

91
00:04:00,600 --> 00:04:03,800
the message. The default here says hello world.

92
00:04:04,970 --> 00:04:08,789
Uh Let's make it a bit more complex. I'm gonna say hello world.

93
00:04:08,860 --> 00:04:13,160
This is a circuit message. OK? I think this is fine. This is a circuit message. OK? So

94
00:04:13,320 --> 00:04:14,500
this is the message

95
00:04:15,229 --> 00:04:18,769
they were going to first of all encrypt and then decrypt. So

96
00:04:19,238 --> 00:04:22,829
I will now come in here right now and say encrypted

97
00:04:23,359 --> 00:04:24,970
on this call message.

98
00:04:25,779 --> 00:04:28,529
Now we're going to encrypt the message and how we're going to do that.

99
00:04:28,660 --> 00:04:30,250
We are going to say cipher

100
00:04:31,140 --> 00:04:33,000
dot encrypt. And then

101
00:04:33,220 --> 00:04:35,739
in brackets, we're gonna pass the message

102
00:04:35,910 --> 00:04:39,510
and the method dot encode to encrypt a

103
00:04:39,619 --> 00:04:40,600
message

104
00:04:41,720 --> 00:04:42,570
so far. So good.

105
00:04:43,410 --> 00:04:44,429
Next step

106
00:04:44,690 --> 00:04:46,510
will now be to print,

107
00:04:46,899 --> 00:04:49,950
we can print the encrypted message right now. So I'm gonna print in brackets,

108
00:04:50,880 --> 00:04:52,820
let's add our codes and then I can say,

109
00:04:53,109 --> 00:04:54,640
uh, encrypted

110
00:04:55,170 --> 00:04:56,980
message

111
00:04:57,239 --> 00:04:59,619
is colon and now

112
00:05:00,540 --> 00:05:01,600
add my comma

113
00:05:01,869 --> 00:05:03,010
and then simply

114
00:05:03,399 --> 00:05:05,720
say encrypted underscore

115
00:05:06,059 --> 00:05:06,790
message.

116
00:05:07,630 --> 00:05:08,290
OK.

117
00:05:08,950 --> 00:05:10,040
So far so good.

118
00:05:10,209 --> 00:05:14,670
The last step, of course, after encryption will be to decrypt.

119
00:05:14,890 --> 00:05:16,739
So I'm gonna say decrypted

120
00:05:18,929 --> 00:05:20,690
underscore message

121
00:05:20,829 --> 00:05:22,059
equals to what

122
00:05:22,709 --> 00:05:23,470
cipher

123
00:05:23,959 --> 00:05:26,070
dot decrypt.

124
00:05:27,119 --> 00:05:30,579
And then in brackets, we're going to pass in what the encrypted

125
00:05:32,790 --> 00:05:34,619
and crypted

126
00:05:35,399 --> 00:05:37,429
underscore message.

127
00:05:38,260 --> 00:05:39,399
And then

128
00:05:39,559 --> 00:05:40,510
dot

129
00:05:40,859 --> 00:05:41,880
the code

130
00:05:43,730 --> 00:05:44,790
brackets

131
00:05:45,260 --> 00:05:46,720
close the last bracket

132
00:05:47,059 --> 00:05:48,739
and there it is.

133
00:05:49,000 --> 00:05:51,869
So last step right now will just be to print

134
00:05:52,119 --> 00:05:54,320
decrypted message is and then

135
00:05:54,459 --> 00:05:55,619
decrypted

136
00:05:55,769 --> 00:06:00,399
message. Now I can see here we do have, oh, there is no closing bracket to forgive me.

137
00:06:00,410 --> 00:06:01,670
I made a mistake right there

138
00:06:02,079 --> 00:06:05,720
and hopefully this should work.

139
00:06:06,010 --> 00:06:08,279
Let me go ahead right now and run

140
00:06:09,519 --> 00:06:10,200
and there you go.

141
00:06:10,209 --> 00:06:13,679
So right now you can see on my screen, it says loading the next environment.

142
00:06:13,690 --> 00:06:14,869
So the beauty about

143
00:06:15,440 --> 00:06:16,649
Python when working

144
00:06:16,910 --> 00:06:17,600
is that

145
00:06:17,890 --> 00:06:23,079
even though this is actually an external library

146
00:06:23,089 --> 00:06:25,660
is going to automatically import the library for.

147
00:06:25,670 --> 00:06:27,950
So it's basically installed the library

148
00:06:28,130 --> 00:06:31,040
and now we have the program running and it says

149
00:06:31,220 --> 00:06:35,350
the generated key. Was this ok? That's a long key.

150
00:06:35,510 --> 00:06:38,660
So the encrypted message is now this right here

151
00:06:39,070 --> 00:06:40,679
very, very long as you can see.

152
00:06:40,690 --> 00:06:46,450
And of course, the decrypted message is hello world, this is a secret message.

153
00:06:46,779 --> 00:06:50,760
Let's try changing the message to something else.

154
00:06:51,149 --> 00:06:54,519
And I'm gonna say, Python is amazing.

155
00:06:55,640 --> 00:06:57,019
Let's run it again

156
00:06:57,440 --> 00:07:02,279
and there you go. So now we have a different generated key and of course, a different

157
00:07:02,559 --> 00:07:04,859
encrypted message. So

158
00:07:05,190 --> 00:07:08,709
that's how to work with the Cryptography

159
00:07:08,959 --> 00:07:13,890
library. And just to kind of give you a quick recap. Once again. First of all,

160
00:07:14,670 --> 00:07:17,049
we imported the module fernet

161
00:07:17,230 --> 00:07:19,079
from the library of Cryptography.

162
00:07:19,760 --> 00:07:21,540
And then of course, in the world of Cryptography,

163
00:07:21,549 --> 00:07:24,829
we're gonna have a key that we use for encryption and decryption.

164
00:07:25,079 --> 00:07:27,059
We generated the key right here with the use of the

165
00:07:27,450 --> 00:07:28,890
generate key method.

166
00:07:29,149 --> 00:07:31,079
And then we assign the key to our

167
00:07:31,190 --> 00:07:31,760
cipher.

168
00:07:32,399 --> 00:07:37,140
We printed out the generated key by making use of the key to decode function

169
00:07:37,489 --> 00:07:38,160
and then

170
00:07:38,369 --> 00:07:43,140
to encrypt the message we have to, first of all add what the message actually is.

171
00:07:43,299 --> 00:07:45,529
The message here is Python is amazing.

172
00:07:45,859 --> 00:07:51,010
And then we say, OK, encrypted message will be equal to now the function cipher

173
00:07:51,170 --> 00:07:52,239
encrypt

174
00:07:52,399 --> 00:07:55,540
and then in brackets message dot encode.

175
00:07:55,769 --> 00:07:59,510
So the encode method right here

176
00:07:59,750 --> 00:08:03,619
will convert the plain text or our string into bytes.

177
00:08:03,790 --> 00:08:04,230
OK?

178
00:08:04,519 --> 00:08:06,119
And now the cipher

179
00:08:06,670 --> 00:08:09,540
encrypt will take the encoded message,

180
00:08:09,709 --> 00:08:14,290
it will then encrypt it producing an encrypted version.

181
00:08:14,299 --> 00:08:19,209
And of course, the output will be equal to the encrypted underscore message.

182
00:08:19,369 --> 00:08:22,450
And then the last step right now is to actually decrypt the message. So

183
00:08:22,600 --> 00:08:25,420
cipher dot decrypt right here,

184
00:08:25,820 --> 00:08:28,459
we'll take the encrypted message and then decrypt it,

185
00:08:28,630 --> 00:08:31,269
we turn it back to the original format

186
00:08:31,489 --> 00:08:34,479
and then the dot decode right here. Then this method

187
00:08:34,659 --> 00:08:36,719
will convert the decrypted bytes

188
00:08:36,900 --> 00:08:39,710
back into a readable string format.

189
00:08:39,719 --> 00:08:44,200
And of course, finally, we printed out the actual uh decrypted message.

190
00:08:44,460 --> 00:08:44,960
So

191
00:08:45,080 --> 00:08:48,200
that's it. Thank you for watching the video. I will see you in the next class.

