Cryptography is the mathematical foundation of digital security. It guarantees confidentiality across untrusted networks, ensures message integrity against active adversaries, and verifies the authentic identity of communicators. In modern computing, secure communications rely on a hybrid model that combines high-speed symmetric ciphers with flexible asymmetric key exchange mechanisms.
In this comprehensive guide, we dissect the inner workings of symmetric block ciphers (AES-GCM), asymmetric public key cryptography (RSA and Elliptic Curve Cryptography), the Diffie-Hellman ephemeral key exchange, and X.509 Public Key Infrastructure (PKI).
Symmetric Encryption: AES and Authenticated Modes#
In symmetric cryptography, the identical shared secret key is utilized for both encryption and decryption:
Plaintext P ----[ Encrypt with Key K ]----> Ciphertext C
Ciphertext C ---[ Decrypt with Key K ]----> Plaintext PThe Advanced Encryption Standard (AES)#
Standardized by NIST in 2001 (FIPS 197), AES is a substitution-permutation network (SPN) operating on 128-bit blocks of data:
- Key Sizes: 128, 192, or 256 bits.
- Transformation Rounds: 10 rounds (128-bit key), 12 rounds (192-bit key), or 14 rounds (256-bit key).
- Core Round Operations:
1. SubBytes: Non-linear byte substitution using a precomputed lookup table (S-box) to provide confusion.
2. ShiftRows: Cyclic byte permutation to spread bytes across different columns (diffusion).
3. MixColumns: Linear algebraic transformation multiplying columns over Galois Field GF(2^8).
4. AddRoundKey: Bitwise XOR with a round key derived from the master secret via the Rijndael key schedule.
Cipher Modes: ECB vs. CBC vs. GCM#
- Electronic Codebook (ECB) - Insecure:
Each 128-bit block is encrypted independently with the same key. Identical plaintext blocks produce identical ciphertext blocks, preserving structural patterns (such as the famous ECB penguin bitmap vulnerability). Never use ECB in production.
- Cipher Block Chaining (CBC) - Legacy:
Each plaintext block is XORed with the preceding ciphertext block before encryption, using an unpredictable Initialization Vector (IV) for the first block. While secure against pattern leakage, CBC is vulnerable to Padding Oracle Attacks unless coupled with an external HMAC.
- Galois/Counter Mode (GCM) - Modern Standard:
GCM transforms the block cipher into a stream cipher by encrypting sequential counter values (IV || Counter). In addition to confidentiality, GCM computes an authentication tag using Galois field polynomial multiplication. This provides Authenticated Encryption with Associated Data (AEAD) in a single operation, protecting both privacy and integrity simultaneously.
Asymmetric Cryptography: RSA vs. ECC#
Asymmetric cryptography uses mathematically linked key pairs: a Public Key (which can be distributed publicly) and a Private Key (which must be kept secret):
- Confidentiality: Encrypt with recipient's Public Key; decrypt with recipient's Private Key.
- Digital Signatures: Sign with sender's Private Key; verify with sender's Public Key.
1. RSA (Rivest-Shamir-Adleman)#
RSA relies on the computational difficulty of factoring the product of two massive prime numbers:
- Select two large prime numbers p and q (each 1024 to 2048 bits).
- Compute modulus
n = p × qand Euler's totientφ(n) = (p - 1) × (q - 1). - Choose public exponent e (commonly 65537) such that
gcd(e, φ(n)) = 1. - Compute private exponent d such that
d × e ≡ 1 (mod φ(n)). - Encryption:
C = P^e mod n. - Decryption:
P = C^d mod n. - Key Sizes: RSA requires key lengths of at least 2048 to 4096 bits to withstand modern factor algorithms (General Number Field Sieve).
2. Elliptic Curve Cryptography (ECC)#
ECC relies on the discrete logarithm problem over the group of points on an elliptic curve defined over finite fields:
y^2 = x^3 + a·x + b (Weierstrass normal form)- Private Key: A random scalar integer d.
- Public Key: A point Q on the curve obtained by multiplying base generator point G by scalar d:
Q = d × G. - Because scalar point multiplication is easy to compute, but discovering scalar d given points Q and G (the Elliptic Curve Discrete Logarithm Problem - ECDLP) is computationally intractable, ECC provides equivalent security to RSA at dramatically smaller key sizes.
+-------------------+--------------------+------------------------+
| Security Strength | RSA Key Size (bits)| ECC Key Size (bits) |
+-------------------+--------------------+------------------------+
| 112 bits | 2048 bits | 224 bits |
| 128 bits (AES-128)| 3072 bits | 256 bits (e.g. secp256r1)|
| 256 bits (AES-256)| 15360 bits | 512 bits (e.g. Ed25519)|
+-------------------+--------------------+------------------------+Smaller keys yield faster handshakes, reduced CPU utilization on web servers, and lower packet transmission overhead.
Ephemeral Key Exchange: ECDHE#
How do two parties establish a shared symmetric secret over a public, untrusted network monitored by adversaries? They use Elliptic Curve Diffie-Hellman Ephemeral (ECDHE):
Alice Bob
Generates ephemeral private a Generates ephemeral private b
Computes public A = a × G Computes public B = b × G
--- Sends Public Key A --->
<-- Sends Public Key B ---
Computes Secret: Computes Secret:
K = a × B = a × (b × G) K = b × A = b × (a × G)
Both parties arrive at identical shared secret K = (a × b) × G!An eavesdropper observing A and B on the wire cannot compute K without solving the discrete logarithm problem.
Perfect Forward Secrecy (PFS): By using ephemeral keys generated randomly for each session and discarded immediately after handshake completion, a future compromise of the server's long-term private key does not compromise past encrypted traffic sessions.
Public Key Infrastructure (PKI) and X.509 Certificates#
While asymmetric cryptography allows secure encryption, it does not inherently prove who owns the public key. Without trust verification, an active attacker could execute a Man-in-the-Middle (MitM) attack by substituting their own public key.
Public Key Infrastructure (PKI) solves this identity problem through Certificate Authorities (CAs):
- An organization generates a private key and a Certificate Signing Request (CSR).
- A trusted Certificate Authority validates the organization's domain control and signs the public key using the CA's private key.
- The resulting X.509 Certificate binds the subject's domain name to their public key, verified by a cryptographic chain of trust terminating at a pre-installed Root Certificate Authority.
+---------------------------------------------------------------+
| Root CA Certificate |
| (Pre-installed in OS Trust Store) |
+---------------------------------------------------------------+
|
Signs and Issues
v
+---------------------------------------------------------------+
| Intermediate CA Certificate |
+---------------------------------------------------------------+
|
Signs and Issues
v
+---------------------------------------------------------------+
| Leaf (Server) Certificate |
| (devthesis.com + Public Key) |
+---------------------------------------------------------------+Checkpoint Questions & Practice Exercises#
Question 1: Why is AES-GCM preferred over AES-CBC for modern TLS 1.3 web traffic?
- Answer: AES-GCM provides Authenticated Encryption with Associated Data (AEAD). It simultaneously encrypts data and computes an authentication tag in a single, parallelizable pass. AES-CBC only encrypts data and requires a separate MAC; if implemented incorrectly (e.g., MAC-then-Encrypt instead of Encrypt-then-MAC), CBC is vulnerable to padding oracle attacks.
Question 2: Explain why Diffie-Hellman Ephemeral (DHE/ECDHE) provides Perfect Forward Secrecy while static RSA key transport does not.
- Answer: In static RSA key exchange, the client encrypts a pre-master secret using the server's static public key. If the server's private key is stolen years later, an attacker who recorded historical network traffic can decrypt all past sessions. With ECDHE, unique ephemeral keys are generated per session and destroyed from RAM immediately after the handshake, meaning past sessions cannot be decrypted even if the server is subsequently compromised.