简体   繁体   English

从 PKCS12 创建私钥对象

[英]Creating PrivateKey Object from PKCS12

I created private key from PKCS12 file with this command:我使用以下命令从 PKCS12 文件创建了私钥:

openssl pkcs12 -in test.p12 -nocerts -out privateKey.pem

How can I create PrivateKey Object from this privateKey.pem file now?我现在如何从这个privateKey.pem文件创建 PrivateKey 对象?

I tried using PKCS12 file itself with this code:我尝试使用以下代码使用 PKCS12 文件本身:

 KeyStore pfx = KeyStore.getInstance("pkcs12");
 pfx.load(new FileInputStream(P12), "123456".toCharArray());
 final Enumeration<String> aliases = pfx.aliases(); //this is empty

pfx.aliases() - was empty, I verified using keytool that it is really empty, no entries. pfx.aliases() - 是空的,我使用 keytool 验证它真的是空的,没有条目。

keytool -v -list -storetype pkcs12 -keystore test.p12

Keystore type: PKCS12
Keystore provider: SunJSSE

Your keystore contains 0 entries

My question is how can I create PrivateKey using code like this:我的问题是如何使用这样的代码创建 PrivateKey:

 public static RSAPrivateKey getPrivateKey(File privateKeyFile) throws IOException {
        byte[] keyBytes = new byte[(int) privateKeyFile.length()];
        FileInputStream fis = new FileInputStream(privateKeyFile);
        fis.read(keyBytes);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);// it works only with PKCS8
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(spec);
        return privKey;
    }

The problem with this code it only works for PKCS8, I need something like this for PKCS12.此代码的问题仅适用于 PKCS8,PKCS12 我需要这样的东西。

The only way I know is a bit low-level, but it works:我知道的唯一方法有点低级,但它有效:

public PrivateKey getPrivateKey(File file) throws IOException, GeneralSecurityException {
    try (FileInputStream fileStream = new FileInputStream(file);
         DataInputStream dataStream = new DataInputStream(fileStream)) {
        byte[] keyBytes = new byte[(int) file.length()];
        dataStream.readFully(keyBytes);
        String temp = new String(keyBytes);
        String header = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
        header = header.replace("-----END PRIVATE KEY-----", "");
        byte[] decoded = new Base64().decode(header);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(spec);
    }
}

This code assumes, that required key is a RSA key.此代码假定所需的密钥是 RSA 密钥。

您可以尝试使用 KeyStore Explorer ( https://keystore-explorer.org/ ),我们使用它来代替 Java Keytool(因为我觉得它很难使用)。

Maybe this will help someone.也许这会帮助某人。

Here is how you can generate a private key from your pkcs12 certificate file.以下是从 pkcs12 证书文件生成私钥的方法。

public PrivateKey getPrivateKey(String pathToPKCS12File) {
    try {
        InputStream stream = new FileInputStream(new File(pathToPKCS12File));
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(stream, "123456".toCharArray());
        return (PrivateKey) ks.getKey(
            ks.aliases.nextElement(),
            "123456".toCharArray()
        );
    } catch (Exception e) {
        //error
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM