public abstract class OpenSSLKey
extends java.lang.Object
implements java.io.Serializable
This is based on work done by Ming Yung at DSTC.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
encAlg |
private java.lang.String |
encAlgStr |
private byte[] |
encodedKey |
private static java.lang.String |
HEADER |
private javax.crypto.spec.IvParameterSpec |
initializationVector |
private java.security.PrivateKey |
intKey |
private boolean |
isEncrypted |
private byte[] |
ivData |
private int |
ivLength |
private java.lang.String |
keyAlg |
private byte[] |
keyData |
private int |
keyLength |
Constructor and Description |
---|
OpenSSLKey(java.io.InputStream is)
Reads a OpenSSL private key from the specified input stream.
|
OpenSSLKey(java.security.PrivateKey key)
Converts a RSAPrivateCrtKey into OpenSSL key.
|
OpenSSLKey(java.lang.String file)
Reads a OpenSSL private key from the specified file.
|
OpenSSLKey(java.lang.String algorithm,
byte[] data)
Initializes the OpenSSL key from raw byte array.
|
Modifier and Type | Method and Description |
---|---|
void |
decrypt(byte[] password)
Decrypts the private key with given password.
|
void |
decrypt(java.lang.String password)
Decrypts the private key with given password.
|
void |
encrypt(byte[] password)
Encrypts the private key with given password.
|
void |
encrypt(java.lang.String password)
Encrypts the private key with given password.
|
boolean |
equals(java.lang.Object other) |
private java.lang.String |
extractEncryptionInfo(java.io.BufferedReader in) |
private java.lang.String |
extractKey(java.io.BufferedReader in) |
private void |
generateIV() |
private javax.crypto.Cipher |
getCipher() |
protected byte[] |
getEncoded() |
protected abstract byte[] |
getEncoded(java.security.PrivateKey key) |
protected abstract java.security.PrivateKey |
getKey(java.lang.String alg,
byte[] data) |
private java.lang.String |
getKeyAlgorithm(java.lang.String line) |
java.security.PrivateKey |
getPrivateKey()
Returns the JCE (RSAPrivateCrtKey) key.
|
protected java.lang.String |
getProvider() |
private javax.crypto.spec.SecretKeySpec |
getSecretKey(byte[] pwd,
byte[] keyInitializationVector) |
int |
hashCode() |
boolean |
isEncrypted()
Check if the key was encrypted or not.
|
private static boolean |
objectsEquals(java.lang.Object a,
java.lang.Object b) |
private void |
parseEncryptionInfo(java.lang.String line) |
private void |
parseKeyAlgorithm(java.io.BufferedReader in) |
private void |
readObject(java.io.ObjectInputStream s) |
private void |
readPEM(java.io.Reader rd) |
private void |
setAlgorithmSettings(java.lang.String alg) |
void |
setEncryptionAlgorithm(java.lang.String alg)
Sets algorithm for encryption.
|
private void |
setIV(byte[] data) |
private void |
setIV(java.lang.String s) |
private java.lang.String |
toPEM() |
void |
writeTo(java.io.OutputStream output)
Writes the private key to the specified output stream in PEM
format.
|
void |
writeTo(java.lang.String file)
Writes the private key to the specified file in PEM format.
|
void |
writeTo(java.io.Writer w)
Writes the private key to the specified writer in PEM format.
|
private static final java.lang.String HEADER
private java.lang.String keyAlg
private boolean isEncrypted
private byte[] encodedKey
private java.security.PrivateKey intKey
private byte[] ivData
private transient javax.crypto.spec.IvParameterSpec initializationVector
private java.lang.String encAlgStr
private java.lang.String encAlg
private int keyLength
private int ivLength
private byte[] keyData
public OpenSSLKey(java.io.InputStream is) throws java.io.IOException, java.security.GeneralSecurityException
is
- input stream with OpenSSL key in PEM format.java.io.IOException
- if I/O problems.java.security.GeneralSecurityException
- if problems with the keypublic OpenSSLKey(java.lang.String file) throws java.io.IOException, java.security.GeneralSecurityException
file
- file containing the OpenSSL key in PEM format.java.io.IOException
- if I/O problems.java.security.GeneralSecurityException
- if problems with the keypublic OpenSSLKey(java.security.PrivateKey key)
key
- private key - must be a RSAPrivateCrtKeypublic OpenSSLKey(java.lang.String algorithm, byte[] data) throws java.security.GeneralSecurityException
algorithm
- the algorithm of the key. Currently only RSA algorithm is supported.data
- the DER encoded key data. If RSA algorithm, the key must be in PKCS#1 format.java.security.GeneralSecurityException
- if any security problems.protected byte[] getEncoded()
private void readPEM(java.io.Reader rd) throws java.io.IOException, java.security.GeneralSecurityException
java.io.IOException
java.security.GeneralSecurityException
private java.lang.String extractKey(java.io.BufferedReader in) throws java.io.IOException
java.io.IOException
private java.lang.String extractEncryptionInfo(java.io.BufferedReader in) throws java.io.IOException, java.security.GeneralSecurityException
java.io.IOException
java.security.GeneralSecurityException
private void parseKeyAlgorithm(java.io.BufferedReader in) throws java.io.IOException, java.security.InvalidKeyException
java.io.IOException
java.security.InvalidKeyException
public boolean isEncrypted()
public void decrypt(java.lang.String password) throws java.security.GeneralSecurityException
password
- password to decrypt the key with.java.security.GeneralSecurityException
- whenever an error occurs during decryption.public void decrypt(byte[] password) throws java.security.GeneralSecurityException
password
- password to decrypt the key with.java.security.GeneralSecurityException
- whenever an error occurs during decryption.public void encrypt(java.lang.String password) throws java.security.GeneralSecurityException
password
- password to encrypt the key with.java.security.GeneralSecurityException
- whenever an error occurs during encryption.public void encrypt(byte[] password) throws java.security.GeneralSecurityException
password
- password to encrypt the key with.java.security.GeneralSecurityException
- whenever an error occurs during encryption.public void setEncryptionAlgorithm(java.lang.String alg) throws java.security.GeneralSecurityException
alg
- algorithm for encryptionjava.security.GeneralSecurityException
- if algorithm is not supportedpublic java.security.PrivateKey getPrivateKey()
public void writeTo(java.io.OutputStream output) throws java.io.IOException
output
- output stream to write the key to.java.io.IOException
- if I/O problems writing the keypublic void writeTo(java.io.Writer w) throws java.io.IOException
w
- writer to output the key to.java.io.IOException
- if I/O problems writing the keypublic void writeTo(java.lang.String file) throws java.io.IOException
file
- file to write the key to.java.io.IOException
- if I/O problems writing the keyprotected abstract byte[] getEncoded(java.security.PrivateKey key)
protected abstract java.security.PrivateKey getKey(java.lang.String alg, byte[] data) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
protected java.lang.String getProvider()
private javax.crypto.Cipher getCipher() throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private java.lang.String getKeyAlgorithm(java.lang.String line)
private void parseEncryptionInfo(java.lang.String line) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private void setAlgorithmSettings(java.lang.String alg) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private void setIV(java.lang.String s) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private void generateIV()
private void setIV(byte[] data)
private javax.crypto.spec.SecretKeySpec getSecretKey(byte[] pwd, byte[] keyInitializationVector) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private java.lang.String toPEM()
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
private static boolean objectsEquals(java.lang.Object a, java.lang.Object b)
public int hashCode()
hashCode
in class java.lang.Object