public class PKCS8Key extends 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 |
|---|---|
static String |
DSA_OID |
static String |
OPENSSL_DSA |
static String |
OPENSSL_RSA |
static String |
PKCS8_ENCRYPTED |
static String |
PKCS8_UNENCRYPTED |
static String |
RSA_OID |
| Constructor and Description |
|---|
PKCS8Key(byte[] encoded,
char[] password) |
PKCS8Key(ByteArrayInputStream in,
char[] password) |
PKCS8Key(InputStream in,
char[] password) |
| Modifier and Type | Method and Description |
|---|---|
static PKCS8Key.DecryptResult |
decrypt(String cipher,
String mode,
DerivedKey dk,
boolean des2,
byte[] iv,
byte[] encryptedBytes) |
static DerivedKey |
deriveKeyPKCS12(char[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
MessageDigest md) |
static DerivedKey |
deriveKeyV1(byte[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
MessageDigest md) |
static DerivedKey |
deriveKeyV2(byte[] password,
byte[] salt,
int iterations,
int keySizeInBits,
int ivSizeInBits,
Mac mac) |
static byte[] |
encode(org.bouncycastle.asn1.ASN1Encodable der) |
static byte[] |
formatAsPKCS8(byte[] privateKey,
String oid,
org.apache.commons.ssl.ASN1Structure pkcs8) |
static Cipher |
generateCipher(String cipher,
String mode,
DerivedKey dk,
boolean des2,
byte[] iv,
boolean decryptMode) |
byte[] |
getDecryptedBytes() |
int |
getKeySize() |
PrivateKey |
getPrivateKey() |
PublicKey |
getPublicKey() |
String |
getTransformation() |
boolean |
isDSA() |
boolean |
isRSA() |
static void |
main(String[] args) |
public static final String RSA_OID
public static final String DSA_OID
public static final String PKCS8_UNENCRYPTED
public static final String PKCS8_ENCRYPTED
public static final String OPENSSL_RSA
public static final String OPENSSL_DSA
public PKCS8Key(InputStream in, char[] password) throws GeneralSecurityException, 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.GeneralSecurityException - If a parsing or decryption problem
occured.IOException - If the supplied InputStream could not be read.public PKCS8Key(ByteArrayInputStream in, char[] password) throws 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.GeneralSecurityException - If a parsing or decryption problem
occured.public PKCS8Key(byte[] encoded,
char[] password)
throws 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.GeneralSecurityException - If a parsing or decryption problem
occured.public boolean isRSA()
public boolean isDSA()
public String getTransformation()
public int getKeySize()
public byte[] getDecryptedBytes()
public PrivateKey getPrivateKey()
public PublicKey getPublicKey() throws GeneralSecurityException
GeneralSecurityExceptionpublic static Cipher generateCipher(String cipher, String mode, DerivedKey dk, boolean des2, byte[] iv, boolean decryptMode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
public static PKCS8Key.DecryptResult decrypt(String cipher, String mode, DerivedKey dk, boolean des2, byte[] iv, byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
public static DerivedKey deriveKeyV1(byte[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, MessageDigest md)
public static DerivedKey deriveKeyPKCS12(char[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, MessageDigest md)
public static DerivedKey deriveKeyV2(byte[] password, byte[] salt, int iterations, int keySizeInBits, int ivSizeInBits, Mac mac) throws InvalidKeyException
InvalidKeyExceptionpublic static byte[] formatAsPKCS8(byte[] privateKey,
String oid,
org.apache.commons.ssl.ASN1Structure pkcs8)
public static byte[] encode(org.bouncycastle.asn1.ASN1Encodable der)
throws IOException
IOExceptionCopyright © 2019. All rights reserved.