Skip to main content

Asymmetric Encryption Example

Download Files #

You can download the python files for the asymmetric encryption example here

Unzip the folder using:

unzip asymmetric_encryption.zip

Setup Environment #

To run the code in these files you will need to install the required python packages. I recommend utilizing a virtual environment to do this:

python -m venv .venv

# If on linux/mac
source .venv/bin/activate

# If you are using windows, execute the activate.bat or activate.exe file
./.venv/bin/activate.bat 

You can then install the cryptography package:

pip install cryptography

Begin Example #

Now you can generate your public and private keys:

python generate_keys.py

You should have two files in the folder now, public.pem and private.pem. These are your keys.

You can now encrypt a message using the following command. Note that we are writing the output to a file because it does not output human-readable content.

python encrypt_message.py "This is a secret message" secret.enc

The code in this file looks for the public.pem file to perform the encryption.

To decrypt the file, we can use:

python decrypt_message.py secret.enc

This code looks for the private.pem file to perform the decryption on the ciphertext file we provided.