Package javax.crypto
Class SealedObject
java.lang.Object
javax.crypto.SealedObject
- All Implemented Interfaces:
Serializable
public class SealedObject extends Object implements Serializable
A
SealedObject is a wrapper around a serializable object
instance and encrypts it using a cryptographic cipher.
Since a SealedObject instance is serializable it can
either be stored or transmitted over an insecure channel.
The wrapped object can later be decrypted (unsealed) using the corresponding key and then be deserialized to retrieve the original object. The sealed object itself keeps track of the cipher and corresponding parameters.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description protected byte[]encodedParamsThe cipher'sAlgorithmParametersin encoded format. -
Constructor Summary
Constructors Modifier Constructor Description SealedObject(Serializable object, Cipher c)Creates a newSealedObjectinstance wrapping the specified object and sealing it using the specified cipher.protectedSealedObject(SealedObject so)Creates a newSealedObjectinstance by copying the data from the specified object. -
Method Summary
Modifier and Type Method Description StringgetAlgorithm()Returns the algorithm this object was sealed with.ObjectgetObject(Key key)Returns the wrapped object, decrypting it using the specified key.ObjectgetObject(Key key, String provider)Returns the wrapped object, decrypting it using the specified key.ObjectgetObject(Cipher c)Returns the wrapped object, decrypting it using the specified cipher.
-
Field Details
-
encodedParams
protected byte[] encodedParamsThe cipher'sAlgorithmParametersin encoded format. Equivalent tocipher.getParameters().getEncoded(), or null if the cipher did not use any parameters.
-
-
Constructor Details
-
SealedObject
Creates a newSealedObjectinstance wrapping the specified object and sealing it using the specified cipher.The cipher must be fully initialized.
- Parameters:
object- the object to seal, can benull.c- the cipher to encrypt the object.- Throws:
IOException- if the serialization fails.IllegalBlockSizeException- if the specified cipher is a block cipher and the length of the serialized data is not a multiple of the ciphers block size.NullPointerException- if the cipher isnull.
-
SealedObject
Creates a newSealedObjectinstance by copying the data from the specified object.- Parameters:
so- the object to copy.
-
-
Method Details
-
getAlgorithm
Returns the algorithm this object was sealed with.- Returns:
- the algorithm this object was sealed with.
-
getObject
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyExceptionReturns the wrapped object, decrypting it using the specified key.- Parameters:
key- the key to decrypt the data with.- Returns:
- the encapsulated object.
- Throws:
IOException- if deserialization fails.ClassNotFoundException- if deserialization fails.NoSuchAlgorithmException- if the algorithm to decrypt the data is not available.InvalidKeyException- if the specified key cannot be used to decrypt the data.
-
getObject
public final Object getObject(Cipher c) throws IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingExceptionReturns the wrapped object, decrypting it using the specified cipher.- Parameters:
c- the cipher to decrypt the data.- Returns:
- the encapsulated object.
- Throws:
IOException- if deserialization fails.ClassNotFoundException- if deserialization fails.IllegalBlockSizeException- if the specified cipher is a block cipher and the length of the serialized data is not a multiple of the ciphers block size.BadPaddingException- if the padding of the data does not match the padding scheme.
-
getObject
public final Object getObject(Key key, String provider) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyExceptionReturns the wrapped object, decrypting it using the specified key. The specified provider is used to retrieve the cipher algorithm.- Parameters:
key- the key to decrypt the data.provider- the name of the provider that provides the cipher algorithm.- Returns:
- the encapsulated object.
- Throws:
IOException- if deserialization fails.ClassNotFoundException- if deserialization fails.NoSuchAlgorithmException- if the algorithm used to decrypt the data is not available.NoSuchProviderException- if the specified provider is not available.InvalidKeyException- if the specified key cannot be used to decrypt the data.
-