public class PKCS8Key extends java.lang.Object
Can handle base64 PEM, or raw DER. Can handle PKCS8 Version 1.5 and 2.0. Can also handle OpenSSL encrypted or unencrypted private keys (DSA or RSA).
The PKCS12 key derivation (the "pkcs12()" method) comes from BouncyCastle.
Modifier and Type | Class and Description |
---|---|
static class |
PKCS8Key.DecryptResult |
Modifier and Type | Field and Description |
---|---|
private byte[] |
decryptedBytes |
static java.lang.String |
DSA_OID |
private boolean |
isDSA |
private boolean |
isRSA |
private int |
keySize |
static java.lang.String |
OPENSSL_DSA |
static java.lang.String |
OPENSSL_RSA |
static java.lang.String |
PKCS8_ENCRYPTED |
static java.lang.String |
PKCS8_UNENCRYPTED |
private java.security.PrivateKey |
privateKey |
static java.lang.String |
RSA_OID |
private java.lang.String |
transformation |
Constructor and Description |
---|
PKCS8Key(byte[] encoded,
char[] password) |
PKCS8Key(java.io.ByteArrayInputStream in,
char[] password) |
PKCS8Key(java.io.InputStream in,
char[] password) |
Modifier and Type | Method and Description |
---|---|
private static boolean |
allZeroes(byte[] b) |
static PKCS8Key.DecryptResult |
decrypt(java.lang.String cipher,
java.lang.String mode,
DerivedKey dk,
boolean des2,
byte[] iv,
byte[] encryptedBytes) |
private static PKCS8Key.DecryptResult |
decryptPKCS8(ASN1Structure pkcs8,
char[] password) |
static DerivedKey |
deriveKeyPKCS12(char[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
java.security.MessageDigest md) |
static DerivedKey |
deriveKeyV1(byte[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
java.security.MessageDigest md) |
static DerivedKey |
deriveKeyV2(byte[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
javax.crypto.Mac mac) |
static byte[] |
encode(DEREncodable der) |
static byte[] |
formatAsPKCS8(byte[] privateKey,
java.lang.String oid,
ASN1Structure pkcs8) |
static javax.crypto.Cipher |
generateCipher(java.lang.String cipher,
java.lang.String mode,
DerivedKey dk,
boolean des2,
byte[] iv,
boolean decryptMode) |
byte[] |
getDecryptedBytes() |
int |
getKeySize() |
java.security.PrivateKey |
getPrivateKey() |
java.security.PublicKey |
getPublicKey() |
java.lang.String |
getTransformation() |
boolean |
isDSA() |
boolean |
isRSA() |
static void |
main(java.lang.String[] args) |
private static PKCS8Key.DecryptResult |
opensslDecrypt(PEMItem item,
char[] password) |
private static byte[] |
pkcs12(int idByte,
int n,
byte[] salt,
byte[] password,
int iterationCount,
java.security.MessageDigest md)
This PKCS12 key derivation code comes from BouncyCastle.
|
public static final java.lang.String RSA_OID
public static final java.lang.String DSA_OID
public static final java.lang.String PKCS8_UNENCRYPTED
public static final java.lang.String PKCS8_ENCRYPTED
public static final java.lang.String OPENSSL_RSA
public static final java.lang.String OPENSSL_DSA
private final java.security.PrivateKey privateKey
private final byte[] decryptedBytes
private final java.lang.String transformation
private final int keySize
private final boolean isDSA
private final boolean isRSA
public PKCS8Key(java.io.InputStream in, char[] password) throws java.security.GeneralSecurityException, java.io.IOException
in
- pkcs8 file to parse (pem or der, encrypted or unencrypted)password
- password to decrypt the pkcs8 file. Ignored if the
supplied pkcs8 is already unencrypted.java.security.GeneralSecurityException
- If a parsing or decryption problem
occured.java.io.IOException
- If the supplied InputStream could not be read.public PKCS8Key(java.io.ByteArrayInputStream in, char[] password) throws java.security.GeneralSecurityException
in
- pkcs8 file to parse (pem or der, encrypted or unencrypted)password
- password to decrypt the pkcs8 file. Ignored if the
supplied pkcs8 is already unencrypted.java.security.GeneralSecurityException
- If a parsing or decryption problem
occured.public PKCS8Key(byte[] encoded, char[] password) throws java.security.GeneralSecurityException
encoded
- pkcs8 file to parse (pem or der, encrypted or unencrypted)password
- password to decrypt the pkcs8 file. Ignored if the
supplied pkcs8 is already unencrypted.java.security.GeneralSecurityException
- If a parsing or decryption problem
occured.public boolean isRSA()
public boolean isDSA()
public java.lang.String getTransformation()
public int getKeySize()
public byte[] getDecryptedBytes()
public java.security.PrivateKey getPrivateKey()
public java.security.PublicKey getPublicKey() throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
private static PKCS8Key.DecryptResult opensslDecrypt(PEMItem item, char[] password) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
public static javax.crypto.Cipher generateCipher(java.lang.String cipher, java.lang.String mode, DerivedKey dk, boolean des2, byte[] iv, boolean decryptMode) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
public static PKCS8Key.DecryptResult decrypt(java.lang.String cipher, java.lang.String mode, DerivedKey dk, boolean des2, byte[] iv, byte[] encryptedBytes) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
java.security.NoSuchAlgorithmException
javax.crypto.NoSuchPaddingException
java.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
javax.crypto.IllegalBlockSizeException
javax.crypto.BadPaddingException
private static PKCS8Key.DecryptResult decryptPKCS8(ASN1Structure pkcs8, char[] password) throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
public static DerivedKey deriveKeyV1(byte[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, java.security.MessageDigest md)
public static DerivedKey deriveKeyPKCS12(char[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, java.security.MessageDigest md)
private static byte[] pkcs12(int idByte, int n, byte[] salt, byte[] password, int iterationCount, java.security.MessageDigest md)
idByte
- 1 == key, 2 == ivn
- keysize or ivsizesalt
- 8 byte saltpassword
- passworditerationCount
- iteration-countmd
- The message digest to usepublic static DerivedKey deriveKeyV2(byte[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, javax.crypto.Mac mac) throws java.security.InvalidKeyException
java.security.InvalidKeyException
public static byte[] formatAsPKCS8(byte[] privateKey, java.lang.String oid, ASN1Structure pkcs8)
private static boolean allZeroes(byte[] b)
public static byte[] encode(DEREncodable der) throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception