com.dstc.security.keymanage
Class SSLeayEncryptedPrivateKey

java.lang.Object
  |
  +--com.dstc.security.keymanage.SSLeayEncryptedPrivateKey

public class SSLeayEncryptedPrivateKey
extends Object

This class encrypts/decrypts a PrivateKey to/from the the proprietary encryption format used in SSLeay. The encrypted keys are stored in the ASCII format

 -----BEGIN RSA PRIVATE KEY-----
 Proc-Type: 4,ENCRYPTED
 DEK-Info: DES-EDE3-CBC,ivBytes
 
 Base 64 encoding goes here
 -----END RSA PRIVATE KEY-----
 
Here, ivBytes is the hexadecimal representation of the Initialization Vector used in the encryption process.

Typical usage of this class would be the following:

To encrypt the PrivateKey object privateKey using the password "myPassword" and store it in a file with the above format,

 SSLeayEncryptedPrivateKey encKey = 
              new SSLeayEncryptedPrivateKey(privateKey);
 FileOutputStream fos = new FileOutputStream(fileName);
 encKey.encrypt("myPassword".toCharArray());
 fos.write(encKey.getEncoded());
 
Conversely, to decrypt the encrypted private key stored in the file "fileName",
 FileInputStream fis = new FileInputStream("fileName");
 SSLeayEncryptedPrivateKey encKey = new SSLeayEncryptedPrivateKey(fis);
 encKey.decrypt("myPassword".toCharArray());
 PrivateKey privateKey = encKey.getPrivateKey();
 


Constructor Summary
SSLeayEncryptedPrivateKey(byte[] encoding)
           Construct an SSLeayEncryptedPrivateKey object from a byte array containing the (base-64 encoded) encrypted private key.
SSLeayEncryptedPrivateKey(InputStream is)
           Construct an SSLeayEncryptedPrivateKey object from an InputStream containing the (base-64 encoded) encrypted private key.
SSLeayEncryptedPrivateKey(SecureRandom rand, PrivateKey priv)
           Construct an SSLeayEncryptedPrivateKey object from a PrivateKey
 
Method Summary
 void decrypt(char[] pass)
           Decrypts the SSLeayEncryptedPrivateKey object using the given password.
 void encrypt(char[] pass)
           Encrypts the SSLeayEncryptedPrivateKey object using the given password.
 byte[] getEncoded()
           Returns the Base64-encoding of the SSLeayEncryptedPrivateKey .
 PrivateKey getPrivateKey()
           Returns the PrivateKey associated with this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SSLeayEncryptedPrivateKey

public SSLeayEncryptedPrivateKey(byte[] encoding)
                          throws SSLeayException

Construct an SSLeayEncryptedPrivateKey object from a byte array containing the (base-64 encoded) encrypted private key.

Parameters:
encoding - the byte array containing the (base-64 encoded) encrypted private key.
Throws:
SSLeayException - if the data is corrupt.

SSLeayEncryptedPrivateKey

public SSLeayEncryptedPrivateKey(InputStream is)
                          throws SSLeayException,
                                 IOException

Construct an SSLeayEncryptedPrivateKey object from an InputStream containing the (base-64 encoded) encrypted private key.

Parameters:
is - the InputStream containing the (base-64 encoded) encrypted private key.
Throws:
SSLeayException - if the data is corrupt.
IOException -  

SSLeayEncryptedPrivateKey

public SSLeayEncryptedPrivateKey(SecureRandom rand,
                                 PrivateKey priv)
                          throws SSLeayException

Construct an SSLeayEncryptedPrivateKey object from a PrivateKey

Parameters:
rand - source of randomness (can be null ).
priv - the PrivateKey to be encrypted.
Throws:
SSLeayException - if PrivateKey is not an instance of java.security.interfaces.RSAPrivateCrtKey .
Method Detail

encrypt

public void encrypt(char[] pass)
             throws SSLeayException

Encrypts the SSLeayEncryptedPrivateKey object using the given password.

Parameters:
password - the character array containing the password used to encrypt the key.
Throws:
SSLeayException - if the encryption process fails.

getEncoded

public byte[] getEncoded()
                  throws SSLeayException

Returns the Base64-encoding of the SSLeayEncryptedPrivateKey .

Throws:
SSLeayException - if encrypt() has not yet been called.

getPrivateKey

public PrivateKey getPrivateKey()
                         throws SSLeayException

Returns the PrivateKey associated with this object.

Throws:
SSLeayException - if decrypt() has not yet been called.

decrypt

public void decrypt(char[] pass)
             throws SSLeayException

Decrypts the SSLeayEncryptedPrivateKey object using the given password.

Parameters:
password - the character array containing the password used to decrypt the key.
Throws:
SSLeayException - if the decryption process fails.