Welcome back. So in the previous video, we successfully created the encryption key
and we give it the file path of encryption underscore key dot key.
And we also wrote a simple function to load the key. So now
let us write the functions which we're going to use
to actually
encrypt our files but also decrypt them.
And by the way,
I just noticed over here on line nine that I did not add my colon at the end.
So please, if you haven't done that,
I'll be sure to do that as well.
And then also on line five, I missed another column as well. So please
do add the columns to line five and line nine as well. OK. So
we are going to write a function to encrypt our file.
So I'm gonna come down here and let's say, define and
let's call the function encrypt,
encrypt
underscore file.
OK.
Now
think about this. OK.
Our function because we're going to encrypt the file,
we're going to require three different parameters.
OK?
The first parameter will be the actual file itself,
which we're going to be encrypted.
So let's call that file
the input
underscore file
OK. Now, we're also going to need to create a file
that will contain the encrypted version. So let's call that file the output
underscore file. OK? And then finally,
we're going to need the actual key,
which we're going to use to encrypt the input file, right?
So let me add my colon at the end.
So the first thing we're going to do right now is
we're going to create our fret object with the provided key.
So I'm gonna say fer
now equals
capital
F
fret.
And now in brackets,
we're going to add our key.
OK? So from here we want to read the input file.
OK?
That will be obviously,
the next step will be to actually open up the input file that we want to encrypt.
So
I'm gonna say with open and now in brackets,
I'm gonna say the inputs underscore file
and now what mode
we're gonna go with the read, binary mode. So RB,
OK?
And now I'm going to say as file
and now
what we're gonna do is we're going to read the contents of
the file and we're going to pass those contents to another variable.
So let's say let's call the variable original because I'm
gonna say original right now will be equal to what
the file dot read because we want to read the contents of the file
and let's add our buckets right there.
OK? We're doing great. We're doing great. Now, we need to encrypt the file.
Ok? And by the way, I forgot to add
the column in here on line 16. So let's add a column right there.
Ok. So now that we've opened up the file, ok, we now want to encrypt it.
So I'm gonna say,
let's call this one encrypted. Ok.
This will be the variable to store the actual encrypted version.
So I'm gonna say encrypted will be equal to.
And now fer
dot encrypt
and now in brackets because we've passed file dot readd into the variable original.
So I'm gonna come in here right now
and simply say, or
region.
OK?
And then what is the last thing we're going to do?
We would have to write the encrypted content to a file.
So
I'm gonna say with
open
and now in brackets the output
underscore file
and now the mode will be wide binary. So WB
and now I'm gonna see as file
at
our
colon right there.
And all we have to do right now is to say file dot Right,
because we're creating our file and now in brackets, what's it going to be,
it's going to be encrypted.
So I'm gonna say encrypted.
So this will write the encrypted content of the file.
And of course, we can simply add a print statement, we can say print
and uh yep, we can go with this one, prints f file, encrypted and saved to
the output file.
So from here,
what we have to do right now will be to create the function that we can use to decrypt
the file. So it's basically going to be like a reverse of what we've we've just done.
So check this out. Like I'm gonna come over here
and let's call our function decrypt
underscore file.
And then just like with encryption, we're going to need three parameters,
the input file, the output file as well as the key.
So
I'm going to add those as you can see, input file, output file and then key.
And now, as usual, I'm gonna say fer
net
equals and then capital fernet and then in brackets key.
So this will be the key that we're gonna use to decrypt our file.
So now what we want to do,
we want to read the contents of the encrypted file. So
I'm gonna come in here on a new line,
I'm gonna say with open
and now what are we opening, we're opening up the input on the
core file.
And now what mode are B
I'm gonna say
uh as file
and a colon.
So now what do we want to do? We want to decrypt. So I'm gonna say the encrypted
encrypted here
equals to file
dot read. OK.
So we're passing the contents of the encrypted file to a variable called encrypted
because now what we're gonna do is we're
going to create another variable called decrypted.
And now this will be equal to fernet
and now I'm going to pass the crypt
in brackets
encrypted.
So this right here is how we're going to decrypt the contents of our file.
And now what is the last step we want to save the decrypted information to a new file.
So now I'm simply going to say with open
and now in brackets the output
underscore file
and now WB
as file
colon
and now simply file dot Right.
And now decrypted.
So we're gonna write the
code to the file.
And then finally, we can just add the usual print statement.
So we can say something like print
our brackets F colon.
And let us say
file, decrypted and save to the
output file. I think, I think this is fine.
And there it is
actually let me improve on this one. I can say
a file,
OK?
And now call braces and the input file.
OK? And now
I would say decrypted
and saved to the output file. I think this is better
and there it is. So
once again, we created functions to both encrypt and decrypt our files.
For the encryption function. We need three parameters,
the input file, which we're going to encrypt
the file that we're going to create that will store the encrypted information,
which is output file
and then the key for the actual encryption.
So we created a key right here by infinite equals finite and then brackets key.
And now we opened up the original file
and we store the contents of the file into our
verbal called original by saying file dot read equals original.
And now to do the actual encryption itself, we set encrypted equals F
dot encrypt. And now in brackets original
and now with open because we want to store
the encrypted information on the new file, the output file,
we created the file right here, lines 1920 21.
And then we did pretty much the exact opposite with
the decryption
function. So
join me in the very next video where
we're gonna go ahead and now create the main function for the user interaction.