Secure Encryption Algorithms with Python

Today we’re going to talk about something that’s crucial for any secure application: encryption algorithms.

To start: what is encryption? It’s the process of converting data (like text or images) into an unreadable format that can only be deciphered by someone who has access to a secret key. This is important for protecting sensitive information, like passwords and financial details, from prying eyes.

Now some popular encryption algorithms in Python. One of the most commonly used libraries for this purpose is cryptography it provides a variety of tools for secure communication over networks or storage of data on disk. To install it, simply run:

# This script installs the cryptography library using the pip package manager.

# The following line uses the pip command to install the cryptography library.
pip install cryptography

# The cryptography library provides tools for secure communication and data storage.
# It is commonly used for encrypting sensitive information to protect it from unauthorized access.

# To install the library, the pip command is used with the "install" option, followed by the name of the library to be installed, which in this case is "cryptography".

Once you’ve got that set up, let’s take a look at how to use the Fernet method (which is one of the simplest and most secure encryption algorithms). Here’s an example script:

# Import the necessary module for encryption
from cryptography.fernet import Fernet
# Import the os module for storing the key in an environment variable
import os

# Generate a key for encryption/decryption
key = Fernet.generate_key()
print("Your secret key:", key)

# Store the key in an environment variable for easier access later on
os.environ['CRYPTOGRAPHY_KEY'] = key

# Encrypt some text using the generated key
message = "Hello, world!"
# Create a Fernet object using the generated key
fernet = Fernet(key)
# Convert the message to bytes and encrypt it using the Fernet object
encrypted = fernet.encrypt(bytes(message, 'utf-8'))
print("Encrypted message:", encrypted)

# Decrypt the same text using the stored key (from environment variable)
# Use the same Fernet object to decrypt the encrypted message
decrypted = fernet.decrypt(encrypted)
# Decode the decrypted message from bytes to string
print("Decrypted message:", decrypted.decode())

As you can see, this script generates a secret key for encryption/decryption and stores it in an environment variable for easier access later on. It then encrypts some text using the generated key (which is stored as bytes), and finally decrypts that same text using the stored key.

Now another popular encryption algorithm: RSA. This method uses a pair of keys one public, and one private to securely transmit data over networks or store it on disk. Here’s an example script for generating those keys and encrypting/decrypting some text using them:

# Import necessary libraries
from Crypto.PublicKey import RSA # Import RSA library for generating keys
import os # Import os library for storing keys in environment variables
import binascii # Import binascii library for converting text to hexadecimal string

# Generate a pair of public/private keys
key = RSA.generate_public_key(512) # Generates a key with 512 bits (you can adjust this value to your liking)
print("Your public key:", key.n, "and", key.e) # Print the public key values
print("Your private key:", key.d) # Print the private key value
os.environ['PUBLIC_KEY'] = str(key.n) + ',' + str(key.e) # Store the keys in environment variables (for easier access later on)
os.environ['PRIVATE_KEY'] = str(key.d)

# Encrypt some text using the public key
message = "Hello, world!" # Define the message to be encrypted
encrypted = int(binascii.hexlify(bytes(message, 'utf-8')).decode()) # Convert message to a hexadecimal string (for easier transmission over networks)
encrypted = pow(encrypted, key.e, key.n) # Encrypt the message using the public key
print("Encrypted message:", encrypted) # Print the encrypted message

# Decrypt the same text using the private key
decrypted = pow(encrypted, int(key.d), key.n) # Decrypt the message using the private key
print("Decrypted message:", binascii.unhexlify(bytes([decrypted & 0xFF for decrypted in (decrypted >> 8).to_bytes((decrypted.bit_length() + 7) // 8, 'big')]))) # Print the decrypted message in its original form

As you can see, this script generates a pair of public/private keys using the RSA algorithm with 512 bits. It then encrypts some text by converting it to hexadecimal format and raising it to the power of e (which is the public exponent). Finally, it decrypts that same text using the private key (by lowering it to the power of d which is the private exponent) and converting it back to its original form.

Two popular encryption algorithms in Python, with some practical examples for your reference. Remember: always keep your keys safe and secure, as they are crucial for protecting sensitive information from prying eyes.

SICORPS