Skip to content
crypto2026-07-253 min read

What Is RSA Encryption

RSA is the most widely used asymmetric encryption algorithm, invented by Rivest, Shamir, and Adleman in 1977. It uses a key pair: public key for encryption, private key for decryption.

How RSA Works

Key Pair Generation:
  Choose two large primes p, q
  Compute n = p × q
  Compute φ(n) = (p-1)(q-1)
  Choose e such that gcd(e, φ(n)) = 1
  Compute d such that e×d ≡ 1 (mod φ(n))

Public Key: (e, n)   ← Can be shared
Private Key: (d, n)   ← Must be kept secret

Encrypt: C = M^e mod n
Decrypt: M = C^d mod n

RSA vs AES

| Feature | RSA (Asymmetric) | AES (Symmetric) | |---------|------------------|-----------------| | Key count | Pair (public + private) | Single shared key | | Speed | Slow (1000x slower) | Fast | | Data size | Limited (< key length) | Unlimited | | Typical use | Key exchange, signatures | Data encryption | | Key length | 2048-4096 bits | 128-256 bits |

Why You Need RSA Encryption

  1. Secure communication: Key exchange in HTTPS/TLS handshakes
  2. Digital signatures: Verify data integrity and source
  3. API authentication: JWT RS256 signature verification
  4. Key exchange: Securely transmit AES session keys
  5. Code signing: Verify software publisher identity

How to Use an Online Tool

Using DevToolkit Pro's RSA Encryption/Decryption Tool:

  1. Generate RSA key pair (2048/4096 bits)
  2. Encrypt data with public key
  3. Decrypt data with private key
  4. Create and verify digital signatures
  5. Import/export in PEM format

RSA in Code

JavaScript: Web Crypto API

// Generate RSA key pair
async function generateRSAKeyPair() {
  const keyPair = await crypto.subtle.generateKey(
    {
      name: 'RSA-OAEP',
      modulusLength: 2048,
      publicExponent: new Uint8Array([1, 0, 1]),
      hash: 'SHA-256',
    },
    true,
    ['encrypt', 'decrypt']
  );
  return keyPair;
}

// Encrypt
async function rsaEncrypt(publicKey, data) {
  const encoder = new TextEncoder();
  const encrypted = await crypto.subtle.encrypt(
    { name: 'RSA-OAEP' },
    publicKey,
    encoder.encode(data)
  );
  return new Uint8Array(encrypted);
}

// Decrypt
async function rsaDecrypt(privateKey, encryptedData) {
  const decrypted = await crypto.subtle.decrypt(
    { name: 'RSA-OAEP' },
    privateKey,
    encryptedData
  );
  return new TextDecoder().decode(decrypted);
}

Python: Using cryptography Library

from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes

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

# Encrypt
ciphertext = public_key.encrypt(
    b"Hello, RSA!",
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

# Decrypt
plaintext = private_key.decrypt(
    ciphertext,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

FAQ

How to Choose RSA Key Length?

  • 2048 bits: Current standard, sufficiently secure
  • 3072 bits: Higher security requirements
  • 4096 bits: Maximum security level (lower performance) NIST recommends at least 2048 bits, 3072+ bits after 2030.

RSA vs ECC — Which Is Better?

ECC (elliptic curve) has shorter keys for the same security level (256-bit ECC ≈ 3072-bit RSA) and better performance. But RSA is more mature with better compatibility. New projects recommend ECC; existing systems keep RSA.

Why Can't RSA Directly Encrypt Large Files?

RSA has length limits (key length - padding overhead) and is slow. In practice, RSA encrypts AES keys, then AES encrypts the file (hybrid encryption).


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad