Creating a PGP Encryption Tool With Python | by Aleksa Zatezalo | May, 2022

Get began with cybersecurity and encryption by way of code


Fairly Good Privateness, extra generally generally known as PGP, is a broadly used encryption program. PGP is used to create digital signatures, encrypt and decrypt giant volumes of information, and to extend the security of electronic mail communications.

PGP encryption makes use of a serial mixture of hashing, data compression, symmetric-key cryptography, and at last, public-key cryptography. Every step makes use of one among a number of supported algorithms. Every public key’s certain to a username or an electronic mail tackle.

Happily, a lot of PGPs encryption protocols can be found to the general public for developer use by way of an open supply Python library — pgpy. Discovering this library led me down one other Saturday afternoon spiral of open supply exploration and the creation of yet one more small applet — a command line encryption instrument utilizing PGP.

Command line snippet of my code

Under, I’ll stroll you thru the fundamental steps of making a primary applet within the terminal PGP encryption instrument. A primary understanding of file streams and programming is required.

Creating the Command Line Applet

To start out, we are going to code 5 primary features that play a vital function in creating our software program which can present the next performance: getting and setting a key, studying and writing the important thing to a selected file, and producing a key to start with.

As a result of getting, setting, studying, and writing are primary features, they are going to be included within the gist under with out clarification. The genKey operate is a bit more fascinating. Though it’s pgpy “boilerplate,” it’s price exploring.

Line 42 within the gist under generates a pair of private and non-private keys of measurement 4096 utilizing the RSA protocol, whereas the following command at line 45 binds it to a fictional electronic mail — Abe Lincoln’s on this case — as required within the introduction.

Line 50 permits us to specify the entire spectrum of use circumstances (electronic mail encryption, disk encryption, and so on.) and predefined hashes, cyphers, and compression algorithms used to encrypt paperwork. No choices or flags have been eliminated within the following case:

Our subsequent step entails combining pgpy’s encryption instructions with Python’s file stream calls. 4 features are wanted: pgp_encrypt to encrypt textual content; pgp_decrypt to decrypt (clearly); encrypt_file, and decrypt_file. Capabilities concerning the encryption and decryption of recordsdata will merely use the encryption instructions and file stream performance together. It’s seen under:

Now stringing all the pieces collectively in a enjoyable most important command as seen under will give us a functioning piece of encryption software program.


Though this exploration of PGP wasn’t terribly in-depth, it’s an fascinating place to begin for newbie programmers and people concerned with cyber safety and encryption.

Exploring open supply software program and utilizing it to create fascinating little initiatives is a good way to boost your understanding of a subject. I might strongly encourage these concerned with cryptography or cyber safety to observe an identical vein and take a look at comparable initiatives for themselves.

More Posts