Skip to main content

Lesson 3 of 10

Asymmetric Encryption & Key Exchange

Asymmetric encryption (public-key cryptography) uses a mathematically linked key pair: a public key (shareable with anyone) and a private key (kept secret). It solves the key distribution problem of symmetric encryption and enables digital signatures.

55 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

How Asymmetric Encryption Works

How Asymmetric Encryption Works
# Public/Private Key Pair Flow

Key Generation:
  Alice generates: public_key (shareable) + private_key (secret)

Encryption (anyone can encrypt for Alice):
  Bob wants to send a secret to Alice
  Bob encrypts with Alice's PUBLIC key → ciphertext
  Only Alice's PRIVATE key can decrypt → plaintext

Digital Signature (only Alice can sign):
  Alice signs message with her PRIVATE key → signature
  Anyone can verify with Alice's PUBLIC key
  → Proves Alice created it (authentication + non-repudiation)

Why not use asymmetric for all data?
  RSA-2048 is ~1000x slower than AES-256
  Solution - Hybrid encryption (used in TLS, PGP, SSH):
  1. Generate random AES key (symmetric)
  2. Encrypt data with AES key (fast)
  3. Encrypt AES key with RSA public key (slow, but only once)
  4. Send: RSA-encrypted AES key + AES-encrypted data

RSA & Elliptic Curve Cryptography

RSA & Elliptic Curve Cryptography
# RSA Key Generation and Encryption in Python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization

# Generate RSA-2048 key pair
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048
)
public_key = private_key.public_key()

# Serialize public key (share this with anyone)
pub_pem = public_key.public_bytes(
    serialization.Encoding.PEM,
    serialization.PublicFormat.SubjectPublicKeyInfo
)
print(pub_pem.decode())

# Encrypt with public key (OAEP padding - secure)
message = b"Secret AES key: " + b"A" * 16   # Encrypting a symmetric key
ciphertext = public_key.encrypt(
    message,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

# Decrypt with private key
decrypted = private_key.decrypt(
    ciphertext,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)
print(f"Decrypted: {decrypted}")

Diffie-Hellman Key Exchange

  • DH solves the key distribution problem: two parties derive the same shared secret over an insecure channel without ever transmitting the secret itself
  • Analogy: Alice and Bob each mix their private color with a shared public color - they exchange the mixes, then each adds their private color to get the same final mixture. An observer sees only the intermediate mixes, not the final secret
  • ECDH (Elliptic Curve Diffie-Hellman): Modern variant using elliptic curves - provides equivalent security to RSA with much smaller keys (ECDH-256 ≈ RSA-3072 security)
  • ECDHE (with Ephemeral keys): Generates a fresh DH key pair for every TLS session - provides Perfect Forward Secrecy (PFS). Used in TLS 1.3
  • Without PFS: If a server's private key is compromised, all past recorded TLS sessions can be decrypted retrospectively. With ECDHE, each session has its own ephemeral key

Common Mistakes

  • Using PKCS#1 v1.5 padding for RSA encryption - vulnerable to Bleichenbacher's padding oracle attack. Always use OAEP padding
  • RSA-1024 - considered broken for long-term security. Use RSA-2048 minimum or prefer elliptic curve (Ed25519, P-256)
  • Encrypting large payloads directly with RSA - RSA can only encrypt data smaller than the key size; use hybrid encryption for any payload over ~200 bytes
  • Storing private keys without a passphrase - private key files should always be encrypted with a strong passphrase

Tip

Tip

Practice Asymmetric Encryption Key Exchange in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).

Practice Task

Note

Practice Task - (1) Write a working example of Asymmetric Encryption Key Exchange from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Asymmetric Encryption Key Exchange is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.

Key Takeaways

  • Asymmetric encryption (public-key cryptography) uses a mathematically linked key pair: a public key (shareable with anyone) and a private key (kept secret).
  • DH solves the key distribution problem: two parties derive the same shared secret over an insecure channel without ever transmitting the secret itself
  • Analogy: Alice and Bob each mix their private color with a shared public color - they exchange the mixes, then each adds their private color to get the same final mixture. An observer sees only the intermediate mixes, not the final secret
  • ECDH (Elliptic Curve Diffie-Hellman): Modern variant using elliptic curves - provides equivalent security to RSA with much smaller keys (ECDH-256 ≈ RSA-3072 security)

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done