Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

5 Powerful Cryptography Libraries To Enhance Your App’s Security!

5 ways to enhance your apps security

In this article, you will learn what cryptography is, the different types of cryptographic algorithms, how to use python for cryptography; how to use python windows gui-builder to add features and functionalities to python; how to use python libraries to perform cryptographic tasks, and much more.

What is Cryptography?

According to Kaspersky, Cryptography is the study of communications security techniques that allow only the sender and intended recipient of a message to view its contents.

Cryptography is closely associated with encryption, which is the act of scrambling ordinary text into what’s known as ciphertext and then back again upon arrival.

Modern cryptography exists at the intersection of the disciplines of mathematics, computer science, electrical engineering, communication science, and physics. Applications of cryptography include electronic commerce, chip-based payment cards, digital currencies, computer passwords, military communications, etc.

3 Types of Cryptographic Algorithms

According to Kessler, 2021, these are the three types of Cryptographic Algorithms:

  1. Secret Key Cryptography (SKC): Uses a single key for both encryption and decryption; also called symmetric encryption. Primarily used for privacy and confidentiality.
  2. Public Key Cryptography (PKC): Uses one key for encryption and another for decryption; also called asymmetric encryption. Primarily used for authentication, non-repudiation, and key exchange.
  3. Hash Functions: Uses a mathematical transformation to irreversibly “encrypt” information, providing a digital fingerprint. Primarily used for message integrity.

Why use Python for Cryptography?

  1. Python provides powerful Cryptographic libraries as we will explore them in this article.
  2. Python is good for rapid prototyping.

Read more here, to see “How Python is Ideal for Solving Mathematically Heavy Problems”:

Delphi adds powerful GUI features and functionalities to Python

In this tutorial, we’ll build Windows Apps with extensive cryptographic capabilities by integrating Python’s Computer Vision libraries with Embarcadero’s Delphi, using Python4Delphi (P4D).

P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python Computer Vision applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.

We’re going to cover the following…

How to use hashlib, hmac, secrets, PyCryptodome, and One-Time-Pad Python libraries to perform Cryptographic tasks

All of them would be integrated with Python4Delphi to create Windows Apps with Cryptographic capabilities.

What are the pre-requisites for using the cryptography libraries?

Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.

Time to get started!

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

Open Demo01dproj

1. How do you perform cryptographic tasks with hashlib?

The hashlib module defines an API for accessing different cryptographic hashing algorithms. To work with a specific hash algorithm, use the appropriate constructor function or new() to create a hash object. From there, the objects use the same API, no matter what algorithm is being used.

Since hashlib is “backed” by OpenSSL, all of the algorithms provided by that library are available, including:

Some algorithms are available on all platforms, and some depend on the underlying libraries.

This section will guide you to combine Python4Delphi with the hashlib library, inside Delphi and C++Builder. Since hashlib is a built-in Python library, no further installation is required.

Let’s try an introductory example of hashlib, to obtain the digest of the byte string b’Hello World!’, it’s more condensed version, and try an algorithm provided by OpenSSL:

[crayon-6767ca3858e24331521734/]

Here is the result of using the cryptography library in the Python GUI

hashlib Demo with Python4Delphi in Windows

Read More:

2. How do you perform cryptographic tasks with hmac?

The hmac module implements keyed-hashing for message authentication, as described in RFC 2104. The HMAC algorithm can be used to verify the integrity of information passed between applications or stored in a potentially vulnerable location.

The basic idea is to generate a cryptographic hash of the actual data combined with a shared secret key. The resulting hash can then be used to check the transmitted or stored message to determine a level of trust, without transmitting the secret key.

This section will guide you to combine Python4Delphi with the hmac library inside Delphi and C++Builder, to create Applications of Message Signatures. Since hmac is a built-in Python library, no further installation is required.

How do I implement applications of message signatures using Python’s hmac?

Let’s try an advanced example: Applications of Message Signatures. HMAC authentication should be used for any public network service, and any time data is stored where security is important. For example, when sending data through a pipe or socket, that data should be signed and then the signature should be tested before the data is used.

Here are the steps to implement the Applications of Message Signatures:

