APIdia Launch Partner
Top Description Fields Constructors Methods
javax.crypto

public non-sealed Class EncryptedPrivateKeyInfo

extends Object
implements BinaryEncodable
Class Inheritance
All Implemented Interfaces
java.security.BinaryEncodable
Imports
jdk.internal.javac.PreviewFeature, sun.security.jca.JCAUtil, sun.security.pkcs.PKCS8Key, sun.security.util.*, sun.security.x509.AlgorithmId, javax.crypto.spec.PBEKeySpec, java.io.IOException, java.security.*, java.security.spec.*, java.util.Objects

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  }
Author
Valerie Peng
Since
1.4
See Also
java.security.spec.PKCS8EncodedKeySpec

Field Summary

Modifier and TypeField and Description
private final AlgorithmId
private final byte[]
private final byte[]
private final AlgorithmParameters

Constructor Summary

AccessConstructor and Description
public
EncryptedPrivateKeyInfo(byte[]
the ASN.1 encoding of this object. The contents of the array are copied to protect against subsequent modification.
encoded
)

Constructs an EncryptedPrivateKeyInfo from a given encrypted PKCS#8 ASN.1 encoding.

public
EncryptedPrivateKeyInfo(String
encryption algorithm name. See the Java Security Standard Algorithm Names document for information about standard Cipher algorithm names.
algName
,
byte[]
encrypted data. The contents of encryptedData are copied to protect against subsequent modification when constructing this object.
encryptedData
)

Constructs an EncryptedPrivateKeyInfo from the encryption algorithm name and the encrypted data.

public
EncryptedPrivateKeyInfo(AlgorithmParameters
the algorithm parameters for the encryption algorithm. algParams.getEncoded() should return the ASN.1 encoded bytes of the parameters field of the AlgorithmIdentifier component of the EncryptedPrivateKeyInfo type.
algParams
,
byte[]
encrypted data. The contents of encryptedData are copied to protect against subsequent modification when constructing this object.
encryptedData
)

Constructs an EncryptedPrivateKeyInfo from the encryption algorithm parameters and the encrypted data.

private
EncryptedPrivateKeyInfo(byte[] encoded, byte[] eData, AlgorithmId id, AlgorithmParameters p)

Create an EncryptedPrivateKeyInfo object from the given components.

Method Summary

Modifier and TypeMethod and Description
private byte[]
decryptData(Key decryptKey, Provider provider)

public static EncryptedPrivateKeyInfo

Returns:

