Example usage for org.bouncycastle.asn1.pkcs PKCS12PBEParams getInstance

List of usage examples for org.bouncycastle.asn1.pkcs PKCS12PBEParams getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PKCS12PBEParams getInstance.

Prototype

public static PKCS12PBEParams getInstance(Object obj) 

Source Link

Usage

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static PrivateKey derivePrivateKeyPBES1(EncryptedPrivateKeyInfo eIn, AlgorithmIdentifier algId,
        char[] password) throws GeneralSecurityException, IOException {
    // From BC's PEMReader
    PKCS12PBEParams pkcs12Params = PKCS12PBEParams.getInstance(algId.getParameters());
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParams = new PBEParameterSpec(pkcs12Params.getIV(),
            pkcs12Params.getIterations().intValue());

    //String algorithm = algId.getAlgorithm().getId();
    String algorithm = ASN1Registry.o2a(algId.getAlgorithm());
    algorithm = (algorithm.split("-"))[0];

    SecretKeyFactory secKeyFact = SecretKeyFactory.getInstance(algorithm);

    Cipher cipher = Cipher.getInstance(algorithm);

    cipher.init(Cipher.DECRYPT_MODE, secKeyFact.generateSecret(pbeSpec), pbeParams);

    PrivateKeyInfo pInfo = PrivateKeyInfo
            .getInstance(ASN1Primitive.fromByteArray(cipher.doFinal(eIn.getEncryptedData())));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pInfo.getEncoded());

    String keyFactAlg = ASN1Registry.o2a(pInfo.getPrivateKeyAlgorithm().getAlgorithm());

    // TODO: Can we just set it to RSA as in derivePrivateKeyPBES2?
    KeyFactory keyFact;/*www  . ja  v a 2  s.c  om*/
    if (keyFactAlg.startsWith("dsa")) {
        keyFact = KeyFactory.getInstance("DSA");
    } else {
        keyFact = KeyFactory.getInstance("RSA");
    }

    return keyFact.generatePrivate(keySpec);
}