Online RSA Encryption/Decryption: Asymmetric Crypto and Digital Signatures
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 ToolVault'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 ToolVault. More developer tools at the homepage.
Related Tools
Related Articles
SHA256 vs MD5: What Is the Difference and Which to Use?
SHA256 vs MD5 difference, why MD5 is no longer safe, and which hash to use for file verification? Compares length, security, and use cases, with our SHA256/MD5 tools.
Can MD5 Be "Decrypted"? What Is a Collision?
People say "MD5 decrypt" — can MD5 actually recover the original? What is a collision attack and why is it dangerous? Clears the misconceptions, with our MD5 / SHA256 tools.
How to Verify File or Text Hashes Match (SHA-256 Comparison for Integrity)
Downloaded a file—how do you know it wasn't tampered with? Learn how hash verification works and step-by-step how to compare SHA-256/MD5 locally to confirm integrity.