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.

JavaSecurity Boundaries

Code

snippet
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

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:`.