Use Python to create an app to encrypt files on your computer

preview_player
Показать описание
We’ll build on our prior encryption videos but this time we’ll make the file more portable. In other words, we’ll be able to use it like a regular executable to encrypt files on your system or similar systems on other computers.
In the example here, I’ll add the encryption key to the actual code, instead of reading it from a key file or generating randomly. I’m doing this so we only have one file. A more secure option might be to generate the key each time and save it somewhere. But then, you would have multiple files to manage and your key might get lost or compromised

Based on this feedback from the user, we can open the file with the name provided and create an if statement to encrypt it or, decrypt depending on their response.
Files encrypted and decrypted – Now pyinstaller.

Now, we have a working program that allows us to encrypt or decrypt any file based on the input from the user. This will work fine here in the PyCharm environment but when we copy outside of the this environment, it will not work unless the Cryptography library is installed.

So, for example, you could send this file to a friend, but you would have to tell him or her to install the Cryptography library first in order for your code to work. In addition, they may not have the Python interpreter installed on their computer or it may be a very old version, which could prevent your code from working

This doesn’t make your program very portable, so instead, we’re going to compile the code to make it work without requiring Python or any additional libraries. In other words, we are going to make this a binary file. So, when you want to encrypt a file, you simply go to the directory of that file, run your code and the file will be encrypted.

To compile the code, we are going to use another python package called, PyInstaller. Pyinstaller bundles your code and all its dependencies into one package. Very neat!

Let’s go back to our terminal in Pycharm and run the pip3 command to install pyinstaller. I did have issues installing pyinstaller with Python version 3.9 ( the latest version as of this recording) Changing to Python 3.8 or below seemed to fix the problem. This issue was only with pyinstaller…not the code itself. Maybe the Pyinstaller developers have not caught up with the latest version of Python and the issue may be resolved by the time you are running this. If not, just download an earlier version of Python and change the interpreter.


When installing pyinstaller with Pip, it’s best to use the sudo command so the package has the appropriate access. Pip is the original command that comes with Macs and Python 2.7 … an older version, so I use pip3 to ensure we are using the later version of Python. It probably doesn’t matter here because we are running in a virtual environment that Pycharm creates but is still use pip3 out of habit.

Pyinstaller takes a minute to install and after it has been successfully installed, we run pyinstaller with the name of the file that contains our code. Pyinstaller creates a distribution for our code but we only want one file so I will put the ‘onefile’ parameter in front of pyinstaller so we are only left with a single executable file for our encryption.

PyInstaller not does it magic and compiles our code.

Now, when we do a directory listing with LS, we see some new directories and files. The directory that we are interested in Dist and when we change into that directory we see a single binary file with the same name as our python file.

We can run this file to verify. Unlike Windows, Macs and Linux need you to specify the directory the file is in ..even if we are in that directory. The ./ specifies the current directory.

And there is our prompt so we can assume the code is working. I don’t want to encrypt anything here so I’m going to cancel out of this.

Now, we want this file accessible from anywhere without having to specify the directory with the ./. So a quick tip here is to simply copy the file to a directory that is already in the system’s file path…rather than adding it to the path. The /usr/local/bin is in the Mac’s system path so now this file will be accessible from any directory on the computer by just typing the file name.

If you are running this in Windows, you would probably copy to the C:\Windows directory or if you prefer, add it to the Environment Variables in the system properties. That may require a reboot.
Рекомендации по теме
Комментарии
Автор

A good and useful video, thank you Paul!

TheOlddog
Автор

how can i encrypt files that are in a directory

crazex