RSA Encryption Tool

Generate RSA key pairs, encrypt and decrypt data using public/private key cryptography

Key Size

Larger keys provide better security but are slower to generate and use

Output Format

Encryption Padding

OAEP provides stronger security than PKCS#1 v1.5

Hash Algorithm

Public Key

Copied!
Note: RSA encryption has a message size limit based on key size. For large data, consider using hybrid encryption (RSA + AES).
Error message

Decryption Padding

Must match the padding used during encryption

Hash Algorithm

Private Key

Copied!
Error message

Understanding RSA Encryption

RSA (Rivest–Shamir–Adleman) is one of the most widely used asymmetric cryptographic algorithms in the world. Unlike symmetric encryption algorithms like AES that use the same key for both encryption and decryption, RSA uses a pair of mathematically linked keys: a public key for encryption and a private key for decryption.

RSA at a Glance

  • Invented: 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman
  • Type: Asymmetric (public key) cryptography
  • Key Pairs: Public key (for encryption) and Private key (for decryption)
  • Common Key Sizes: 1024, 2048, 3072, or 4096 bits
  • Security Basis: The difficulty of factoring the product of two large prime numbers
  • Primary Uses: Digital signatures, key exchange, secure communications

The History of RSA

The RSA algorithm was publicly described in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman at MIT, whose surnames form the acronym "RSA". However, a similar system was secretly developed in 1973 at GCHQ (the British intelligence agency) by Clifford Cocks, but this work was classified until 1997.

RSA was one of the first practical public-key cryptosystems and is widely used for secure data transmission. The algorithm was patented in the United States until the patent expired in 2000, which contributed to its widespread adoption and implementation in various security protocols, including HTTPS, SSH, and many VPN systems.

RSA Encryption Concept
The concept of RSA encryption using public and private keys

How RSA Encryption Works

RSA encryption is based on a fundamental mathematical property: it's easy to multiply two large prime numbers together, but extremely difficult to determine the original prime factors if you only have their product (known as the "factoring problem").

1

