๐Ÿ”
๐Ÿ”‘
๐Ÿ›ก๏ธ

Public Key Cryptography

Explore the fascinating world of modern cryptographic systems

Introduction to Public Key Cryptography

Public key cryptography, also known as asymmetric cryptography, revolutionized secure communication by solving the key distribution problem. Unlike symmetric cryptography where the same key is used for encryption and decryption, public key systems use mathematically related key pairs.

1

Key Generation

Generate mathematically related public and private key pairs

2

Public Distribution

Share public key openly while keeping private key secret

3

Secure Communication

Encrypt with public key, decrypt with private key

Key Applications

  • Digital Signatures: Verify authenticity and integrity of messages
  • Key Exchange: Securely establish shared secrets over insecure channels
  • Secure Communications: Enable encrypted email, messaging, and web traffic
  • Authentication: Verify identity in digital certificates and PKI systems

Requirements for Public Key Systems

Mathematical Requirements

  • Computationally easy to generate key pairs
  • Easy to encrypt/decrypt with appropriate keys
  • Computationally infeasible to derive private key from public key
  • Computationally infeasible to decrypt without private key
๐Ÿ”— Learn more about Public Key Cryptography

RSA Algorithm

RSA (Rivest-Shamir-Adleman) is one of the most widely used public key cryptosystems, based on the computational difficulty of factoring large integers.

๐Ÿ”ง Interactive RSA Demo

Click "Generate Keys" to start the RSA demonstration

RSA Key Generation Algorithm

1

Choose Primes

Select two large prime numbers p and q

2

Calculate n

Compute n = p ร— q (the modulus)

3

Calculate ฯ†(n)

Compute ฯ†(n) = (p-1) ร— (q-1)

4

Choose e

Select e such that gcd(e, ฯ†(n)) = 1

5

Calculate d

Find d such that d ร— e โ‰ก 1 (mod ฯ†(n))

6

Key Pair

Public: (n, e), Private: (n, d)

Computational Aspects

// RSA Encryption: C = M^e mod n
function rsaEncrypt(message, publicKey) {
   const {n, e} = publicKey;
   return modularExponentiation(message, e, n);
}

// RSA Decryption: M = C^d mod n
function rsaDecrypt(ciphertext, privateKey) {
   const {n, d} = privateKey;
   return modularExponentiation(ciphertext, d, n);
}

// Efficient modular exponentiation
function modularExponentiation(base, exponent, modulus) {
   let result = 1;
   base = base % modulus;
   while (exponent > 0) {
      if (exponent % 2 === 1) {
         result = (result * base) % modulus;
      }
      exponent = Math.floor(exponent / 2);
      base = (base * base) % modulus;
   }
return result;
}

Security Analysis

RSA security depends on the difficulty of factoring large integers. Current recommendations suggest:

Key Size: Minimum 2048 bits (recommended 3072+ bits for future security)

โš ๏ธ RSA Vulnerabilities

  • Factorization Attacks: Quantum computers threaten RSA security
  • Timing Attacks: Side-channel attacks on implementation
  • Chosen Ciphertext Attacks: Padding oracle attacks
  • Small Exponent Attacks: When e is small and message is short
๐Ÿ”— Learn more about RSA

Diffie-Hellman Key Exchange

The Diffie-Hellman key exchange algorithm allows two parties to establish a shared secret key over an insecure channel without prior communication.

๐Ÿค Interactive Diffie-Hellman Demo

Click "Initialize Parameters" to start the Diffie-Hellman demonstration

๐Ÿ‘ค

Alice

Generates private key 'a'

Computes A = g^a mod p

๐ŸŒ

Public Channel

Exchanges public values

A and B are transmitted

๐Ÿ‘ค

Bob

Generates private key 'b'

Computes B = g^b mod p

Algorithm Steps

1

Public Parameters

Agree on prime p and generator g

2

Private Keys

Alice picks 'a', Bob picks 'b'

3

Public Keys

A = g^a mod p, B = g^b mod p

4

Exchange

Alice sends A, Bob sends B

5

Shared Secret

Both compute g^(ab) mod p

// Diffie-Hellman Key Exchange Implementation
class DiffieHellman {
   constructor(p, g) {
      this.p = p; // Large prime
      this.g = g; // Generator
   }

generatePrivateKey() {
   // Generate random private key
   return Math.floor(Math.random() * (this.p - 2)) + 1;
}

generatePublicKey(privateKey) {
   // Compute g^privateKey mod p
   return this.modPow(this.g, privateKey, this.p);
}

computeSharedSecret(otherPublicKey, myPrivateKey) {
   // Compute otherPublicKey^myPrivateKey mod p
   return this.modPow(otherPublicKey, myPrivateKey, this.p);
}

modPow(base, exponent, modulus) {
   let result = 1;
   base = base % modulus;
   while (exponent > 0) {
      if (exponent % 2 === 1) {
         result = (result * base) % modulus;
      }
      exponent = Math.floor(exponent / 2);
      base = (base * base) % modulus;
   }
   return result;
  }
}
๐Ÿ”— Learn more about Diffie-Hellman key exchange

Security Threats & Man-in-the-Middle Attacks

Understanding security vulnerabilities is crucial for implementing robust cryptographic systems. The Man-in-the-Middle (MITM) attack is a fundamental threat to key exchange protocols.

๐Ÿšจ Man-in-the-Middle Attack Simulation

Click "Simulate MITM Attack" to see how an attacker can intercept communications

๐Ÿ‘ค

Alice

Believes she's talking to Bob

Actually talking to Eve

๐Ÿ‘ค

Eve (Attacker)

Intercepts all communications

Establishes separate keys with both

๐Ÿ‘ค

Bob

Believes he's talking to Alice

Actually talking to Eve

Attack Scenarios

1

Interception

Attacker intercepts initial key exchange

2

Impersonation

Establishes separate sessions with both parties

3

Relay

Decrypts, reads, and re-encrypts messages

4

Control

Can modify or inject malicious content

Countermeasures

๐Ÿ›ก๏ธ Protection Strategies

  • Authentication: Use digital certificates and PKI
  • Key Verification: Out-of-band verification of public keys
  • Perfect Forward Secrecy: Use ephemeral keys for each session
  • Certificate Transparency: Monitor certificate issuance
  • Key Pinning: Pin expected public keys in applications

Other Cryptanalytic Attacks

๐Ÿ”

Brute Force

Exhaustive key search attacks

โšก

Side Channel

Timing, power, and electromagnetic attacks

๐Ÿงฎ

Mathematical

Factorization and discrete logarithm attacks

๐Ÿ”ฎ

Quantum

Shor's algorithm threatens current systems

๐Ÿ”— Learn more about MITM