All right. So let's begin.
And the very first thing we're going to do
is we're going to import the required library.
And that's of course, going to be the Cryptography dot fernet.
So
I'm gonna come in here and say from
and then Cryptography
does fnet now import
fernet.
OK.
Now we want to write a function
that we can use to generate and save our encryption key.
So let's go ahead and create a function.
I'm going to call this one define and let's say generate
underscore key.
And now in brackets.
So this is gonna be the name of our function,
which we're going to use to generate our encryption key.
Now, the thing is whenever you're generating your keys,
whenever you're using this function,
we can actually add a particular kind of parameter
which will be the file path, we can create the name of our file.
So what I'm gonna do right now is I'm gonna say file
and now underscore path,
OK, will now be equal. And now we can add the name of the file in
this case right now in codes, I'm going to say encryption
underscore key
dot key.
So this is how you want to save the file for your encryption key.
It's going to be the name and then dot key and I'm going to add my colon at the end, very,
very important.
So now to generate the actual key itself, you can see
it's already given us the
information I'm gonna say key
equals and now fer
A F
and now dot
generate
underscore key and now in brackets. So
this right here is what we're going to use to generate the actual key.
We're going to use this method which is the fer
to generate key
and we're going to save it in a variable called key.
So now let us create the actual file itself. So
I'm gonna come in here right now
and I'm gonna say with OK
with
and now open. So we're gonna create our file and now in brackets,
I'm going to add the file path.
OK? And now comma
and now
the right mode. So it's gonna be WB
and now I'm gonna say
key
on the score file.
So this right here will open up a file in what we call a right binary mode.
Now, you may wonder what exactly is WB,
it's similar to the right mode which will
just be w which we're already familiar with.
However,
in WB, you will write data in a binary format
instead of plain text. So basically this means it will write data as raw bytes
rather than characters. Now you might be wondering. OK.
So what exactly is the point? What is the advantage? Well,
if you write binary
data using your regular text mode, which will be w
the program will attempt to interpret the bytes
as characters which can very often result in
encoding errors, data corruption,
or maybe even loss of data integrity. So
using WB is always recommended whenever you're working with encryption,
image files or any non text data to ensure that
the data will be correctly stored in its original byte format
without any alteration.
All right. So now we'll have to write the key to the file.
So I'm going to come over here right now
and I'm gonna say key
underscore file and now the method writes
and now what are we passing in? We're passing in the actual key itself.
So this right here will write the key to the file
and then it's done, but we can also just write something to print out.
So let's just say
print.
OK. And now in brackets,
we can use our string
and I'm gonna say codes, let's say encryption
encryption key saved
two.
And now we can add the file path
underscore path
and there it is. So
we have successfully created the function for generating our encryption key.
But now we'll also write another function to load the encryption key from the file.
So
I'm going to come in here,
I'm going to say, define load underscore key.
And now in brackets, I'm going to add our file path
and now this will be equal of course to encryption key,
encryption underscore key key.
So now from here
I'm going to say
uh with
and now open
can I add the file on the score path?
And now we want to read, right? We want to read from the file.
So what do you think we're going to use, we're going to use RB
as opposed to WB. So
it's not going to be in codes,
it's going to be RB.
Let me add my comma
right there
and then close the bracket. And now as key
underscore file,
add a colon.
And now finally to with the key from the file, we can simply say key equals
and then key
underscore file dot the
read method
and now in brackets
and now we can simply return
our
key.
So the function we've just created right here,
the load underscore key will simply read and
return the decryption key stored in the file.
And of course,
this is going to be the key that will be needed for both encryption and decryption.
So join me next video where we're going to go ahead right now
to write functions to encrypt a file and also to decrypt the file.