Key Generation

  1. Choose two distinct large prime numbers, p and q
  2. Compute n = p × q (the modulus)
  3. Calculate φ(n) = (p-1)(q-1) (Euler's totient function)
  4. Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1
  5. Calculate d such that d × e ≡ 1 (mod φ(n))

The public key is (n, e) and the private key is (n, d). The values p, q, and φ(n) must be kept secret.

2

Encryption

To encrypt a message M, the sender:

  1. Obtains the recipient's public key (n, e)
  2. Represents the message as an integer m (0 ≤ m < n)
  3. Computes the ciphertext c = me mod n

The ciphertext c is then transmitted to the recipient.

3

Decryption

To decrypt the ciphertext c, the recipient:

  1. Uses their private key (n, d)
  2. Computes the plaintext m = cd mod n
  3. Converts m back to the original message M

RSA Key Sizes and Security

The security of RSA depends primarily on the length of the keys. Longer keys provide better security but require more computational resources to process. The following table outlines different key sizes and their security levels:

Key Size Security Level Recommended Use Performance
1024 bits Low Legacy systems only, not recommended for new applications Fast, but insecure
2048 bits Medium Current standard for general-purpose encryption (as of 2025) Good balance of security and performance
3072 bits High Sensitive data and long-term security Slower than 2048-bit keys
4096 bits Very High Highly sensitive data and long-term security concerns Significantly slower operations

Security Considerations

As computing power increases, the minimum recommended key size also increases. Keys considered secure today may become vulnerable in the future. Additionally, quantum computers pose a theoretical threat to RSA encryption through Shor's algorithm, which could efficiently factor large numbers.

RSA Padding Schemes

RSA on its own is deterministic, meaning the same plaintext always encrypts to the same ciphertext with a given key. This vulnerability is addressed by padding schemes, which add randomness to the encryption process:

PKCS#1 v1.5

Description: The original padding scheme defined in PKCS#1.

Structure: Adds a block format, random padding, and a separator before the data.

Security: Vulnerable to certain attacks (like Bleichenbacher's attack) when used for encryption in TLS.

Use Cases: Still commonly used for digital signatures, but not recommended for new encryption implementations.

OAEP (Optimal Asymmetric Encryption Padding)

Description: A modern padding scheme providing security against chosen-ciphertext attacks.

Structure: Uses hash functions to create a secure padding based on random input.

Security: Provides semantic security and is provably secure in the random oracle model.

Use Cases: Recommended for all new RSA encryption implementations.

PSS (Probabilistic Signature Scheme)

Description: A specialized padding scheme designed specifically for digital signatures.

Structure: Uses a salt to randomize signatures even for identical messages.

Security: Provides stronger security guarantees than PKCS#1 v1.5 for signatures.

Use Cases: Modern digital signature implementations.

RSA Message Size Limitations

One important limitation of RSA is that it can only encrypt data that is smaller than the key size. For example, with a 2048-bit key, the maximum size of data that can be encrypted is approximately 245 bytes (2048 bits ÷ 8 - padding overhead). This limitation exists because:

  1. The mathematical operation requires the plaintext (as a number) to be less than the modulus n
  2. Padding schemes require additional space, further reducing the available space for actual data

Maximum Message Size by Key Size (with OAEP Padding)

  • 1024-bit RSA: ~86 bytes
  • 2048-bit RSA: ~214 bytes
  • 3072-bit RSA: ~342 bytes
  • 4096-bit RSA: ~470 bytes

Because of this size limitation, RSA is rarely used to directly encrypt large messages. Instead, it's typically used in a hybrid encryption scheme:

  1. Generate a random symmetric key (e.g., for AES)
  2. Encrypt the actual message with the symmetric key
  3. Encrypt only the symmetric key with RSA
  4. Transmit both the RSA-encrypted symmetric key and the symmetrically encrypted message

RSA Key Formats

RSA keys can be stored in various formats. The most common formats include:

PEM (Privacy Enhanced Mail)

A Base64 encoded format with header and footer lines, commonly used in OpenSSL and many other cryptographic tools.

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...
-----END PUBLIC KEY-----

Advantages: Human-readable, widely supported, can be easily copied and pasted.

JWK (JSON Web Key)

A JSON-based format commonly used in web applications and APIs, especially with JWT (JSON Web Tokens).

{
  "kty": "RSA",
  "n": "0vx7agoebGcQSuuPiLJXZ...",
  "e": "AQAB",
  "alg": "RS256"
}

Advantages: Integrates well with JSON-based systems, directly usable in web browsers via Web Crypto API.

XML

An XML-based format often used in .NET applications and XML-based security standards.

<RSAKeyValue>
  <Modulus>xA7SEU+e0yQH5rm9kbCDN9...</Modulus>
  <Exponent>AQAB</Exponent>
</RSAKeyValue>

Advantages: Integrates well with XML-based systems and .NET frameworks.

Common Use Cases for RSA

🔑

Digital Signatures

Using the private key to sign data, allowing anyone with the public key to verify the authenticity of the data.

🔒

Key Exchange

Securely exchanging symmetric encryption keys across unsecured channels like the internet.

🌐

HTTPS/TLS

Securing web traffic through certificate validation and establishing encrypted sessions.

📧

Secure Email

Protocols like S/MIME and PGP use RSA for encrypting email contents and providing digital signatures.

💳

Secure Transactions

Protecting financial transactions and sensitive data in banking and e-commerce systems.

📁

Document Signing

Creating legally binding electronic signatures that verify the authenticity and integrity of documents.

RSA Security Best Practices

To ensure the security of RSA implementations, consider the following best practices:

Use Adequate Key Sizes

Use at least 2048-bit keys for general purposes and 3072-bit or 4096-bit keys for highly sensitive data or long-term security.

Implement Secure Padding

Always use OAEP padding for encryption and PSS for digital signatures rather than the older PKCS#1 v1.5 padding.

Use Established Libraries

Implement RSA using well-established cryptographic libraries rather than creating your own implementation.

Hybrid Encryption for Large Data

For messages larger than what RSA can handle directly, use hybrid encryption by combining RSA with a symmetric algorithm like AES.

Secure Key Storage

Protect private keys using secure storage mechanisms. Consider using hardware security modules (HSMs) for critical applications.

Key Rotation

Periodically generate new RSA key pairs, especially for high-value systems, and have a plan for key rotation.

Future of RSA and Quantum Computing

With the advancement of quantum computers, RSA faces a significant theoretical threat. Shor's algorithm, when implemented on a sufficiently powerful quantum computer, could factor large numbers exponentially faster than classical computers, potentially breaking RSA encryption.

This has led to research in post-quantum cryptography (PQC) – cryptographic algorithms believed to be secure against quantum computers. Organizations like NIST are currently standardizing post-quantum algorithms that could eventually replace RSA in applications requiring long-term security.

Post-Quantum Alternatives to RSA

  • Lattice-based cryptography: CRYSTALS-Kyber, NTRU
  • Hash-based signatures: SPHINCS+, LMS
  • Code-based cryptography: Classic McEliece
  • Multivariate polynomial cryptography: Rainbow
  • Isogeny-based cryptography: SIKE

Conclusion

RSA remains a cornerstone of modern cryptography and digital security, providing a robust framework for secure communications, digital signatures, and key exchange. By understanding its principles, limitations, and best practices, you can effectively implement RSA in your security systems while preparing for future cryptographic challenges.

When implementing RSA, always consider the specific security requirements of your application, use appropriate key sizes, implement secure padding, and stay informed about developments in cryptography and potential threats to ensure your implementations remain secure for years to come.