# SecretKeySpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Inherited Constants](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#inhconstants)\|[Ctors](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#pubctors)\|[Methods](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#inhmethods)  

# SecretKeySpec

`
public


class
SecretKeySpec
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


implements

`[KeySpec](https://developer.android.com/reference/java/security/spec/KeySpec)`,

`[SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey)`


`

|---|---------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | javax.crypto.spec.SecretKeySpec |

<br />

*** ** * ** ***

This class specifies a secret key in a provider-independent fashion.

It can be used to construct a`SecretKey`from a byte array, without having to go through a (provider-based)`SecretKeyFactory`.

This class is only useful for raw secret keys that can be represented as a byte array and have no key parameters associated with them, e.g., DES or Triple DES keys.  
**See also:**

- [SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey)
- [SecretKeyFactory](https://developer.android.com/reference/javax/crypto/SecretKeyFactory)

## Summary

|                                                                                                                                                                                                                                                                                    ### Inherited constants                                                                                                                                                                                                                                                                                     |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From interface[javax.crypto.SecretKey](https://developer.android.com/reference/javax/crypto/SecretKey) |--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `long` | [serialVersionUID](https://developer.android.com/reference/javax/crypto/SecretKey#serialVersionUID) The class fingerprint that is set to indicate serialization compatibility since J2SE 1.4. |                                                                   |
| From interface[java.security.Key](https://developer.android.com/reference/java/security/Key) |--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `long` | [serialVersionUID](https://developer.android.com/reference/java/security/Key#serialVersionUID) *This constant was deprecated in API level 34. A`serialVersionUID`field in an interface is ineffectual. Do not use; no replacement.* | |

|                                                                                                                                                                                    ### Public constructors                                                                                                                                                                                     ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SecretKeySpec](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#SecretKeySpec(byte[],%20int,%20int,%20java.lang.String))`(byte[] key, int offset, int len, `[String](https://developer.android.com/reference/java/lang/String)` algorithm) ` Constructs a secret key from the given byte array, using the first`len`bytes of`key`, starting at`offset`inclusive. |
| ` `[SecretKeySpec](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#SecretKeySpec(byte[],%20java.lang.String))`(byte[] key, `[String](https://developer.android.com/reference/java/lang/String)` algorithm) ` Constructs a secret key from the given byte array.                                                                                                     |

|                                                                                                                                                         ### Public methods                                                                                                                                                         ||
|-----------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean`                                                            | ` `[equals](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Tests for equality between the specified object and this object. |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[getAlgorithm](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#getAlgorithm())`() ` Returns the name of the algorithm associated with this secret key.                                                                           |
| ` byte[]`                                                             | ` `[getEncoded](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#getEncoded())`() ` Returns the key material of this secret key.                                                                                                     |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[getFormat](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#getFormat())`() ` Returns the name of the encoding format for this secret key.                                                                                       |
| ` int`                                                                | ` `[hashCode](https://developer.android.com/reference/javax/crypto/spec/SecretKeySpec#hashCode())`() ` Calculates a hash code value for the object.                                                                                                         |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[javax.security.auth.Destroyable](https://developer.android.com/reference/javax/security/auth/Destroyable)` ` |--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------| | ` default void`    | ` `[destroy](https://developer.android.com/reference/javax/security/auth/Destroyable#destroy())`() ` Destroy this`Object`.                                | | ` default boolean` | ` `[isDestroyed](https://developer.android.com/reference/javax/security/auth/Destroyable#isDestroyed())`() ` Determine if this`Object`has been destroyed. | ||
| From interface` `[java.security.Key](https://developer.android.com/reference/java/security/Key)` ` |--------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract `[String](https://developer.android.com/reference/java/lang/String) | ` `[getAlgorithm](https://developer.android.com/reference/java/security/Key#getAlgorithm())`() ` Returns the standard algorithm name for this key.                                                     | | ` abstract byte[]`                                                             | ` `[getEncoded](https://developer.android.com/reference/java/security/Key#getEncoded())`() ` Returns the key in its primary encoding format, or null if this key does not support encoding.            | | ` abstract `[String](https://developer.android.com/reference/java/lang/String) | ` `[getFormat](https://developer.android.com/reference/java/security/Key#getFormat())`() ` Returns the name of the primary encoding format of this key, or null if this key does not support encoding. | ||

## Public constructors

### SecretKeySpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public SecretKeySpec (byte[] key, 
                int offset, 
                int len, 
                String algorithm)
```

Constructs a secret key from the given byte array, using the first`len`bytes of`key`, starting at`offset`inclusive.

The bytes that constitute the secret key are those between`key[offset]`and`key[offset+len-1]`inclusive.

This constructor does not check if the given bytes indeed specify a secret key of the specified algorithm. For example, if the algorithm is DES, this constructor does not check if`key`is 8 bytes long, and also does not check for weak or semi-weak keys. In order for those checks to be performed, an algorithm-specific key specification class (in this case:[DESKeySpec](https://developer.android.com/reference/javax/crypto/spec/DESKeySpec)) must be used.

<br />

|                                                                                                                                                             Parameters                                                                                                                                                             ||
|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key`       | `byte`: the key material of the secret key. The first`len`bytes of the array beginning at`offset`inclusive are copied to protect against subsequent modification. <br />                                                                                                                                              |
| `offset`    | `int`: the offset in`key`where the key material starts. <br />                                                                                                                                                                                                                                                        |
| `len`       | `int`: the length of the key material. <br />                                                                                                                                                                                                                                                                         |
| `algorithm` | `String`: the name of the secret-key algorithm to be associated with the given key material. See Appendix A in the[Java Cryptography Architecture Reference Guide](https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA)for information about standard algorithm names. <br /> |

|                                                                                                   Throws                                                                                                   ||
|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)             | if`algorithm`is null or`key`is null, empty, or too short, i.e.`key.length-offset<len`. |
| [ArrayIndexOutOfBoundsException](https://developer.android.com/reference/java/lang/ArrayIndexOutOfBoundsException) | is thrown if`offset`or`len`index bytes outside the`key`.                               |

### SecretKeySpec

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public SecretKeySpec (byte[] key, 
                String algorithm)
```

Constructs a secret key from the given byte array.

This constructor does not check if the given bytes indeed specify a secret key of the specified algorithm. For example, if the algorithm is DES, this constructor does not check if`key`is 8 bytes long, and also does not check for weak or semi-weak keys. In order for those checks to be performed, an algorithm-specific*key specification* class (in this case:[DESKeySpec](https://developer.android.com/reference/javax/crypto/spec/DESKeySpec)) should be used.

<br />

|                                                                                                                                                             Parameters                                                                                                                                                             ||
|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `key`       | `byte`: the key material of the secret key. The contents of the array are copied to protect against subsequent modification. <br />                                                                                                                                                                                   |
| `algorithm` | `String`: the name of the secret-key algorithm to be associated with the given key material. See Appendix A in the[Java Cryptography Architecture Reference Guide](https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA)for information about standard algorithm names. <br /> |

|                                                                        Throws                                                                         ||
|--------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if`algorithm`is null or`key`is null or empty. |

## Public methods

### equals

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean equals (Object obj)
```

Tests for equality between the specified object and this object. Two SecretKeySpec objects are considered equal if they are both SecretKey instances which have the same case-insensitive algorithm name and key encoding.

<br />

|                                Parameters                                 ||
|-------|--------------------------------------------------------------------|
| `obj` | `Object`: the object to test for equality with this object. <br /> |

|                                            Returns                                             ||
|-----------|-------------------------------------------------------------------------------------|
| `boolean` | true if the objects are considered equal, false if`obj`is null or otherwise. <br /> |

### getAlgorithm

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getAlgorithm ()
```

Returns the name of the algorithm associated with this secret key.

<br />

|                                               Returns                                                ||
|--------------------------------------------------------------------|----------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the secret key algorithm. <br /> |

### getEncoded

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public byte[] getEncoded ()
```

Returns the key material of this secret key.

<br />

|                                         Returns                                         ||
|----------|-------------------------------------------------------------------------------|
| `byte[]` | the key material. Returns a new array each time this method is called. <br /> |

### getFormat

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getFormat ()
```

Returns the name of the encoding format for this secret key.

<br />

|                                           Returns                                            ||
|--------------------------------------------------------------------|--------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the string "RAW". <br /> |

### hashCode

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int hashCode ()
```

Calculates a hash code value for the object. Objects that are equal will also have the same hashcode.

<br />

|                     Returns                      ||
|-------|-------------------------------------------|
| `int` | a hash code value for this object. <br /> |