Cryptography with Python

Mauricio Trejos Gómez
4 min readFeb 26, 2023

The science and art of protecting information from unauthorized access is known as cryptography. As it has been in use for so long, cryptography is now fundamental to modern communication and data security. Due to its popularity, clarity, and abundance of cryptographic libraries, Python is a widely used programming language in the field of cryptography.

In this post, we’ll look at the fundamentals of cryptography and how to use Python to create several cryptographic methods.

Source: https://devqa.io/assets/images/encode-decode-data-python.png

Fundamentals of Cryptography

The process of converting plaintext (information that can be read by humans) into ciphertext (unreadable information) and back again is known as cryptography. Symmetric and asymmetric encryption are two categories into which cryptography can be separated.

Symmetric Encryption

Symmetric encryption, also known as shared-secret encryption, involves using the same secret key for both encryption and decryption of data. The sender uses a secret key to encrypt the message, and the receiver uses the same key to decrypt it. Examples of symmetric encryption algorithms include AES (Advanced Encryption Standard), DES (Data Encryption Standard), and Blowfish.

Source: https://www.ssl2buy.com/wiki/wp-content/uploads/2015/12/Symmetric-Encryption.png

Asymmetric Encryption

Asymmetric encryption, also known as public-key encryption, uses a pair of keys: a public key and a private key. The sender uses the recipient’s public key to encrypt the message, and the recipient uses their private key to decrypt it. RSA (Rivest–Shamir–Adleman) is an example of an asymmetric encryption algorithm.

Source: https://www.okta.com/sites/default/files/styles/tinypng/public/media/image/2021-03/asymmetric-encryption.png?itok=hn1fyqg-

Hashing

Hashing is another fundamental concept in cryptography. Hashing is a one-way function that takes input data and generates a fixed-length output, also known as a hash. The hash function is designed to be computationally infeasible to reverse, meaning that the original input data cannot be obtained from the hash. Hashing is commonly used to store passwords, verify data integrity, and ensure message authenticity.

Source: https://www.okta.com/identity-101/hashing-vs-encryption/

Implementing Cryptography with Python

Python has a rich set of libraries that can be used to implement various cryptographic algorithms. Let’s explore some examples of how to use Python libraries to implement various cryptographic techniques.

Hashing with Python

Python’s hashlib library can be used to implement various hashing algorithms. The following code demonstrates how to use the SHA-256 algorithm to hash a message:

import hashlib

message = 'Hello, World!'
hash_object = hashlib.sha256(message.encode())
hex_dig = hash_object.hexdigest()

print(hex_dig)

Output:

3e25960a79dbc69b674cd4ec67a72c62bdd802a2ab2ba9d6dea2b5a5131a1d4c

The output, usually referred to as a hash, is a fixed-length string of characters that represents the input message. It is challenging to recover the original input data from the hash since even a tiny change in the input message causes a drastically different hash.

Python Encryption and Decryption

Several encryption and decryption techniques can be implemented using Python’s libraries. Let’s look at how to construct AES encryption and decryption using Python’s cryptography package.

Encrypting:

from Crypto.Cipher import AES
import base64

# Key should be 16, 24, or 32 bytes long
key = b'Sixteen byte key'
message = "Hello World"

# Padding the message to make it multiple of 16
message = message + " "*(16-len(message)%16)

# Create an AES object with the key
cipher = AES.new(key, AES.MODE_ECB)

# Encrypt the message and encode it with base64
encrypted_message = base64.b64encode(cipher.encrypt(message.encode()))

print(encrypted_message.decode())

Output:

1YF/gxcBvfeJ6M4L6GAPZpLbU6vysaX9zTiqnulnCmA=

After implementing our AES encryption you can check that we got a complex, unreadable and long text from a simple message as “Hello world”.

Now lets take a look on how to de-encrypt the message.

Decrypting:

from Crypto.Cipher import AES
import base64

# Key should be 16, 24, or 32 bytes long
key = b'Sixteen byte key'

# Encrypted message
encrypted_message = '1YF/gxcBvfeJ6M4L6GAPZpLbU6vysaX9zTiqnulnCmA='

# Create an AES object with the key
cipher = AES.new(key, AES.MODE_ECB)

# Decode the base64 and decrypt the message
decrypted_message = cipher.decrypt(base64.b64decode(encrypted_message)).strip().decode()

print(decrypted_message)

Output:

Hello World

After using our key, we can check that the encrypted message has been resolved and now is readable information for our human understanding.

In conclusion cryptography is an important aspect of data security, and Python provides a diverse set of libraries for implementing various cryptographic algorithms. In this article, we looked at Python hashing, encryption, and decryption. It is important to note, however, that cryptography is a vast and complex field with much more to learn. It is recommended to refer to the Python documentation and other reliable sources to gain a deeper understanding of cryptography and its implementation in Python.

--

--