To round up this section on libraries and modules.
I need to show you how you can work with an external library.
And the one we're going to be working with is going to be a very,
very powerful library and it's going to be the Cryptography
library. And in fact, let me drag over the documentation.
So this particular library is developed and maintained by the Python community.
You can go to Cryptography dot I or if you want to learn more.
But right here, they provide
us with ideas on how to actually import
the library and specifically
a particular function, which is the
fernet function. OK. This is what is mostly used for
encrypting and also decrypting messages.
So
let me show you
how we're going to work with it.
And I've added some notes in here because it's
very important for you to understand step by step
how our program is actually going to work. Because to be honest,
it can get quite confusing. OK? I'm not gonna lie.
Remember that in encryption,
we can encrypt a key.
All right, we're going to use a cipher that we can then use to encrypt our key.
But then we can also decrypt the message that has been encrypted.
So
the way it works is that first of all,
by using the F
function, we'll have to generate the key
that we're going to use for both encrypting and decrypting.
And then we can pass that key to our cipher.
Remember in the world of encryption, we're gonna use ciphers
that combines an algorithm
and a key for the actual encryption and decryption. So it's basically four steps.
First of all, we generate our key.
So we say
to generate key, this will generate the key
and then we can pass the key to a cipher
and now to encrypt the actual message,
we will say cipher because now it's holding the key and then dot encrypt
and then in brackets are function message dot encode
and then the opposite to decrypt,
we can say decrypted message would be equal to cipher dot decrypt.
And now in brackets because we're decrypted what the encrypted message.
So we're gonna pass that in
and then add the last method dot decode
to decrypt
a message.
So
let us start from the very beginning. OK.
The first thing I'm gonna do
is to import, so I'm gonna say from
clip to
graph
feed
dot
fernet,
we're going to import
the class of fernet.
OK?
That's the very first step.
Now step one remember is to generate the key.
So I'm gonna say key
equals
and now fer
generate underscore key.
This will create a
key
and now we can say cipher
will be equal to
fernet
and then key.
At this point in time, I would like us to first of all print
the generated key that we've created. So
I can come in here right now and say print and then in brackets,
let's say uh generated key. OK. So J
narrated
and then key
and add my colon
and then a comma
and then key
dot
the code.
All right.
So this right here, this particular function here is what we're going to use
to show the key as a string.
Now,
next step will be to actually encrypt the message.
So what do we do?
We can say message
equals, let's add
the message. The default here says hello world.
Uh Let's make it a bit more complex. I'm gonna say hello world.
This is a circuit message. OK? I think this is fine. This is a circuit message. OK? So
this is the message
they were going to first of all encrypt and then decrypt. So
I will now come in here right now and say encrypted
on this call message.
Now we're going to encrypt the message and how we're going to do that.
We are going to say cipher
dot encrypt. And then
in brackets, we're gonna pass the message
and the method dot encode to encrypt a
message
so far. So good.
Next step
will now be to print,
we can print the encrypted message right now. So I'm gonna print in brackets,
let's add our codes and then I can say,
uh, encrypted
message
is colon and now
add my comma
and then simply
say encrypted underscore
message.
OK.
So far so good.
The last step, of course, after encryption will be to decrypt.
So I'm gonna say decrypted
underscore message
equals to what
cipher
dot decrypt.
And then in brackets, we're going to pass in what the encrypted
and crypted
underscore message.
And then
dot
the code
brackets
close the last bracket
and there it is.
So last step right now will just be to print
decrypted message is and then
decrypted
message. Now I can see here we do have, oh, there is no closing bracket to forgive me.
I made a mistake right there
and hopefully this should work.
Let me go ahead right now and run
and there you go.
So right now you can see on my screen, it says loading the next environment.
So the beauty about
Python when working
is that
even though this is actually an external library
is going to automatically import the library for.
So it's basically installed the library
and now we have the program running and it says
the generated key. Was this ok? That's a long key.
So the encrypted message is now this right here
very, very long as you can see.
And of course, the decrypted message is hello world, this is a secret message.
Let's try changing the message to something else.
And I'm gonna say, Python is amazing.
Let's run it again
and there you go. So now we have a different generated key and of course, a different
encrypted message. So
that's how to work with the Cryptography
library. And just to kind of give you a quick recap. Once again. First of all,
we imported the module fernet
from the library of Cryptography.
And then of course, in the world of Cryptography,
we're gonna have a key that we use for encryption and decryption.
We generated the key right here with the use of the
generate key method.
And then we assign the key to our
cipher.
We printed out the generated key by making use of the key to decode function
and then
to encrypt the message we have to, first of all add what the message actually is.
The message here is Python is amazing.
And then we say, OK, encrypted message will be equal to now the function cipher
encrypt
and then in brackets message dot encode.
So the encode method right here
will convert the plain text or our string into bytes.
OK?
And now the cipher
encrypt will take the encoded message,
it will then encrypt it producing an encrypted version.
And of course, the output will be equal to the encrypted underscore message.
And then the last step right now is to actually decrypt the message. So
cipher dot decrypt right here,
we'll take the encrypted message and then decrypt it,
we turn it back to the original format
and then the dot decode right here. Then this method
will convert the decrypted bytes
back into a readable string format.
And of course, finally, we printed out the actual uh decrypted message.
So
that's it. Thank you for watching the video. I will see you in the next class.