an EncryptedPrivateKeyInfo
encrypt
(BinaryEncodable
the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.
be
,
char[]
the password used for PBE encryption. This array is cloned before use.
password
,
String
the PBE encryption algorithm
algorithm
,
AlgorithmParameterSpec
the AlgorithmParameterSpec used for encryption. If null, the provider’s default parameters are applied.
params
,
Provider
the Provider for SecretKeyFactory and Cipher operations. If null, the default provider list is used.
provider
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable.

public static EncryptedPrivateKeyInfo

Returns:

an EncryptedPrivateKeyInfo
encrypt
(BinaryEncodable
the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.
be
,
char[]
the password used for PBE encryption. This array is cloned before use.
password
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable.

public static EncryptedPrivateKeyInfo

Returns:

an EncryptedPrivateKeyInfo
encrypt
(BinaryEncodable
the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.
be
,
Key
the key used to encrypt the encoding
encryptKey
,
String
the encryption algorithm, such as a password-based encryption (PBE) algorithm
algorithm
,
AlgorithmParameterSpec
the AlgorithmParameterSpec used for encryption. If null, the provider’s default parameters are applied.
params
,
Provider
the Provider for Cipher operations. If null, the default provider list is used.
provider
,
SecureRandom
the SecureRandom instance used during encryption. If null, the default is used.
random
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable.

private static EncryptedPrivateKeyInfo
encryptImpl(byte[] encoded, String algorithm, Key encryptKey, AlgorithmParameterSpec params, Provider provider, SecureRandom random)

private static SecretKey
generateSecretKey(char[] password, String algorithm, Provider provider)

public String

Returns:

the encryption algorithm name.
getAlgName
()

Returns the encryption algorithm.

public AlgorithmParameters

Returns:

the algorithm parameters.
getAlgParameters
()

Returns the algorithm parameters used by the encryption algorithm.

public byte[]

Returns:

the ASN.1 encoding. Returns a new array each time this method is called.
getEncoded
()

Returns the ASN.1 encoding of this object.

private static byte[]
public byte[]

Returns:

the encrypted data. Returns a new array each time this method is called.
getEncryptedData
()

Returns the encrypted data.

public PrivateKey

Returns:

the decrypted PrivateKey
getKey
(char[]
the password used for PBE decryption. The array is cloned before use.
password
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Extracts and returns the enclosed PrivateKey using the specified password.

public PrivateKey

Returns:

the decrypted PrivateKey
getKey
(Key
the decryption key; must not be null
decryptKey
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Extracts and returns the enclosed PrivateKey using the specified decryption key.

public KeyPair

Returns:

a decrypted KeyPair
getKeyPair
(char[]
the password used for PBE decryption. The array is cloned before use.
password
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Extracts and returns the enclosed KeyPair using the specified password.

public KeyPair

Returns:

a decrypted KeyPair
getKeyPair
(Key
the decryption key; must not be null
decryptKey
)
Preview Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Extracts and returns the enclosed KeyPair using the specified decryption key.

public PKCS8EncodedKeySpec

Returns:

the PKCS8EncodedKeySpec object
getKeySpec
(Cipher
the initialized Cipher object which will be used for decrypting the encrypted data.
cipher
)

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

public PKCS8EncodedKeySpec

Returns:

the PKCS8EncodedKeySpec object with a specified algorithm
getKeySpec
(Key
key used for decrypting the encrypted data.
decryptKey
)

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

public PKCS8EncodedKeySpec

Returns:

the PKCS8EncodedKeySpec object with a specified algorithm
getKeySpec
(Key
key used for decrypting the encrypted data.
decryptKey
,
String
the name of provider whose cipher implementation will be used.
providerName
)

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

public PKCS8EncodedKeySpec

Returns:

the PKCS8EncodedKeySpec object with a specified algorithm
getKeySpec
(Key
key used for decrypting the encrypted data.
decryptKey
,
Provider
the provider whose cipher implementation will be used.
provider
)

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

private PKCS8EncodedKeySpec
getKeySpecImpl(Key decryptKey, Provider provider)

private static PKCS8EncodedKeySpec
pkcs8EncodingToSpec(byte[] encodedKey)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

algidback to summary
private final AlgorithmId algid
encodedback to summary
private final byte[] encoded
encryptedDataback to summary
private final byte[] encryptedData
paramsback to summary
private final AlgorithmParameters params

Constructor Detail

EncryptedPrivateKeyInfoback to summary
public EncryptedPrivateKeyInfo(byte[] encoded) throws IOException

Constructs an EncryptedPrivateKeyInfo from a given encrypted PKCS#8 ASN.1 encoding.

Parameters
encoded:byte[]

the ASN.1 encoding of this object. The contents of the array are copied to protect against subsequent modification.

Exceptions
IOException:
if error occurs when parsing the ASN.1 encoding.
NullPointerException:
if encoded is null.
EncryptedPrivateKeyInfoback to summary
public EncryptedPrivateKeyInfo(String algName, byte[] encryptedData) throws NoSuchAlgorithmException

Constructs an EncryptedPrivateKeyInfo 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 not null, a different constructor, e.g. EncryptedPrivateKeyInfo(AlgorithmParameters, byte[]), should be used.

Parameters
algName:String

encryption algorithm name. See the Java Security Standard Algorithm Names document for information about standard Cipher algorithm names.

encryptedData:byte[]

encrypted data. The contents of encryptedData are copied to protect against subsequent modification when constructing this object.

Exceptions
NoSuchAlgorithmException:
if the specified algName is not supported.
NullPointerException:
if algName or encryptedData is null.
IllegalArgumentException:
if encryptedData is empty, i.e. 0-length.
External Specification
Java Security Standard Algorithm Names
EncryptedPrivateKeyInfoback to summary
public EncryptedPrivateKeyInfo(AlgorithmParameters algParams, byte[] encryptedData) throws NoSuchAlgorithmException

Constructs an EncryptedPrivateKeyInfo from the encryption algorithm parameters and the encrypted data.

Parameters
algParams:AlgorithmParameters

the algorithm parameters for the encryption algorithm. algParams.getEncoded() should return the ASN.1 encoded bytes of the parameters field of the AlgorithmIdentifier component of the EncryptedPrivateKeyInfo type.

encryptedData:byte[]

encrypted data. The contents of encryptedData are copied to protect against subsequent modification when constructing this object.

Exceptions
NoSuchAlgorithmException:
if the specified algName of the specified algParams parameter is not supported.
NullPointerException:
if algParams or encryptedData is null.
IllegalArgumentException:
if encryptedData is empty, i.e. 0-length.
EncryptedPrivateKeyInfoback to summary
private EncryptedPrivateKeyInfo(byte[] encoded, byte[] eData, AlgorithmId id, AlgorithmParameters p)

Create an EncryptedPrivateKeyInfo object from the given components.

Method Detail

decryptDataback to summary
private byte[] decryptData(Key decryptKey, Provider provider) throws NoSuchAlgorithmException, InvalidKeyException
encryptback to summary
public static EncryptedPrivateKeyInfo encrypt(BinaryEncodable be, char[] password, String algorithm, AlgorithmParameterSpec params, Provider provider)

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use encrypt when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable. A valid password-based encryption (PBE) algorithm and password must be specified.

The format of the PBE algorithm string is described in the Cipher Algorithms section of the Java Security Standard Algorithm Names Specification.

Parameters
be:BinaryEncodable

the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.

password:char[]

the password used for PBE encryption. This array is cloned before use.

algorithm:String

the PBE encryption algorithm

params:AlgorithmParameterSpec

the AlgorithmParameterSpec used for encryption. If null, the provider’s default parameters are applied.

provider:Provider

the Provider for SecretKeyFactory and Cipher operations. If null, the default provider list is used.

Returns:EncryptedPrivateKeyInfo

an EncryptedPrivateKeyInfo

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NullPointerException:
if be, password, or algorithm is null
IllegalArgumentException:
if be is an unsupported BinaryEncodable or has no encoding
CryptoException:
if an error occurs while generating the PBE key, if algorithm or params are not supported by any provider, or if an error occurs during encryption
Since
27
encryptback to summary
public static EncryptedPrivateKeyInfo encrypt(BinaryEncodable be, char[] password)

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use encrypt when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable. A valid password must be specified. A default password-based encryption (PBE) algorithm and provider are used.

Implementation Note

The jdk.epkcs8.defaultAlgorithm security property defines the default encryption algorithm. The AlgorithmParameterSpec defaults are determined by the provider.

Parameters
be:BinaryEncodable

the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.

password:char[]

the password used for PBE encryption. This array is cloned before use.

Returns:EncryptedPrivateKeyInfo

an EncryptedPrivateKeyInfo

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NullPointerException:
if be or password is null
IllegalArgumentException:
if be is an unsupported BinaryEncodable or has no encoding
CryptoException:
if an error occurs while generating the PBE key, if the default algorithm is misconfigured, or if an error occurs during encryption
Since
27
encryptback to summary
public static EncryptedPrivateKeyInfo encrypt(BinaryEncodable be, Key encryptKey, String algorithm, AlgorithmParameterSpec params, Provider provider, SecureRandom random)

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use encrypt when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Creates an EncryptedPrivateKeyInfo by encrypting the specified BinaryEncodable. A valid encryption algorithm and Key must be specified.

The format of the algorithm string is described in the Cipher Algorithms section of the Java Security Standard Algorithm Names Specification.

Parameters
be:BinaryEncodable

the BinaryEncodable to encrypt. Supported types include PrivateKey, KeyPair, and PKCS8EncodedKeySpec.

encryptKey:Key

the key used to encrypt the encoding

algorithm:String

the encryption algorithm, such as a password-based encryption (PBE) algorithm

params:AlgorithmParameterSpec

the AlgorithmParameterSpec used for encryption. If null, the provider’s default parameters are applied.

provider:Provider

the Provider for Cipher operations. If null, the default provider list is used.

random:SecureRandom

the SecureRandom instance used during encryption. If null, the default is used.

Returns:EncryptedPrivateKeyInfo

an EncryptedPrivateKeyInfo

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NullPointerException:
if be, encryptKey, or algorithm is null
IllegalArgumentException:
if be is an unsupported BinaryEncodable or has no encoding
CryptoException:
if encryptKey is invalid, if algorithm or params are not supported by any provider, or if an error occurs during encryption
Since
27
encryptImplback to summary
private static EncryptedPrivateKeyInfo encryptImpl(byte[] encoded, String algorithm, Key encryptKey, AlgorithmParameterSpec params, Provider provider, SecureRandom random)
generateSecretKeyback to summary
private static SecretKey generateSecretKey(char[] password, String algorithm, Provider provider)
getAlgNameback to summary
public String 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:String

the encryption algorithm name.

External Specification
Java Security Standard Algorithm Names
getAlgParametersback to summary
public AlgorithmParameters getAlgParameters()

Returns the algorithm parameters used by the encryption algorithm.

Returns:AlgorithmParameters

the algorithm parameters.

getEncodedback to summary
public byte[] getEncoded() throws IOException

Returns the ASN.1 encoding of this object.

Returns:byte[]

the ASN.1 encoding. Returns a new array each time this method is called.

Exceptions
IOException:
if error occurs when constructing its ASN.1 encoding.
getEncodingback to summary
private static byte[] getEncoding(BinaryEncodable d)
getEncryptedDataback to summary
public byte[] getEncryptedData()

Returns the encrypted data.

Returns:byte[]

the encrypted data. Returns a new array each time this method is called.

getKeyback to summary
public PrivateKey getKey(char[] password) throws NoSuchAlgorithmException, InvalidKeyException

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use getKey when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Extracts and returns the enclosed PrivateKey using the specified password.

Parameters
password:char[]

the password used for PBE decryption. The array is cloned before use.

Returns:PrivateKey

the decrypted PrivateKey

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NoSuchAlgorithmException:
if the decryption algorithm is unsupported
InvalidKeyException:
if an error occurs during parsing, decryption, or key generation
NullPointerException:
if password is null
Since
25
getKeyback to summary
public PrivateKey getKey(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use getKey when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Extracts and returns the enclosed PrivateKey using the specified decryption key.

Parameters
decryptKey:Key

the decryption key; must not be null

Returns:PrivateKey

the decrypted PrivateKey

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NoSuchAlgorithmException:
if the decryption algorithm is unsupported
InvalidKeyException:
if an error occurs during parsing, decryption, or key generation
NullPointerException:
if decryptKey is null
Since
27
getKeyPairback to summary
public KeyPair getKeyPair(char[] password) throws NoSuchAlgorithmException, InvalidKeyException

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use getKeyPair when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Extracts and returns the enclosed KeyPair using the specified password. If the encoded data does not contain both a public and private key, an InvalidKeyException is thrown.

Parameters
password:char[]

the password used for PBE decryption. The array is cloned before use.

Returns:KeyPair

a decrypted KeyPair

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NoSuchAlgorithmException:
if the decryption algorithm is unsupported
InvalidKeyException:
if the encoded data lacks a public key, or if an error occurs during parsing, decryption, or key generation
NullPointerException:
if password is null
Since
26
getKeyPairback to summary
public KeyPair getKeyPair(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

Programs can only use getKeyPair when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

Extracts and returns the enclosed KeyPair using the specified decryption key. If the encoded data does not contain both a public and private key, an InvalidKeyException is thrown.

Parameters
decryptKey:Key

the decryption key; must not be null

Returns:KeyPair

a decrypted KeyPair

Annotations
@PreviewFeature
feature:PEM_API
Exceptions
NoSuchAlgorithmException:
if the decryption algorithm is unsupported
InvalidKeyException:
if the encoded data lacks a public key, or if an error occurs during parsing, decryption, or key generation
NullPointerException:
if decryptKey is null
Since
27
getKeySpecback to summary
public PKCS8EncodedKeySpec getKeySpec(Cipher cipher) throws InvalidKeySpecException

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns 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:Cipher

the initialized Cipher object which will be used for decrypting the encrypted data.

Returns:PKCS8EncodedKeySpec

the PKCS8EncodedKeySpec object

Exceptions
InvalidKeySpecException:
if the given cipher is inappropriate for the encrypted data or the encrypted data is corrupted and cannot be decrypted.
NullPointerException:
if cipher is null.
getKeySpecback to summary
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

Parameters
decryptKey:Key

key used for decrypting the encrypted data.

Returns:PKCS8EncodedKeySpec

the PKCS8EncodedKeySpec object with a specified algorithm

Exceptions
NoSuchAlgorithmException:
if cannot find appropriate cipher to decrypt the encrypted data.
InvalidKeyException:
if decryptKey cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8EncodedKeySpec.
NullPointerException:
if decryptKey is null.
Since
1.5
getKeySpecback to summary
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, String providerName) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

Parameters
decryptKey:Key

key used for decrypting the encrypted data.

providerName:String

the name of provider whose cipher implementation will be used.

Returns:PKCS8EncodedKeySpec

the PKCS8EncodedKeySpec object with a specified algorithm

Exceptions
NoSuchProviderException:
if no provider providerName is registered.
NoSuchAlgorithmException:
if cannot find appropriate cipher to decrypt the encrypted data.
InvalidKeyException:
if decryptKey cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8EncodedKeySpec.
NullPointerException:
if decryptKey or providerName is null.
Since
1.5
getKeySpecback to summary
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, Provider provider) throws NoSuchAlgorithmException, InvalidKeyException

Extracts the enclosed PKCS8EncodedKeySpec object from the encrypted data and returns it.

Parameters
decryptKey:Key

key used for decrypting the encrypted data.

provider:Provider

the provider whose cipher implementation will be used.

Returns:PKCS8EncodedKeySpec

the PKCS8EncodedKeySpec object with a specified algorithm

Exceptions
NoSuchAlgorithmException:
if cannot find appropriate cipher to decrypt the encrypted data in provider.
InvalidKeyException:
if decryptKey cannot be used to decrypt the encrypted data or the decryption result is not a valid PKCS8EncodedKeySpec.
NullPointerException:
if decryptKey or provider is null.
Since
1.5
getKeySpecImplback to summary
private PKCS8EncodedKeySpec getKeySpecImpl(Key decryptKey, Provider provider) throws NoSuchAlgorithmException, InvalidKeyException
pkcs8EncodingToSpecback to summary
private static PKCS8EncodedKeySpec pkcs8EncodingToSpec(byte[] encodedKey) throws IOException