30 Mins

7. Asymmetric Encryption (RSA Math & Key Generation)

Asymmetric Encryption Principles

Asymmetric encryption, or public-key cryptography, utilizes a mathematically related pair of keys: a public key for encryption and a private key for decryption. This solves the distribution problem of symmetric keys.

The security relies on one-way functions where it is computationally infeasible to derive the private key from the public key. While the public key is shared openly, the private key must remain secret to ensure confidentiality and provide digital signatures for integrity and non-repudiation.

Security Operator Note: Always secure your private key with a passphrase to prevent unauthorized use if the file is exfiltrated.
Text
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem

RSA Algorithm: Mathematical Foundation

RSA security is derived from the difficulty of factoring the product of two large prime numbers. We define n = p * q. The totient phi(n) = (p-1)(q-1) is crucial for deriving the private exponent.

The public exponent e is typically 65537 (0x10001). The private exponent d is calculated as the modular multiplicative inverse of e modulo phi(n). This ensures that (m^e)^d mod n = m, allowing for the decryption of ciphertext.

This hardness means that even with the public key (n, e), an attacker cannot compute d without knowing the factors p and q.

Security Operator Note: For production, use primes of at least 2048 bits to resist modern factorization algorithms like the General Number Field Sieve.
Python 3
def gcd(a, b):
    while b: a, b = b, a % b
    return a

def modInverse(e, phi):
    return pow(e, -1, phi)

p = 61
q = 53
n = p * q
phi = (p-1) * (q-1)
e = 17
d = modInverse(e, phi)
print(f'Public Key (n={n}, e={e}), Private Key d={d}')

RSA Padding Schemes: PKCS#1 v1.5 vs OAEP

Raw RSA is insecure because it is deterministic. Padding adds randomness to prevent attacks like chosen-ciphertext attacks.

SchemeDescription / Details
PKCS#1 v1.5Legacy padding, vulnerable to Bleichenbacher oracle attacks. Should be avoided.
OAEPOptimal Asymmetric Encryption Padding. Uses Feistel network and hashing to achieve security against chosen-ciphertext attacks.
Security Operator Note: Always use OAEP for modern applications; treat PKCS#1 v1.5 only as a legacy necessity.
Python 3
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes

# OAEP is the modern standard
pad = padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)

Managing RSA Keys in Python

Modern Python library cryptography allows for robust key generation and serialization. Keys can be exported to PEM (Privacy Enhanced Mail) format, which is a standard base64 encoded DER structure.

When handling keys in applications, ensure memory is cleared or keys are stored in secure hardware security modules (HSMs) or encrypted keystores.

Security Operator Note: Use cryptography.hazmat primitives to ensure implementation follows the latest security standards rather than rolling your own crypto.
Python 3
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
private_bytes = key.private_bytes(encoding=None, format=None, encryption_algorithm=None)
print('Key generated successfully')

Knowledge Check

Ready to test your understanding of 7. Asymmetric Encryption (RSA Math & Key Generation)?

Back