Class EncryptedPrivateKeyInfo
java.lang.Object
javax.crypto.EncryptedPrivateKeyInfo
- All Implemented Interfaces:
DEREncodablePREVIEW
This class implements the
EncryptedPrivateKeyInfo
type
as defined in PKCS #8.
Its ASN.1 definition is as follows:
EncryptedPrivateKeyInfo ::= SEQUENCE { encryptionAlgorithm AlgorithmIdentifier, encryptedData OCTET STRING } AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }
- Since:
- 1.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionEncryptedPrivateKeyInfo
(byte[] encoded) Constructs anEncryptedPrivateKeyInfo
from a given encrypted PKCS#8 ASN.1 encoding.EncryptedPrivateKeyInfo
(String algName, byte[] encryptedData) Constructs anEncryptedPrivateKeyInfo
from the encryption algorithm name and the encrypted data.EncryptedPrivateKeyInfo
(AlgorithmParameters algParams, byte[] encryptedData) Constructs anEncryptedPrivateKeyInfo
from the encryption algorithm parameters and the encrypted data. -
Method Summary
Modifier and TypeMethodDescriptionstatic EncryptedPrivateKeyInfo
encryptKey
(PrivateKey key, char[] password) Preview.Creates and encrypts anEncryptedPrivateKeyInfo
from a givenPrivateKey
and password.static EncryptedPrivateKeyInfo
encryptKey
(PrivateKey key, char[] password, String algorithm, AlgorithmParameterSpec params, Provider provider) Preview.Creates and encrypts anEncryptedPrivateKeyInfo
from a givenPrivateKey
.static EncryptedPrivateKeyInfo
encryptKey
(PrivateKey key, Key encKey, String algorithm, AlgorithmParameterSpec params, Provider provider, SecureRandom random) Preview.Creates and encrypts anEncryptedPrivateKeyInfo
from the givenPrivateKey
using theencKey
and given parameters.Returns the encryption algorithm.Returns the algorithm parameters used by the encryption algorithm.byte[]
Returns the ASN.1 encoding of this object.byte[]
Returns the encrypted data.getKey
(char[] password) Preview.Extract the enclosedPrivateKey
object from the encrypted data and return it.Preview.Extract the enclosedPrivateKey
object from the encrypted data and return it.getKeySpec
(Key decryptKey) Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.getKeySpec
(Key decryptKey, String providerName) Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.getKeySpec
(Key decryptKey, Provider provider) Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.getKeySpec
(Cipher cipher) Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.
-
Constructor Details
-
EncryptedPrivateKeyInfo
Constructs anEncryptedPrivateKeyInfo
from a given encrypted PKCS#8 ASN.1 encoding.- Parameters:
encoded
- the ASN.1 encoding of this object. The contents of the array are copied to protect against subsequent modification.- Throws:
NullPointerException
- ifencoded
isnull
.IOException
- if error occurs when parsing the ASN.1 encoding.
-
EncryptedPrivateKeyInfo
public EncryptedPrivateKeyInfo(String algName, byte[] encryptedData) throws NoSuchAlgorithmException Constructs anEncryptedPrivateKeyInfo
from the encryption algorithm name and the encrypted data.Note: This constructor will use
null
as the value of the algorithm parameters. If the encryption algorithm has parameters whose value is notnull
, a different constructor, e.g. EncryptedPrivateKeyInfo(AlgorithmParameters, byte[]), should be used.- Parameters:
algName
- encryption algorithm name. See the Java Security Standard Algorithm Names document for information about standard Cipher algorithm names.encryptedData
- encrypted data. The contents ofencryptedData
are copied to protect against subsequent modification when constructing this object.- Throws:
NullPointerException
- ifalgName
orencryptedData
isnull
.IllegalArgumentException
- ifencryptedData
is empty, i.e. 0-length.NoSuchAlgorithmException
- if the specified algName is not supported.- External Specifications
-
EncryptedPrivateKeyInfo
public EncryptedPrivateKeyInfo(AlgorithmParameters algParams, byte[] encryptedData) throws NoSuchAlgorithmException Constructs anEncryptedPrivateKeyInfo
from the encryption algorithm parameters and the encrypted data.- Parameters:
algParams
- the algorithm parameters for the encryption algorithm.algParams.getEncoded()
should return the ASN.1 encoded bytes of theparameters
field of theAlgorithmIdentifier
component of theEncryptedPrivateKeyInfo
type.encryptedData
- encrypted data. The contents ofencryptedData
are copied to protect against subsequent modification when constructing this object.- Throws:
NullPointerException
- ifalgParams
orencryptedData
isnull
.IllegalArgumentException
- ifencryptedData
is empty, i.e. 0-length.NoSuchAlgorithmException
- if the specified algName of the specifiedalgParams
parameter is not supported.
-
-
Method Details
-
getAlgName
Returns the encryption algorithm.Note: Standard name is returned instead of the specified one in the constructor when such mapping is available. See the Java Security Standard Algorithm Names document for information about standard Cipher algorithm names.
- Returns:
- the encryption algorithm name.
- External Specifications
-
getAlgParameters
Returns the algorithm parameters used by the encryption algorithm.- Returns:
- the algorithm parameters.
-
getEncryptedData
public byte[] getEncryptedData()Returns the encrypted data.- Returns:
- the encrypted data. Returns a new array each time this method is called.
-
getKeySpec
Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.
Note: In order to successfully retrieve the enclosed PKCS8EncodedKeySpec object,cipher
needs to be initialized to either Cipher.DECRYPT_MODE or Cipher.UNWRAP_MODE, with the same key and parameters used for generating the encrypted data.- Parameters:
cipher
- the initializedCipher
object which will be used for decrypting the encrypted data.- Returns:
- the PKCS8EncodedKeySpec object.
- Throws:
NullPointerException
- ifcipher
isnull
.InvalidKeySpecException
- if the given cipher is inappropriate for the encrypted data or the encrypted data is corrupted and cannot be decrypted.
-
encryptKey
public static EncryptedPrivateKeyInfo encryptKey(PrivateKey key, char[] password, String algorithm, AlgorithmParameterSpec params, Provider provider) encryptKey
is a preview API of the Java platform.Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.Creates and encrypts anEncryptedPrivateKeyInfo
from a givenPrivateKey
. A valid password-based encryption (PBE) algorithm and password must be specified.The PBE algorithm string format details can be found in the Cipher section of the Java Security Standard Algorithm Names Specification.
- Implementation Note:
- The
jdk.epkcs8.defaultAlgorithm
Security Property defines the default encryption algorithm and theAlgorithmParameterSpec
are the provider's algorithm defaults. - Parameters:
key
- thePrivateKey
to be encryptedpassword
- the password used in the PBE encryption. This array will be cloned before being used.algorithm
- the PBE encryption algorithm. The default algorithm will be used ifnull
. However,null
is not allowed whenparams
is non-null.params
- theAlgorithmParameterSpec
to be used with encryption. The provider default will be used ifnull
.provider
- theProvider
will be used for PBESecretKeyFactory
generation andCipher
encryption operations. The default provider list will be used ifnull
.- Returns:
- an
EncryptedPrivateKeyInfo
- Throws:
IllegalArgumentException
- on initialization errors based on the arguments passed to the methodRuntimeException
- on an encryption errorNullPointerException
- if the key or password arenull
. Ifparams
is non-null whenalgorithm
isnull
.- Since:
- 25
-
encryptKey
encryptKey
is a preview API of the Java platform.Programs can only useencryptKey
when preview features are enabled.Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.Creates and encrypts anEncryptedPrivateKeyInfo
from a givenPrivateKey
and password. Default algorithm and parameters are used.- Implementation Note:
- The
jdk.epkcs8.defaultAlgorithm
Security Property defines the default encryption algorithm and theAlgorithmParameterSpec
are the provider's algorithm defaults. - Parameters:
key
- thePrivateKey
to be encryptedpassword
- the password used in the PBE encryption. This array will be cloned before being used.- Returns:
- an
EncryptedPrivateKeyInfo
- Throws:
IllegalArgumentException
- on initialization errors based on the arguments passed to the methodRuntimeException
- on an encryption errorNullPointerException
- when thekey
orpassword
isnull
- Since:
- 25
-
encryptKey
public static EncryptedPrivateKeyInfo encryptKey(PrivateKey key, Key encKey, String algorithm, AlgorithmParameterSpec params, Provider provider, SecureRandom random) encryptKey
is a preview API of the Java platform.Programs can only useencryptKey
when preview features are enabled.Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.Creates and encrypts anEncryptedPrivateKeyInfo
from the givenPrivateKey
using theencKey
and given parameters.- Implementation Note:
- The
jdk.epkcs8.defaultAlgorithm
Security Property defines the default encryption algorithm and theAlgorithmParameterSpec
are the provider's algorithm defaults. - Parameters:
key
- thePrivateKey
to be encryptedencKey
- the password-based encryption (PBE)Key
used to encryptkey
.algorithm
- the PBE encryption algorithm. The default algorithm is will be used ifnull
; however,null
is not allowed whenparams
is non-null.params
- theAlgorithmParameterSpec
to be used with encryption. The provider list default will be used ifnull
.provider
- theProvider
is used forCipher
encryption operation. The default provider list will be used ifnull
.random
- theSecureRandom
instance used during encryption. The default will be used ifnull
.- Returns:
- an
EncryptedPrivateKeyInfo
- Throws:
IllegalArgumentException
- on initialization errors based on the arguments passed to the methodRuntimeException
- on an encryption errorNullPointerException
- if thekey
orencKey
arenull
. Ifparams
is non-null,algorithm
cannot benull
.- Since:
- 25
-
getKey
getKey
is a preview API of the Java platform.Programs can only usegetKey
when preview features are enabled.Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.Extract the enclosedPrivateKey
object from the encrypted data and return it.- Parameters:
password
- the password used in the PBE encryption. This array will be cloned before being used.- Returns:
- a
PrivateKey
- Throws:
GeneralSecurityException
- if an error occurs parsing or decrypting the encrypted data, or producing the key object.NullPointerException
- ifpassword
is null- Since:
- 25
-
getKey
getKey
is a preview API of the Java platform.Programs can only usegetKey
when preview features are enabled.Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.Extract the enclosedPrivateKey
object from the encrypted data and return it.- Parameters:
decryptKey
- the decryption key and cannot benull
provider
- theProvider
used for Cipher decryption andPrivateKey
generation. Anull
value will use the default provider configuration.- Returns:
- a
PrivateKey
- Throws:
GeneralSecurityException
- if an error occurs parsing or decrypting the encrypted data, or producing the key object.NullPointerException
- ifdecryptKey
is null- Since:
- 25
-
getKeySpec
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.- Parameters:
decryptKey
- key used for decrypting the encrypted data.- Returns:
- the PKCS8EncodedKeySpec object.
- Throws:
NullPointerException
- ifdecryptKey
isnull
.NoSuchAlgorithmException
- if cannot find appropriate cipher to decrypt the encrypted data.InvalidKeyException
- ifdecryptKey
cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8KeySpec.- Since:
- 1.5
-
getKeySpec
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, String providerName) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.- Parameters:
decryptKey
- key used for decrypting the encrypted data.providerName
- the name of provider whose cipher implementation will be used.- Returns:
- the PKCS8EncodedKeySpec object.
- Throws:
NullPointerException
- ifdecryptKey
orproviderName
isnull
.NoSuchProviderException
- if no providerproviderName
is registered.NoSuchAlgorithmException
- if cannot find appropriate cipher to decrypt the encrypted data.InvalidKeyException
- ifdecryptKey
cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8KeySpec.- Since:
- 1.5
-
getKeySpec
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, Provider provider) throws NoSuchAlgorithmException, InvalidKeyException Extract the enclosed PKCS8EncodedKeySpec object from the encrypted data and return it.- Parameters:
decryptKey
- key used for decrypting the encrypted data.provider
- the name of provider whose cipher implementation will be used.- Returns:
- the PKCS8EncodedKeySpec object.
- Throws:
NullPointerException
- ifdecryptKey
orprovider
isnull
.NoSuchAlgorithmException
- if cannot find appropriate cipher to decrypt the encrypted data inprovider
.InvalidKeyException
- ifdecryptKey
cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8KeySpec.- Since:
- 1.5
-
getEncoded
Returns the ASN.1 encoding of this object.- Returns:
- the ASN.1 encoding. Returns a new array each time this method is called.
- Throws:
IOException
- if error occurs when constructing its ASN.1 encoding.
-
encryptKey
when preview features are enabled.