The following is the code example of hmac and Python4Delphi to apply the Message Signatures  (Run this inside the lower Memo of Python4Delphi Demo01 GUI):

[crayon-6767ca3858e36573385862/]

Here is an example of the hmac for Applications of Message Signatures in the Python GUI:

hmac Demo with Python4Delphi in Windows

3. How do you perform cryptographic tasks with the Python secrets library?

secrets is a Python built-in library to generate secure random numbers for managing secrets. The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

The secrets module provides functions for generating secure tokens, suitable for applications such as password resets, hard-to-guess URLs, and similar tasks.

To be secure against brute-force attacks, tokens need to have sufficient randomness. Unfortunately, what is considered sufficient will necessarily increase as computers get more powerful and able to make more guesses in a shorter period. In 2015, it is believed that 32 bytes (256 bits) of randomness is sufficient for the typical use-case expected for the secrets module.

This section will guide you to combine Python4Delphi with the secrets library, inside Delphi and C++Builder, from installing secrets with pip, to create a Generator for a Hard-to-Guess Temporary URL; Containing a Security Token Suitable for Password Recovery.

After installing Python4Delphi properly, you can get secrets using pip or easy install to your command prompt:

[crayon-6767ca3858e3b342668806/]

Don’t forget to put the path where your secrets library installed, to the System Environment Variables:

System Environment Variable Examples

[crayon-6767ca3858e3f735846071/]

Let’s run this example:

How can we generate a hard-to-guess temporary URL containing a security token suitable for password recovery applications?

[crayon-6767ca3858e44550331843/]

We execute them several times to show you that all the output are unique, random, and hard to guess:

secrets Demo with Python4Delphi in Windows

4. How do you perform cryptographic tasks with PyCryptodome?

PyCryptodome is a self-contained Python package of low-level cryptographic primitives.

PyCryptodome is a fork of PyCrypto. It brings the following enhancements with respect to the last official version of PyCrypto (2.6.1):

First, here is how you can get PyCryptodome:

[crayon-6767ca3858e46709078112/]

Run the following code inside the lower Memo of Python4Delphi Demo01 GUI, to generate an RSA key. The following code generates a new RSA key pair (secret) and saves it into a file, protected by a password. We use the scrypt key derivation function to thwart dictionary attacks. In the end, the code prints the RSA public key in ASCII/PEM format:

[crayon-6767ca3858e48114275740/]

PyCryptodome example for generating an RSA Key

PyCryptodome Demo with Python4Delphi in Windows

5. How do you perform cryptographic tasks with the One-Time-Pad library?

One-Time-Pad Python is a command-line encryption tool, which uses an encryption mechanism very similar to One-time Pad (OTP) encryption technique.

In cryptography, the one-time pad (OTP) is an encryption technique that cannot be cracked, but requires the use of a single-use pre-shared key that is no smaller than the message being sent. In this technique, a plaintext is paired with a random secret key (also referred to as a one-time pad). Then, each bit or character of the plaintext is encrypted by combining it with the corresponding bit or character from the pad using modular addition.

Here is how you can get One-Time-Pad:

[crayon-6767ca3858e4b128219691/]

The following is a basic code example of One-Time-Pad to encrypt and decrypt “Embarcadero” using “a_random_key” (run this inside the lower Memo of Python4Delphi Demo01 GUI):

[crayon-6767ca3858e4d228891532/]

Here is the One-Time-Pad demo result in the Python GUI:

One Time Pad Demo with Python4Delphi in Windows

6. Conclusion

We already demonstrate 5 powerful Python libraries for Cryptography and their simple-yet powerful real-world implementation (encryption with sha224, sha256, and ripemd160 algorithm, applications of message signatures, create a generator for a hard-to-guess temporary URL, and many more). All of them wrapped well inside a powerful GUI provided by Python4Delphi. We can’t wait to see what you build with Python4Delphi!

Know more about possible alternatives to power-hungry cryptocurrencies and follow up on the next big crypto updates in this blockchain article.

In a separate post, you’ll learn about the improvements of this compiler which include pair C++ and many more.


Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Exit mobile version