So welcome to the final part of the program where
we are going to create the main user interaction.
We're going to provide the user with four different options.
Maybe they would like to generate a key encrypted file, the crypto file,
maybe even just exit.
And then depending on which option they choose, we are going to have to do something.
So let's begin.
All right. So I'm going to say,
define main. This will be the main, the name of our
interface
and let us print out a general welcome message. This isn't necessary.
But hey, let's just do that. Welcome to the encryption decryption tool.
OK?
OK.
Now we're gonna create the while loop that's going to go through
the four options. I'm gonna say while and now true
call on
and now let us print out the state
the options. OK? So I'm gonna go print
and now number one generates
and encryption
key.
OK?
And now let's go with option number two, which would be to simply encrypt,
encrypt, a file.
Uh Option number three
will be what's
decrypts
file.
And now the final option,
which is the most boring option,
exit the program.
OK?
There it is. Now,
since we've,
since we now have the four options, let us prompt the user
to provide us with which option they would like to go with.
So I'm gonna come over here right now and let's go with the variable option.
So I'm gonna say option
and our equals and our input. So let's prompt the user to tell us
what
option
would you like to go with? Question mark? OK.
So let's ask the user, hey, what would you like to do
now? We'll have to create the el statements for each particular option. So
let us go
with the first one in here. So I'm gonna say if
the option
is equal to one,
what do we want to do?
Very simply, we can generate our key so I can come over here right now and simply say
generate
underscore key. And now in brackets, what's the name of the key
that is encryption
underscore key
dot key.
So we can run the function immediately, right? And this will work perfectly fine.
However,
I want to show you something this isn't necessary.
But what if we wanted to provide the user
with the option of actually using their own custom key
and not just the default key that we've already created.
So we give the user two options.
You can either use the default key which we already have or
you can provide us with the path to your own encryption key.
Now, how would we do this? Well,
I've already written the line right here.
So let me just grab this cut,
gonna come down in here and then paste and
let me explain to you what's happening in here.
So
we're gonna have to pass this into the variable file underscore path
and now input. So let's ask the user that, hey, enter the file path to save the key.
Now over here where it says default encryption on
key dot key is still part of the string.
I am just indicating to the user that hey,
if you don't provide us with your own custom key, we're going to use the default key,
which is encryption on the key key.
Now, this is very important where it says or
encryption on the key key. Now, right here
we are telling Python that hey,
shooting case,
even though the user has been provided with the option to use their own custom key.
If they want to use our own default key,
then use it. So here we said
all
and now the name of a default key.
So all we need to do right here is just to remove
the default option
and then simply pass in the file underscore path
and there you go. So that's option number one,
all laid out.
Let's move on to option number two.
I'm going to come in here
and say if
choice
equals
two
call on.
So in here
we would like to encrypt the file.
Now, remember to encrypt the file, we need three parameters.
First of all, we need to ask the user to tell us
the file, they would like to encrypt which will be the input file.
And then also
we would like to store the encrypted content.
What's going to be the name of the output file?
And then third, which key would they like to use to do the encryption? So
I'm gonna come over here right now.
Let's pass the variable input underscore choice. Oh
Do forgive me. It should be
if option,
not choice, I got carried away. Option, not choice. So
input
uh
input file
to forgive me, input underscore file.
I'm gonna say equals
and now let us prompt the user to tell us
what's the name of the file. So I'm gonna say enter the path of the file to encrypt,
let me remove this message. So
enter the path of the file to encrypt. OK? So let's prompt the user to do that
and then
outputs
underscore file is going to be equal to input. And now let us prompt the user to tell us
the name of the Apple. First, I'm gonna say enter the name
of the
of the output file
call on.
And now finally, what about the key?
So I'm gonna say key underscore path,
OK? It's going to be equal to input
and now
we can say enter the path
of the encryption
uh encryption key.
Otherwise
the defaults
will be used.
OK?
So I'm going to simplify the string statement in here.
I'm not going to indicate the name of the default K. I don't think that's necessary.
So all we have to do at this point right now is just to also say the or
encryption
uh underscore key,
the key
and there you go. Alright. So
one thing we need to do right here is we need to account for errors.
OK? What if the user, what if maybe they provided the wrong input file
or the name of the output file is incorrect or maybe even the key
isn't correct?
What should happen? So let us provide the option right now to cater for that.
So I'm gonna say try
remember that. And now
key
equals load underscore key
and now in brackets
the key path,
OK? And now
the most important parts encrypt
underscore file
and now in brackets, what are the three parameters?
We have the input underscore file, we have the output
underscore file
and now we have the key as well. So these are the three parameters
and if there's any
error, let us generate. So I'm gonna say except
Exception,
OK?
As E
and now we can print
our error message. So I'm going to use the F string
and then I will say something like
uh during
encryption,
OK? Call on and now we can use our call braces to indicate
what the particular error is. And There you go.
So we have successfully created option number two with a
user to provide the path of the file.
They want to encrypt the name of the output file and then also the key as well.
And we've also added some error handling. Fantastic.
Now,
all I'm going to do
is I'm just gonna grab this block of code
and I'm simply going to paste it because think about it, the encryption
and decryption process is almost the exact same thing is just opposite, right? So
I'm gonna copy all the block of code
and I'm gonna come over here
and now I am going to paste it.
OK?
Now notice though that the indentation isn't correct. So make sure
that when you do the exact same thing as I did,
make sure that your indentation is correct,
make sure everything is aligned properly and this should be fine. OK?
All we need to do right now is to change the options in here.
So option number three to number two
input file, I'm gonna say enter the path of the file to what to decrypt.
OK?
An output file,
enter the name of the app file, that's fine.
Enter the path of the encryption key otherwise. OK?
Keep in mind that the encryption key is the exact same thing as the decryption key.
So we don't need to change the text in here.
You could change it if you want to, but I'm just gonna leave it as it is.
And finally, OK, over here as well, we just need to change this
and there you go, error handling is intact.
And one more thing we need to change is going
to be over here where it says the encrypt file
over here. It should be a decrypt
file. OK? So you want to make sure
that whenever you copy and paste code and you need to alter some text,
make sure that you go through it properly. Otherwise
you may end up having some issues.
Ok?
All we have to do right now is to add the fourth option. So
I'm going to come in here and say
option equals four,
we can print in
the program
and now we're going to have the break statement as well because remember
that even though we provided four options to the user,
they might become quite silly and decide to choose option
number five or six or seven or any other option
that doesn't even exist. So we need to account for that as well.
I'm gonna say break and then
we're going to provide the final else statement
and now we can simply say print and then
tell the user,
please choose a valid option
and there you go
and there it is. And now all we got to do is to run
the main function
and there it is. So
hopefully the program should work.
Uh
let me go ahead right now and
run the program.
And there you go. Ok? So
let us test this out. I'm gonna go, first of all with option number one
press enter,
I'm gonna go with the default. So I'm gonna press enter again
and there you go. Awesome.
So you can see right now we do have the encryption key that's been created
by the program.
So now let us try to encrypt a file.
So I'm gonna come over here right now and let's create a sample
dot TXT file. Let's add some text in here. This is some sample text
which we are going to encrypt
and also decrypt.
OK? So let's see. I'm gonna go ahead right now and go to the program.
Let's go with option number two right now.
So let us try to encrypt our sample
dot TXT file.
I'm gonna press enter
and now what's going to be the name of the output file. Let's call this the encrypted
underscore file dot TXT.
Let's go. We're gonna use the default key. So I'm gonna press enter
and there you go. So let's take a look. We have the encrypted underscore
FDO TXT
and there it is, this is the encrypted version
of our sample text file. So now let us try to decrypt the encrypted file
and see if we'll get back the same text. I'm gonna go with option number three,
enter the path of the file to decrypt is going to be encrypted
underscore file
dot TXT.
And that will be the name of the
output file. We can call it decrypted
underscore file
dot TXT.
We're gonna go with the default key
and the 0000, I'm sorry. We have an error message that says
no such file. The director encrypted
file dot TXT. What did I do wrong?
Uh We do have, oh.
Hm.
Do you see what I did?
My file is called encrypted, not encrypted.
I do apologize. I need to get myself a new pair of glasses.
I do apologize.
Let's try one of that again. So option number three.
So the file here is encrypted
uh underscore file dot TXT.
And now the name of the output file,
I'm gonna go with decrypted
and let's go file
dot txt
default key and there you go. Ok. So now let's take a look at the decrypted file
and there you can see it work.
So first of all, we encrypted sample dot TXT.
We got our encrypted file which you can see right now and then we decrypted this file
to get back the exact same text. So you can see right now it works perfectly fine.
Let's try option number four to exit the program. Ok. It exited the program.
Let's run the program one more time and let's go with option number seven.
And it says please choose a valid option and there you go. So
well,
we successfully created ourselves an encryption and decryption tool.
It's been quite a lot. But hopefully
you've learned some new concepts such as using the
right binary and read binary modes.
You've also learned how you can provide the option
for the user to use their own custom key or
use the default key.
And hopefully you learned a lot in this money project.
Thank you so much for watching and of course, I will see you in the next class.