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
- Secure communication: Key exchange in HTTPS/TLS handshakes
- Digital signatures: Verify data integrity and source
- API authentication: JWT RS256 signature verification
- Key exchange: Securely transmit AES session keys
- Code signing: Verify software publisher identity
How to Use an Online Tool
Using DevToolkit Pro's RSA Encryption/Decryption Tool:
- Generate RSA key pair (2048/4096 bits)
- Encrypt data with public key
- Decrypt data with private key
- Create and verify digital signatures
- 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). You can handle the symmetric AES side with our AES Encrypt/Decrypt tool.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
relatedTools
Related Articles
Online HMAC Generator: Message Authentication Code Explained
Learn how HMAC (Hash-based Message Authentication Code) works. Understand HMAC-SHA256 workflows, real-world use cases, and how to generate and verify HMAC signatures online.
SHA-256 Hash Algorithm: How It Works, Applications, and Online Tools
Understand SHA-256 internals, comparison with MD5/SHA-1, and real-world applications in password storage, blockchain, and file integrity.
Online Bcrypt Hash Tool: The Gold Standard for Password Hashing
Learn how Bcrypt password hashing works. Understand cost factors, salt generation, and how to use an online tool to generate and verify Bcrypt hashes.