SealedObject Wraps Payload In Cipher
Wrap a record payload in a SealedObject under AES/GCM, then getObject with the key to reconstruct it from the stored algorithm parameters.
Code
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
public class VaultEnvelope {
record Credential(String user, String token) implements java.io.Serializable { }
public static void main(String[] args) throws Exception {
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(256);
SecretKey key = generator.generateKey();
Credential payload = new Credential("svc-deploy", "tok_9f2c");
// SealedObject wraps the payload: 对象序列化后连同 GCM 算法参数一起被密文包住
Cipher encryptor = Cipher.getInstance("AES/GCM/NoPadding");
encryptor.init(Cipher.ENCRYPT_MODE, key);
SealedObject envelope = new SealedObject(payload, encryptor);
// getObject(Key) 用封存的算法参数重建解密器,还原原始对象
Credential opened = (Credential) envelope.getObject(key);
System.out.println("user: " + opened.user());
System.out.println("roundtrip matches: " + opened.equals(payload));
}
}How this snippet works
SealedObject Wraps Payload In Cipher reduces to the classes VaultEnvelope and Credential.
What to watch for
- Nothing arrives from a registry; the language ships every API used.
Using it
The output is labeled — user:, roundtrip matches: — when the file runs.
Key statements
Credential payload = new Credential("svc-deploy", "tok_9f2c");
SealedObject envelope = new SealedObject(payload, encryptor);
Cipher encryptor = Cipher.getInstance("AES/GCM/NoPadding");
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 SealedObject Wraps Payload In Cipher print when run?
- Run it as a complete program — the output lines carry labels such as `user:`, `roundtrip matches:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.