如何使用java sun security从.p12文件中提取私钥

时间:2016-10-28 07:06:21

标签: java security private-key

这是我的计划。我收到错误"错误打开密钥文件"。请帮助我在哪里出错。

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.util.Base64;

public class ExtractionClass {
    public static void main(String args[]) {
       String  p12Password = "PassWord";
       try{
       FileInputStream file = new FileInputStream("C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12");
       System.out.println("FILE "+ file);
       KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
       keystore.load(file, p12Password.toCharArray());
       String alias = keystore.aliases().nextElement(); 
       PrivateKey key = (PrivateKey)keystore.getKey(alias, p12Password.toCharArray());
       System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
       } catch(KeyStoreException kse) {
           kse.printStackTrace();
           System.err.println("Error getting the key");  
       } catch (Exception e) {
              System.err.println("Error opening the key file");
              e.printStackTrace();
     }
   }
}

1 个答案:

答案 0 :(得分:0)

我假设您的堆栈跟踪是:

Error opening registry key 'C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12'
Error: could not find Java.dll
Error: could not find Java 2 Runtime Environment

若然,请尝试:Java Error opening registry key