How read a PKCS8 encrypted Private key which is also encoded in DER with bouncycastle?

时间:2018-04-20 01:01:46

标签: java cryptography bouncycastle pem der

I have tried answers of these questions:

Bouncy Castle : PEMReader => PEMParser

Read an encrypted private key with bouncycastle/spongycastle

However as my encrypted key is encoded in DER when I call

Object object = pemParser.readObject(); 

object is null.

I can convert it to PEM with this openssl's command (it decrypts the key too)

openssl pkcs8 -inform der -in pkey.key -out pkey.pem 

but I need to read the key in its original file

2 个答案:

答案 0 :(得分:4)

这些Q都是关于使用OpenSSL的传统PEM解析和解密文件的。加密。您正在使用虽然相似但不同的PKCS8加密,因此Reading PKCS8 in PEM format: Cannot find provider更接近。您可以使用大部分方法,但跳过PEM解析:

import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo; // NOT the 
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;       // javax ones!
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.bouncycastle.operator.InputDecryptorProvider;
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;

    // args[0] = filename args[1] = password
    FileInputStream fis = new FileInputStream(args[0]);
    byte[] buff = new byte[9999]; int len = fis.read(buff); fis.close();
    // could use File.readAllBytes in j8 but my dev machine is old

    // create what PEMParser would have 
    ASN1Sequence derseq = ASN1Sequence.getInstance (Arrays.copyOf(buff,len));
    PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo.getInstance(derseq));
    // decrypt and convert key
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
    PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
    PrivateKey key = converter.getPrivateKey(keyInfo);

    // now actually use key, this is just a dummy
    System.out.println (key.getAlgorithm());

答案 1 :(得分:0)

通过使用SpongyCastle,我们可以获得良好的结果。我做了一些代码,将获得期望的结果。 海绵城堡罐子可以从下面下载: coreprovpkix

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;

// need core.jar, prov.jar, bcpkix-jdk15on.jar
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
import org.spongycastle.asn1.pkcs.PrivateKeyInfo;
import org.spongycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.spongycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.spongycastle.operator.InputDecryptorProvider;
import org.spongycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;

class Decrypt {

    private static void usage() {
        System.err.println(" Usage: encrypted.pk8 password out_decrypted.pk8");
        System.exit(2);
    }

    public static byte[] copyOf(byte[] original, int newLength) {
        byte[] copy = new byte[newLength];
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }

    public static void loadProvider(String providerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class providerClass = Class.forName(providerClassName);
        Provider provider = (Provider)providerClass.newInstance();
//        Security.insertProviderAt(provider, 1);
        Security.addProvider(provider);
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 3) usage();

        loadProvider("org.spongycastle.jce.provider.BouncyCastleProvider");

        FileInputStream fis = new FileInputStream(args[0]);
        byte[] buff = new byte[fis.available()];
        int len = fis.read(buff);
        fis.close();

        // create what PEMParser would have
        ASN1Sequence derseq = ASN1Sequence.getInstance(copyOf(buff, len));
        EncryptedPrivateKeyInfo epkInfo = EncryptedPrivateKeyInfo.getInstance(derseq);
        PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(epkInfo);

        // decrypt and convert key
        InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
        PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("SC");
        PrivateKey key = converter.getPrivateKey(keyInfo);

        // now actually use key, this is just a dummy
        System.out.println(key.getAlgorithm());

        FileOutputStream fos = new FileOutputStream(args[2]);
        fos.write(key.getEncoded());
        fos.close();
    }
}