6 Public Key Encryption and Digital Signatures: Unleashing RSA
This chapter gives developers a practical, code-first tour of public key cryptography with a focus on RSA for both encryption and digital signatures. It motivates the need by showing how symmetric keys don’t scale across many services and create risky operational burdens (distribution, storage, and rotation). The remedy is to give each service its own public–private key pair, keep private keys protected (for example, in a vault), and share public keys freely. Because adversaries can tamper with keys in transit, the chapter emphasizes that public keys must be distributed via trusted digital certificates to bind keys to identities and prevent substitution attacks.
After introducing the public/private key model, the chapter frames RSA’s security around the hardness of factoring large integers, notes the implications of faster factoring or future quantum capabilities, and provides concrete configuration guidance. It recommends modern key sizes (2048-bit minimum, 3072/4096-bit preferred) and strongly advises using RSA-OAEP padding instead of the legacy and vulnerable PKCS#1 v1.5 to avoid downgrade risks. For performance, it shows the standard hybrid approach: encrypt bulk data with a fast symmetric cipher like AES-GCM and “wrap” the AES key with RSA-OAEP—an approach used by protocols such as TLS, IPsec, JWE, and SSH.
The chapter then covers RSA digital signatures for integrity, authentication, and non-repudiation, contrasting them with HMACs (shared secret, faster but no non-repudiation). It clarifies key usage—private keys for decryption and signing; public keys for encryption and verification—and stresses separating key pairs for encryption/decryption and signing/verification. Through plain Java examples, it demonstrates generating key pairs, encrypting/decrypting files, and signing/verifying data, while highlighting real-world operations: expose public keys via endpoints for smooth rotation, safeguard private keys, and rely on certificates to establish trust. The result is a clear playbook for applying RSA correctly and securely in modern service-based systems.
ACME Inc. staff inspect returned merchandise and approve refunds using the warehouse management service. The payment service issues refund to customer credit card accounts.
Usually in a service-oriented system, more than one application needs to talk to others. This fact complicates the distribution of the keys.
A key pair consisting of a private and a public key. The keys are numbers that are related to each other using a mathematical equation. The private key only works with the public in the same key pair. The public key only works with the private key from the same key pair. The private key must be kept secret and the public key by shared with anyone including friends, enemies, and hackers. Security in a public key cryptosystem rest on keeping private keys private.
Each service has a unique public-private key pair, it freely shares its public key over the network to any other service that might ask for it. When two services want to communicate, they exchange public keys and use those keys to secure the communication link. If a hacker steals a private key, only the communication link using that private key is compromised. A hacker that only has access to the public keys cannot comprise the security of the system.
Hybrid encryption using a high-performance symmetric algorithm like AES to encrypt content. The AES Content Encryption Key (CEK) is then encrypted with the recipient’s RSA public key, allowing the recipient to use their private key to decrypt the CEK and then decrypt the encrypted content.
Using RSA for digital signatures. The Warehouse Service signs data with its private key, and the Payments Service (or any other service) can verify the signature using the corresponding public key. This ensures integrity and authenticity, since only the service holding the private key can create the signature.
The Warehouse service uses the public key from a key pair to encrypt the content of the refunds.json file. The Payments service uses the private key from the pair to decrypt the data it receives.
Summary
- Public key cryptography solves the key distribution problem by using mathematically related public-private key pairs where public keys can be freely shared while private keys remain secret.
- RSA public key encryption is based on the mathematical difficulty of factoring large numbers into prime factors, with security depending on this computational challenge.
- RSA keys should be at least 2048 bits today, with 3072 or 4096 bits recommended for long-term security against advancing computational power.
- RSAES-PKCS1-v1_5 is an older, insecure RSA padding scheme that should be avoided, while RSA-OAEP provides secure randomized padding and is the recommended standard.
- Hybrid encryption combines fast AES for content encryption with RSA for encrypting the AES key, providing both performance and security benefits.
- Key wrapping refers to encrypting an encryption key itself, commonly used in hybrid schemes where RSA encrypts AES content encryption keys.
- RSA digital signatures use the private key to sign data and the public key to verify signatures, providing integrity, authenticity, and non-repudiation.
- Digital signatures differ from HMACs by enabling non-repudiation since only the private key holder can create valid signatures, while anyone can verify with the public key.
- RSA signatures are slower than HMACs due to heavy mathematical operations but provide stronger guarantees including proof of authorship for legal purposes.
- Separate RSA key pairs should be used for encryption/decryption versus signing/verification to maintain security and avoid compromising either function.
- Private keys are always used for sensitive operations like decryption and signing, while public keys are used for encryption and signature verification.
- RSA encryption requires splitting large data into chunks since RSA can only encrypt data smaller than the key size minus padding overhead.
- Quantum computers could break RSA by efficiently factoring large numbers, making current key sizes inadequate against future quantum threats.
- Digital certificates are essential for validating public key authenticity and preventing man-in-the-middle attacks during key exchange.
- Sharing symmetric encryption and signing keys between different applications possess very high management costs and security risks.
- Public key cryptography algorithms use key pairs to perform encryption and signing operations.
- Key pairs have two mathematically related keys called the private and public key. Public keys are freely shared with the world it is okay for anyone to possess a copy of the public key. Private keys are kept secret and never shared with anyone.
- RSA and Ecliptic Curve Cryptography (ECC) are the two mostly widely used public key cryptosystems.
- Quantum computers will break RSA and ECC because they solve the mathematical problems RSA and ECC use much more quickly than classical computers. However, quantum computers are not powerful or widespread enough to be a practical threat.
- Key warping describes a widely used technique where a content encryption key is used to encrypt some content. Then the content encryption key is itself encrypted to protect the content encryption key during transit or storage.
- A hybrid encryption scheme combines symmetric and public key cryptography. A symmetric cipher such as AES is used to encrypt content. Then, the AES content-encryption key is wrapped by encryption using RSA.
- RSA signatures use the private key to sign and the public key to verify. This ensures that only the private key holder can create a valid signature, while anyone can check it with the public key.
- Digital signatures provide integrity, authenticity, and non-repudiation. With HMAC, you get integrity and authentication, but not non-repudiation since both sides share the same secret.
- RSA signing and RSA encryption use different key pairs. One pair should be used for encryption/decryption and another for signing/verification to avoid weakening security.
- RSA signatures are slower than HMACs. They rely on heavier math, so they are often reserved for cases where non-repudiation or public verification is required.
- Always consult with your Information Security team to ensure you use corporate recommended configurations of common cryptographic algorithms.
FAQ
What problem does public key cryptography solve for developers?
It solves the secret key distribution problem. Instead of sharing the same symmetric key across many services (which is hard to manage and risky), each service keeps a private key secret and shares only its public key, enabling secure communication without placing secrets in config files.How do RSA public and private keys differ and how are they used?
An RSA key pair consists of a public key (freely shared) and a private key (kept secret). Anyone can encrypt with the public key, but only the private key holder can decrypt. For signatures, the private key signs and the public key verifies. Security depends on keeping the private key secret.Why are digital certificates needed when sharing public keys?
Public keys can be intercepted or replaced by attackers. Digital certificates bind a public key to an identity and are signed by a trusted authority, providing authenticity and tamper protection so you can trust that a received public key belongs to the claimed service.Which RSA padding should I use: PKCS#1 v1.5 or OAEP?
Use RSA-OAEP. PKCS#1 v1.5 is older and vulnerable to chosen-ciphertext and downgrade attacks. OAEP adds randomized padding and stronger security properties and is the modern, recommended choice. Disable PKCS#1 v1.5 wherever possible.What RSA key sizes are recommended today?
2048-bit remains acceptable today, but 3072-bit or 4096-bit is recommended for stronger, longer-term security. Follow your organization’s cryptographic standards for minimum sizes.Why is RSA commonly combined with AES (hybrid encryption), and how does it work?
RSA is computationally expensive and slow for large data, while AES is fast. Hybrid encryption uses AES to encrypt the content and RSA to encrypt (wrap) the AES content-encryption key (CEK). Steps: generate a random AES key; encrypt data with AES (e.g., AES-GCM); encrypt the AES key with RSA-OAEP; send both the ciphertext and wrapped key. The receiver unwraps the AES key with the RSA private key, then decrypts the data with AES.When should I use digital signatures (RSA) vs HMAC?
- Keys: HMAC uses a shared secret; RSA signatures use a private/public key pair.- Verification: HMAC can be verified only by parties with the secret; RSA signatures can be verified by anyone with the public key.
- Non-repudiation: Not possible with HMAC; achievable with RSA signatures (only the private key holder could have signed).
- Performance: HMAC is very fast; RSA signatures are slower but provide stronger trust and distribution properties.
How do I encrypt, decrypt, sign, and verify with RSA in Java (JCA)?
- Key generation: KeyPairGenerator.getInstance("RSA"), initialize with key size and SecureRandom.- Encryption/decryption: Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); init with public key for encrypt, private key for decrypt. Encrypt/decrypt in chunks based on modulus size and padding limits.
- Sign/verify: Signature.getInstance("SHA256withRSA"); initSign(privateKey) to sign; initVerify(publicKey) to verify.
Software Security for Developers ebook for free