# SealedObject

public class **SealedObject** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
implements [Serializable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Serializable.html) This class enables a programmer to create an object and protect its
confidentiality with a cryptographic algorithm.

Given any Serializable object, one can create a SealedObject
that encapsulates the original object, in serialized
format (i.e., a "deep copy"), and seals (encrypts) its serialized contents,
using a cryptographic algorithm such as DES, to protect its
confidentiality. The encrypted content can later be decrypted (with
the corresponding algorithm using the correct decryption key) and
de-serialized, yielding the original object.

Note that the Cipher object must be fully initialized with the
correct algorithm, key, padding scheme, etc., before being applied
to a SealedObject.

The original object that was sealed can be recovered in two different
ways:

- by using the `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getObject(javax.crypto.Cipher)` method that takes a `Cipher` object.

  This method requires a fully initialized `Cipher` object,
  initialized with the
  exact same algorithm, key, padding scheme, etc., that were used to seal the
  object.

  This approach has the advantage that the party who unseals the
  sealed object does not require knowledge of the decryption key. For example,
  after one party has initialized the cipher object with the required
  decryption key, it could hand over the cipher object to
  another party who then unseals the sealed object.

- by using one of the `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getObject(java.security.Key)` methods that take a `Key` object.

  In this approach, the `getObject` method creates a cipher
  object for the appropriate decryption algorithm and initializes it with the
  given decryption key and the algorithm parameters (if any) that were stored
  in the sealed object.

  This approach has the advantage that the party who
  unseals the object does not need to keep track of the parameters (e.g., an
  IV) that were used to seal the object.

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Cipher.html`

### Field Summary

|---|---|---|
| protected byte\[\] | [encodedParams](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#encodedParams) | The cryptographic parameters used by the sealing Cipher, encoded in the default format. |

### Public Constructor Summary

|---|---|
|   | [SealedObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#SealedObject(java.io.Serializable,%20javax.crypto.Cipher))([Serializable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Serializable.html) object, [Cipher](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Cipher.html) c) Constructs a SealedObject from any Serializable object. |

### Protected Constructor Summary

|---|---|
|   | [SealedObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#SealedObject(javax.crypto.SealedObject))([SealedObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html) so) Constructs a SealedObject object from the passed-in SealedObject. |

### Public Method Summary

|---|---|
| final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getAlgorithm](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getAlgorithm())() Returns the algorithm that was used to seal this object. |
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [getObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getObject(java.security.Key,%20java.lang.String))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider) Retrieves the original (encapsulated) object. |
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [getObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getObject(java.security.Key))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key) Retrieves the original (encapsulated) object. |
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [getObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html#getObject(javax.crypto.Cipher))([Cipher](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Cipher.html) c) Retrieves the original (encapsulated) object. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Fields

#### protected byte\[\]
**encodedParams**

The cryptographic parameters used by the sealing Cipher,
encoded in the default format.


That is, `cipher.getParameters().getEncoded()`.

## Public Constructors

#### public
**SealedObject**
([Serializable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Serializable.html) object, [Cipher](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Cipher.html) c)

Constructs a SealedObject from any Serializable object.

The given object is serialized, and its serialized contents are
encrypted using the given Cipher, which must be fully initialized.

Any algorithm parameters that may be used in the encryption
operation are stored inside of the new `SealedObject`.

##### Parameters

| object | the object to be sealed; can be null. |
| c | the cipher used to seal the object. |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the given cipher is null. |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs during serialization |
| [IllegalBlockSizeException](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/IllegalBlockSizeException.html) | if the given cipher is a block cipher, no padding has been requested, and the total input length (i.e., the length of the serialized object contents) is not a multiple of the cipher's block size |
|---|---|

## Protected Constructors

#### protected
**SealedObject**
([SealedObject](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/SealedObject.html) so)

Constructs a SealedObject object from the passed-in SealedObject.

##### Parameters

| so | a SealedObject object |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the given sealed object is null. |
|---|---|

## Public Methods

#### public final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**getAlgorithm**
()

Returns the algorithm that was used to seal this object.

##### Returns

- the algorithm that was used to seal this object.

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**getObject**
([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider)

Retrieves the original (encapsulated) object.

This method creates a cipher for the algorithm that had been used in
the sealing operation, using an implementation of that algorithm from
the given `provider`.
The Cipher object is initialized for decryption, using the given
`key` and the parameters (if any) that had been used in the
sealing operation.

The encapsulated object is unsealed and de-serialized, before it is
returned.

##### Parameters

| key | the key used to unseal the object. |
| provider | the name of the provider of the algorithm to unseal the object. |
|---|---|

##### Returns

- the original object.

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the given provider is null or empty. |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs during de-serialiazation. |
| [ClassNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ClassNotFoundException.html) | if an error occurs during de-serialiazation. |
| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if the algorithm to unseal the object is not available. |
| [NoSuchProviderException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchProviderException.html) | if the given provider is not configured. |
| [InvalidKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidKeyException.html) | if the given key cannot be used to unseal the object (e.g., it has the wrong algorithm). |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if `key` is null. |
|---|---|

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**getObject**
([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key)

Retrieves the original (encapsulated) object.

This method creates a cipher for the algorithm that had been used in
the sealing operation.
If the default provider package provides an implementation of that
algorithm, an instance of Cipher containing that implementation is used.
If the algorithm is not available in the default package, other
packages are searched.
The Cipher object is initialized for decryption, using the given
`key` and the parameters (if any) that had been used in the
sealing operation.

The encapsulated object is unsealed and de-serialized, before it is
returned.

##### Parameters

| key | the key used to unseal the object. |
|---|---|

##### Returns

- the original object.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs during de-serialiazation. |
| [ClassNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ClassNotFoundException.html) | if an error occurs during de-serialiazation. |
| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if the algorithm to unseal the object is not available. |
| [InvalidKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidKeyException.html) | if the given key cannot be used to unseal the object (e.g., it has the wrong algorithm). |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if `key` is null. |
|---|---|

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**getObject**
([Cipher](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Cipher.html) c)

Retrieves the original (encapsulated) object.

The encapsulated object is unsealed (using the given Cipher,
assuming that the Cipher is already properly initialized) and
de-serialized, before it is returned.

##### Parameters

| c | the cipher used to unseal the object |
|---|---|

##### Returns

- the original object.

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the given cipher is null. |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs during de-serialiazation |
| [ClassNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ClassNotFoundException.html) | if an error occurs during de-serialiazation |
| [IllegalBlockSizeException](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/IllegalBlockSizeException.html) | if the given cipher is a block cipher, no padding has been requested, and the total input length is not a multiple of the cipher's block size |
| [BadPaddingException](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/BadPaddingException.html) | if the given cipher has been initialized for decryption, and padding has been specified, but the input data does not have proper expected padding bytes |
|---|---|