RSA OAEP Encryption With Cipher
Encrypt and decrypt payloads with RSA-OAEP via Cipher, passing an explicit OAEPParameterSpec so the digest and MGF1 hash never depend on provider defaults.
Code
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSource;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
public class SealedEnvelope {
public static void main(String[] args) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, new SecureRandom());
KeyPair pair = generator.generateKeyPair();
// 显式指定摘要与 MGF1 哈希:不依赖提供方对 OAEP 各组件的默认选择
OAEPParameterSpec spec = new OAEPParameterSpec(
"SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
Cipher encryptor = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
encryptor.init(Cipher.ENCRYPT_MODE, pair.getPublic(), spec);
byte[] plain = "small grant token".getBytes(StandardCharsets.UTF_8);
byte[] sealed = encryptor.doFinal(plain);
System.out.println("ciphertext bytes = " + sealed.length);
Cipher decryptor = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
decryptor.init(Cipher.DECRYPT_MODE, pair.getPrivate(), spec);
byte[] opened = decryptor.doFinal(sealed);
System.out.println("roundtrip ok = " + Arrays.equals(plain, opened));
// RSA-OAEP 消息必须短于模量:2048 位密钥扣掉两份 SHA-256 摘要后上限约 190 字节
try {
encryptor.init(Cipher.ENCRYPT_MODE, pair.getPublic(), spec);
encryptor.doFinal(new byte[256]);
} catch (Exception rejected) {
System.out.println("oversize rejected: " + rejected.getClass().getSimpleName());
}
}
}Inside the program logic
The working surface is the class SealedEnvelope — everything else is wiring.
Caveats
- Nothing external is involved; the standard library provides all behavior.
Putting it to use
Expect labeled output — ciphertext bytes =, roundtrip ok =, oversize rejected: — when the file runs.
Core lines
Cipher encryptor = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
Cipher decryptor = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
byte[] plain = "small grant token".getBytes(StandardCharsets.UTF_8);
Interactive Tools for This Topic
HMAC GeneratorCompute an HMAC-SHA-256, SHA-384, or SHA-512 signature for a message and key using the Web Crypto API, with hex and Base64 output.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.SHA-256 Hash GeneratorGenerate SHA-256 cryptographic hashes from text input using the Web Crypto API. Compare hashes and verify data integrity with hexadecimal digest output.JWT Signature InspectorInspect a JWT signature: decode the header and payload, show the algorithm and signature bytes, and verify HS256/384/512 HMAC signatures with a secret.
FAQ
- What does RSA OAEP Encryption With Cipher print when run?
- Run it as a complete program — the output lines carry labels such as `ciphertext bytes =`, `roundtrip ok =`, `oversize rejected:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.