|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--iaik.pkcs.pkcs8.EncryptedPrivateKeyInfo
This class implements from the PKCS#8 Private-Key Information Syntax Standard the syntax for encrypted private keys.
For encrypting some private key, it is suggested to use a password-based
encryption algorithm, as for instance described in PKCS#5 or PKCS#12. Both
types of algorithms require a password for creating a secret key to be fed
into the en/decryption process. For PKCS#5 this secret key has to be an
instance of iaik.security.cipher.PBEKey
, for PKCS#12 an
iaik.security.cipher.PBEKeyBMP
is used, treating the password
as a BMPString according to PKCS#12.
PKCS#8 defines EncryptedPrivateKeyInfo
as a ASN.1 SEQUENCE
containing the following components:
EncryptedPrivateKeyInfo ::= SEQUENCE { encryptionAlgorithm EncryptionAlgorithmIdentifier, encryptedData EncryptedData }
where:
encryptionAlgorithmIdentifier ::= AlgorithmIdentifier -- algorithm for encrypting the private-key information EncryptedData ::= OCTET STRING -- the encrypted private-key information
IAIK-JCE implements the PbeWithMD5AndDES_CBC
algorithm of the
PKCS#5 standard, and the PbeWithSHAAnd3_KeyTripleDES_CBC
and
PbeWithSHAAnd40BitRC2_CBC
algorithms of the PKCS#12 standard,
that may be used for password based encrypting some private key
according to PKCS#8.
Suppose you have created a RSAPrivateKey rsa_priv_key
and are
going to protect it with a password according to PKCS#5 and PKCS#8. You
therefore will encode a value of type PrivateKeyInfo
according
to PKCS#8 to represent the private key in an algorithm-independent manner,
which subsequently will be encrypted using the PbeWithMD5AndDES_CBC
algorithm and encoded as PKCS#8 EncryptedPrivateKeyInfo
:
Decrypting goes the reverse way obtaining a PrivateKeyInfo from the EncryptedPrivateKeyInfo and "extracting" RSAPrivateKey:EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(rsa_priv_key); epki.encrypt("password", AlgorithmID.pbeWithMD5AndDES_CBC, null);
RSAPrivateKey rsa_priv_key = (RSAPrivateKey)epki.decrypt("password");
PbeWithMD5AndDES_CBC
,
PbeWithSHAAnd3_KeyTripleDES_CBC
,
PbeWithSHAAnd40BitRC2_CBC
,
PBEKey
,
PBEKeyBMP
,
PrivateKeyInfo
, Serialized FormFields inherited from interface java.security.PrivateKey |
serialVersionUID |
Constructor Summary | |
EncryptedPrivateKeyInfo(ASN1Object obj)
Creates a new EncryptedPrivateKeyInfo from an ASN1Object. |
|
EncryptedPrivateKeyInfo(byte[] arr)
Creates a new EncryptedPrivateKeyInfo from a byte array. |
|
EncryptedPrivateKeyInfo(InputStream is)
Creates a new EncryptedPrivateKeyInfo from an InputStream. |
|
EncryptedPrivateKeyInfo(PrivateKey privateKey)
Creates a new EncryptedPrivateKeyInfo from a PrivateKey. |
Method Summary | |
void |
decode(ASN1Object obj)
Decodes the given ASN.1 EncryptedPrivateKeyInfo object
for parsing the internal structure.
|
PrivateKey |
decrypt(char[] password)
Decrypts an encrypted PrivateKeyInfo (PKCS#5 and PKCS#8). |
PrivateKey |
decrypt(String password)
Decrypts an encrypted PrivateKeyInfo (PKCS#5 and PKCS#8). |
void |
encrypt(char[] password,
AlgorithmID encryptionAlgorithm,
SecureRandom random)
Encrypts the PrivateKeyInfo data structure (PKCS#5 and PKCS#8) password based using the specified PBE algorithm. |
void |
encrypt(char[] password,
AlgorithmID encryptionAlgorithm,
SecureRandom random,
int iterationCount)
Encrypts the PrivateKeyInfo data structure (PKCS#5 and PKCS#8) password based using the specified PBE algorithm. |
void |
encrypt(String password,
AlgorithmID encryptionAlgorithm,
SecureRandom random)
Encrypts the PrivateKeyInfo data structure (PKCS#5 and PKCS#8) password based using the specified PBE algorithm. |
String |
getAlgorithm()
Returns the name of the algorithm. |
byte[] |
getEncoded()
Returns this EncryptedPrivateKeyInfo as a DER encoded byte array. |
String |
getFormat()
Returns the name of the encoding format.. |
PrivateKey |
getPrivateKeyInfo()
Gets the PrivateKey from this EncryptedPrivateKeyInfo. |
ASN1Object |
toASN1Object()
Returns this EncryptedPrivateKeyInfo as ASN1Object. |
String |
toString()
Returns a string that represents the contents of this EncryptedPrivateKeyInfo .
|
void |
writeTo(OutputStream os)
Writes this EncryptedPrivateKeyInfo to an output stream.
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public EncryptedPrivateKeyInfo(PrivateKey privateKey)
Use this constructor for supplying the private key to be encrypted, e.g.:
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(rsa_priv_key);
privateKeyInfo
- the PrivateKeyInfo to be used for initializing this
EncryptedPrivateKeyInfopublic EncryptedPrivateKeyInfo(ASN1Object obj) throws InvalidKeyException
Do not use this constructor for supplying the private key
to be encrypted. This constructor may be used for parsing an
already exisiting EncryptedPrivateKeyInfo
object, supplied as
ASN1Object that may have been created by calling
toASN1Object
.
Use the EncryptedPrivateKeyInfo(PrivateKeyInfo privateKeyInfo)
constructor for supplying the private key to be encrypted when creating an
EncryptedPrivateKeyInfo
object.
obj
- the PrivateKeyInfo as ASN1ObjectInvalidKeyException
- if the object can not be parsedpublic EncryptedPrivateKeyInfo(InputStream is) throws InvalidKeyException, IOException
This constructor reads an EncryptedPrivateKeyInfo priviously written with method writeTo(OutputStream). This constructor cannot be used to read a serialized object.
is
- the input stream from where the EncryptedPrivateKeyInfo shall be readInvalidKeyException
- if the data can not be parsedIOException
- if an I/O error occurspublic EncryptedPrivateKeyInfo(byte[] arr) throws InvalidKeyException
Do not use this constructor for supplying the private key
to be encrypted. This constructor may be used for parsing an
already exisiting EncryptedPrivateKeyInfo
object, supplied
as DER encoded ASN.1 structure which may have been created by calling the
getEncoded
method of this class.
Use the EncryptedPrivateKeyInfo(PrivateKeyInfo privateKeyInfo)
constructor for supplying the private key to be encrypted when creating an
EncryptedPrivateKeyInfo
object.
arr
- the array containing the encoded EncryptedPrivateKeyInfoInvalidKeyException
- if the data can not be parsedMethod Detail |
public void decode(ASN1Object obj) throws CodingException
EncryptedPrivateKeyInfo
object
for parsing the internal structure.
This method implements the ASN1Type interface and internally is called when
creating a PKCS#8 EncryptedPrivateKeyInfo
object from an
already existing EncryptedPrivateKeyInfo
object, supplied
as ASN1Object or DER encoded ASN1Object.
decode
in interface ASN1Type
obj
- the EncryptedPrivateKeyInfo as ASN1ObjectCodingException
- if the ASN1Object could not be parsedpublic void encrypt(String password, AlgorithmID encryptionAlgorithm, SecureRandom random) throws NoSuchAlgorithmException
password
- the password to useencryptionAlgorithm
- the AlgorithmID of the encryption algorithmrandom
- the source or randomness for generating the salt or null
if the default SecureRandom() shall be usedNoSuchAlgorithmException
- if there is no implementation for the specified algorithmpublic void encrypt(char[] password, AlgorithmID encryptionAlgorithm, SecureRandom random) throws NoSuchAlgorithmException
password
- the password to useencryptionAlgorithm
- the AlgorithmID of the encryption algorithmrandom
- the source or randomness for generating the salt or null
if the default SecureRandom() shall be usedNoSuchAlgorithmException
- if there is no implementation for the specified algorithmpublic void encrypt(char[] password, AlgorithmID encryptionAlgorithm, SecureRandom random, int iterationCount) throws NoSuchAlgorithmException
password
- the password to useencryptionAlgorithm
- the AlgorithmID of the encryption algorithmrandom
- the source or randomness for generating the salt or null
if the default SecureRandom() shall be usediterationCount
- the iteration count for key derivationNoSuchAlgorithmException
- if there is no implementation for the specified algorithmpublic PrivateKey decrypt(String password) throws NoSuchAlgorithmException, GeneralSecurityException
password
- the password to decrypt the keyNoSuchAlgorithmException
- if there is no implementation for the encryption algorithmGeneralSecurityException
- if the private key could not be decrypted (password wrong)public PrivateKey decrypt(char[] password) throws NoSuchAlgorithmException, GeneralSecurityException
password
- the password to decrypt the keyNoSuchAlgorithmException
- if there is no implementation for the encryption algorithmGeneralSecurityException
- if the private key could not be decrypted (password wrong)public PrivateKey getPrivateKeyInfo()
public ASN1Object toASN1Object()
If the private key is encrypted, an EncryptedPrivateKeyInfo is returned, otherwise a PrivateKeyInfo.
toASN1Object
in interface ASN1Type
public byte[] getEncoded()
getEncoded
in interface Key
public String getAlgorithm()
getAlgorithm
in interface Key
public String getFormat()
getFormat
in interface Key
public void writeTo(OutputStream os) throws IOException
EncryptedPrivateKeyInfo
to an output stream.
os
- the output streamIOException
- if an I/O error occurspublic String toString()
EncryptedPrivateKeyInfo
.
If the private key already has been encrypted, the name of the encryption algorithm is specified.
toString
in class Object
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |