Python Asymmetric Key Encryption

If you’re not familiar with this fancy term, let me break it down for ya: asymmetric key encryption is like having a secret handshake but instead of using physical gestures, we use math to keep our secrets safe.

Now, before we dive into the details, why you might want to use this fancy method in your Python scripts. Well, for starters, it allows us to encrypt and decrypt data using two different keys a public key (which can be shared with anyone) and a private key (which is kept secret). This means that we can share our public key with others without worrying about them being able to access our sensitive information.

So how does this work, you ask? Well, let’s take a look at some code examples:

First, let’s install the Python rsa library using pip:

# This script installs the Python rsa library using pip

# First, we need to specify the interpreter for the script
#!/bin/bash

# Next, we need to use the pip command to install the rsa library
pip install rsa

# We can also specify the version of the library we want to install by using the -v flag
pip install -v rsa

# If we want to install the library for a specific version of Python, we can use the -p flag
pip install -p python3 rsa

# We can also specify the location where we want to install the library using the -t flag
pip install -t /usr/local/lib/python3.8/site-packages rsa

# Finally, we can use the -U flag to upgrade the library to the latest version
pip install -U rsa

# And that's it! Now we have successfully installed the Python rsa library using pip.

Next, we can generate our public and private keys using the `rsa.newkeys()` method:

# Import the necessary module for RSA encryption
from Crypto.PublicKey import RSA

# Generate a new pair of RSA keys with a public exponent of 65537 and a key size of 2048 bits
private_key = RSA.generate(2048, e=65537)

# Get the public key from the generated private key
public_key = private_key.publickey()

# Note: The public key is used for encryption and the private key is used for decryption in RSA encryption.

Now that we have our keys, let’s encrypt some data using the public key:

# Encrypt a message using the public key
# Define the message to be encrypted as a byte string
message = b"Hello, world!"

# Import necessary libraries for encryption
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
from Crypto.Hash import SHA256

# Create an instance of the public key
public_key = RSA.importKey(public_key)

# Define the encryption algorithm and hash function to be used
# Use SHA256 for both the mask generation function and the message digest algorithm
mgf1 = SHA256.new()
mdalg = SHA256.new()

# Generate a random byte string of the appropriate size for the encryption
# Use the sizeinblocks function to determine the size needed for the public key
random_bytes = Random.new().read(RSA.sizeinblocks(public_key))

# Encrypt the message using the public key and the defined encryption parameters
encrypted_data = public_key.encrypt(message, PKCS1_OAEP(mgf=mgf1, mdalg=mdalg), random_bytes)

# Print the encrypted data
print(encrypted_data)

And finally, let’s decrypt the data using our private key:

# Import the necessary library for encryption and decryption
from cryptography.hazmat.primitives import serialization

# Load the private key from a file
with open("private_key.pem", "rb") as key_file:
    private_key = serialization.load_pem_private_key(
        key_file.read(),
        password=None
    )

# Decrypt the encrypted message using the private key
decrypted_data = private_key.decrypt(encrypted_data) # Use the private key to decrypt the encrypted data and store it in a variable called decrypted_data

Our secret message is now safe and sound. Asymmetric key encryption also allows us to sign messages digitally this means that we can prove that a particular message came from a specific sender without the need for physical signatures or stamps.

It’s like having your own secret handshake with math instead of gestures. Who needs physical security when we can rely on algorithms to keep our secrets safe?

SICORPS