Lab 06: Public Key Cryptography
🎯 Objective
Generate RSA and EC key pairs using OpenSSL, encrypt/decrypt messages, create and verify digital signatures, and understand how public key infrastructure (PKI) enables secure communication at internet scale.
📚 Background
Public key cryptography (asymmetric cryptography) solved one of the fundamental problems in cryptography: how do two parties establish a shared secret over an insecure channel without having met before? The mathematics of public key cryptography allows two keys to be mathematically linked — what one encrypts, only the other can decrypt.
RSA (Rivest–Shamir–Adleman) was invented in 1977 and remains widely used. Its security relies on the difficulty of factoring the product of two large prime numbers. A 2048-bit RSA key means the modulus is a 2048-bit number — factoring it would take longer than the age of the universe with current computers.
Elliptic Curve Cryptography (ECC) achieves the same security level as RSA with much smaller keys. A 256-bit EC key is equivalent in strength to a 3072-bit RSA key. This is why modern TLS, SSH, and mobile apps prefer ECC — smaller keys mean less CPU and memory usage.
PKI (Public Key Infrastructure) manages the certificates that bind public keys to identities. Certificate Authorities (CAs) sign certificates to vouch for their authenticity. This is how your browser trusts that google.com's public key actually belongs to Google, not an attacker.
⏱️ Estimated Time
45 minutes
📋 Prerequisites
Lab 4 (Cryptography Basics) and Lab 5 (Hashing) completed
Docker with
innozverse-cybersecimage
🛠️ Tools Used
openssl genrsa— RSA key generationopenssl ecparam— EC key generationopenssl rsautl/openssl pkeyutl— Encryption/decryptionopenssl dgst— Digital signaturesopenssl x509— Certificate inspection
🔬 Lab Instructions
Step 1: Generate an RSA 2048-bit Key Pair
📸 Verified Output:
💡 What this means: The private key is wrapped in
-----BEGIN PRIVATE KEY-----markers — this is PKCS#8 format. The public key uses-----BEGIN PUBLIC KEY-----. The "2048 bit, 2 primes" confirms our key size and that RSA used two prime numbers to generate the key. NEVER share the private key — it's your secret identity.
Step 2: RSA Encryption and Decryption
📸 Verified Output:
💡 What this means: The ciphertext is unrecognizable binary data — an attacker intercepting this cannot recover "Top secret message" without the private key. The failed decryption with a wrong key proves the mathematical binding between the key pair. In practice, RSA is only used to encrypt short messages (like symmetric keys) — for large files, use AES with RSA key wrapping.
Step 3: Create and Verify Digital Signatures
📸 Verified Output:
💡 What this means: Digital signatures work in reverse of encryption: the private key signs, the public key verifies. The 256-byte signature is 2048 bits — matching our key size. Any modification to the document (even adding a space) causes verification failure. This is exactly how APT package management works: package maintainers sign packages with their private key;
aptverifies with the stored public key before installing.
Step 4: Generate an EC Key Pair (More Efficient)
📸 Verified Output:
💡 What this means: EC-256 produces a 71-byte signature vs RSA-2048's 256-byte signature — 3.6x smaller. The private key is only 227 bytes vs RSA's ~1700 bytes. EC is mathematically based on elliptic curves over finite fields, providing equivalent security with much smaller keys. This is why modern TLS 1.3 mandates EC key exchange.
Step 5: Create a Self-Signed Certificate
📸 Verified Output:
💡 What this means: A certificate is a public key wrapped in metadata (owner, issuer, validity dates) and signed by a Certificate Authority. "Self-signed" means the certificate is signed by itself — your browser will show a security warning because there's no trusted CA vouching for it. This is fine for internal testing but never for production websites. The fingerprint lets you verify the certificate's integrity.
Step 6: Inspect a Real-World Certificate
📸 Verified Output:
💡 What this means: GitHub's certificate is signed by DigiCert — a trusted CA. The Subject says it belongs to "GitHub, Inc." The
DNS:github.comSANs (Subject Alternative Names) list valid hostnames. Your browser has DigiCert's root certificate pre-installed and uses it to verify GitHub's cert, establishing the chain of trust. If an attacker tried to intercept with a fake cert, your browser would warn you.
Step 7: Key Exchange with Diffie-Hellman
📸 Verified Output:
💡 What this means: DH key exchange allows two parties to establish a shared secret over a public channel without ever transmitting the secret itself. Modern TLS uses ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) — same concept but with elliptic curves and new keys for each session ("ephemeral"), providing perfect forward secrecy: even if the server's private key is later compromised, past sessions cannot be decrypted.
Step 8: Understand Certificate Chains
📸 Verified Output:
💡 What this means: The 2011 DigiNotar CA compromise showed the danger of trusting a single CA — attackers issued fraudulent certificates for google.com, enabling MITM attacks on Iranian users. Certificate Transparency (CT) logs now require all publicly-trusted CAs to publicly log every certificate they issue, making unauthorized certificates detectable.
Step 9: Encrypt a File with RSA + AES (Hybrid Encryption)
📸 Verified Output:
💡 What this means: This is exactly how PGP email encryption and HTTPS work: use RSA (slow) only to encrypt the tiny AES key, then use AES (fast) for actual data. The recipient only needs your public key to send you encrypted messages. You only need your private key to decrypt. This solves both the performance problem (AES is 1000x faster than RSA) and the key distribution problem.
Step 10: Security Best Practices Summary
📸 Verified Output:
💡 What this means: Key management failures are responsible for many real-world breaches. Heartbleed (2014) exposed private keys stored in OpenSSL's memory. Cloudflare accidentally disclosed private keys via Cloudbleed (2017). Let's Encrypt made 90-day certificates the norm, forcing regular rotation. Hardware Security Modules (HSMs) are tamper-resistant devices that store and use private keys without ever exposing them.
✅ Verification
📸 Verified Output:
🚨 Common Mistakes
Sharing private keys: Private keys must NEVER be shared. If you need someone to decrypt your data, they need YOUR public key, not your private key.
Using RSA for large data: RSA can only encrypt data smaller than the key size (256 bytes for RSA-2048). Use hybrid encryption for real files.
Trusting self-signed certificates blindly: Self-signed certs provide encryption but no authentication. An attacker could present their own self-signed cert claiming to be your bank.
📝 Summary
RSA uses mathematically linked key pairs: public key encrypts, private key decrypts; private key signs, public key verifies
ECC provides equivalent security to RSA with much smaller keys, making it preferred for modern TLS and mobile
PKI chains of trust allow browsers to verify that a public key truly belongs to the claimed owner, using Certificate Authorities
Hybrid encryption combines RSA (for key exchange) and AES (for bulk data) — the foundation of all HTTPS connections
🔗 Further Reading
Last updated
