RSA Encryption and Decryption

RSA encryption and decryption with public and private keys generation.

What is RSA Encryption?

RSA (Rivest–Shamir–Adleman) is a widely used asymmetric encryption algorithm. Unlike symmetric encryption, RSA uses a pair of keys: a public key for encryption and a private key for decryption. It's a foundational technology for securing digital communications, especially over the internet.

Key Features of RSA

Asymmetric Encryption

Uses two keys — public and private.

Based on Number Theory

Relies on the difficulty of factoring large prime numbers.

Key Lengths

Commonly 2048 or 4096 bits.

Slower

Not ideal for bulk data encryption.

Common Uses

Secure Key Exchange

Exchanging symmetric keys securely.

Digital Signatures

Verifying authenticity and integrity of data.

SSL/TLS (HTTPS)

Securing web communications.

Email Encryption

Securing email content (e.g., PGP, S/MIME).

How RSA Works

RSA uses a pair of mathematically linked keys. Here’s a simplified view of the process:

  1. Generate two large prime numbers (p and q).
  2. Compute n = p × q and ϕ(n) = (p - 1)(q - 1).
  3. Choose public exponent e such that 1 < e < ϕ(n).
  4. Calculate private exponent d such that d × e ≡ 1 (mod ϕ(n)).
  5. Publish {e, n} as the public key and keep {d, n} private.

To encrypt, the sender uses the recipient’s public key. To decrypt, the recipient uses their private key.

Encryption vs. Signing

Encryption
  • Encrypt with the recipient's public key
  • Decrypt with the recipient's private key
  • Used for confidentiality.
Digital Signatures
  • Sign with your private key
  • Verify with your public key
  • Used for authenticity and integrity.

Limitations of RSA

  • Slower than symmetric algorithms like AES.
  • Not suitable for encrypting large files directly.
  • Quantum computers could eventually break RSA.

RSA vs Symmetric Encryption

FeatureRSAAES
TypeAsymmetricSymmetric
Keys UsedPublic & PrivateSingle Shared Key
Speed🐢 Slow⚡ Fast
Best ForKey exchange, signaturesBulk data encryption
Security BasisPrime factorizationSubstitution & permutation