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).
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
SHA-256 vs MD5: When to Use Which Hash Algorithm (2026)
SHA-256 vs MD5 compared: security, speed, and use cases. Learn when MD5 is still acceptable and where you must use SHA-256. Practical decision guide.
Complete Guide to MD5 Hash: How It Works, Uses, and Security
Deep dive into the MD5 algorithm, its common uses, and security vulnerabilities. Learn the differences between MD5, SHA-1, and SHA-256, and when MD5 is still okay to use.
Is It Safe to Paste Your JWT into an Online Decoder? On Token Leakage Risk
Is it safe to paste a JWT token into an online decoder? This article analyzes the security risks of online JWT decoding, token leakage risk, and how to safely decode a JWT without exposing private data.