APIdia Launch Partner
Top Description Fields Constructors Methods
javax.crypto.spec

public Class SecretKeySpec

extends Object
implements KeySpec, SecretKey
Class Inheritance
All Implemented Interfaces
javax.crypto.SecretKey, javax.security.auth.Destroyable, java.security.Key, java.io.Serializable, java.security.spec.KeySpec
Imports
jdk.internal.access.SharedSecrets, javax.crypto.SecretKey, java.io.IOException, .InvalidObjectException, .ObjectInputStream, java.security.MessageDigest, java.security.spec.KeySpec, java.util.Arrays, .Locale

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.

Author
Jan Luehe
Since
1.4
See Also
javax.crypto.SecretKey, javax.crypto.SecretKeyFactory

Field Summary

Modifier and TypeField and Description
private final String
algorithm

The name of the algorithm associated with this key.

private byte[]
key

The secret key.

private static final long

Constructor Summary

AccessConstructor and Description
public
SecretKeySpec(byte[]
the key material of the secret key. The contents of the array are copied to protect against subsequent modification.
key
,
String
the name of the secret key algorithm to be associated with the given key material. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.
algorithm
)

Constructs a secret key from the given byte array.

public
SecretKeySpec(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.
key
,
int
the offset in key where the key material starts.
offset
,
int
the length of the key material.
len
,
String
the name of the secret key algorithm to be associated with the given key material. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.
algorithm
)

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

Method Summary

Modifier and TypeMethod and Description
pack-priv void
clear()

Clear the key bytes inside.

private static String
doSanityCheck(byte[] key, String algorithm)

public boolean

Returns:

true if the objects are considered equal, false if obj is null or otherwise.
equals
(Object
the object to test for equality with this object.
obj
)

Overrides java.lang.Object.equals.

Tests for equality between the specified object and this object.

public String

Returns:

the secret key algorithm.
getAlgorithm
()

Implements java.security.Key.getAlgorithm.

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

public byte[]

Returns:

the key material. Returns a new array each time this method is called.
getEncoded
()

Implements java.security.Key.getEncoded.

Returns the key material of this secret key.

public String

Returns:

the string "RAW".
getFormat
()

Implements java.security.Key.getFormat.

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

public int
hashCode()

Overrides java.lang.Object.hashCode.

Calculates a hash code value for the object.

private void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
stream
)

Restores the state of this object from the stream.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

algorithmback to summary
private final String algorithm

The name of the algorithm associated with this key.

keyback to summary
private byte[] key

The secret key.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides javax.crypto.SecretKey.serialVersionUID.

Annotations
@Serial

Constructor Detail

SecretKeySpecback to summary
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) should be used.

Parameters
key:byte[]

the key material of the secret key. The contents of the array are copied to protect against subsequent modification.

algorithm:String

the name of the secret key algorithm to be associated with the given key material. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.

Exceptions
IllegalArgumentException:
if algorithm is null or key is null or empty.
External Specification
Java Security Standard Algorithm Names
SecretKeySpecback to summary
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) must be used.

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.

offset:int

the offset in key where the key material starts.

len:int

the length of the key material.

algorithm:String

the name of the secret key algorithm to be associated with the given key material. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.

Exceptions
IllegalArgumentException:
if algorithm is null or key is null, empty, or too short, i.e. key.length-offset<len.
ArrayIndexOutOfBoundsException:
if offset or len are negative.
External Specification
Java Security Standard Algorithm Names

Method Detail

clearback to summary
pack-priv void clear()

Clear the key bytes inside.

doSanityCheckback to summary
private static String doSanityCheck(byte[] key, String algorithm)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

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.

Parameters
obj:Object

the object to test for equality with this object.

Returns:boolean

true if the objects are considered equal, false if obj is null or otherwise.

Annotations
@Override
getAlgorithmback to summary
public String getAlgorithm()

Implements java.security.Key.getAlgorithm.

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

Returns:String

the secret key algorithm.

getEncodedback to summary
public byte[] getEncoded()

Implements java.security.Key.getEncoded.

Returns the key material of this secret key.

Returns:byte[]

the key material. Returns a new array each time this method is called.

getFormatback to summary
public String getFormat()

Implements java.security.Key.getFormat.

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

Returns:String

the string "RAW".

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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

Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException

Restores the state of this object from the stream.

Parameters
stream:ObjectInputStream

the ObjectInputStream from which data is read

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if a serialized class cannot be loaded