Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BouncyCastleProvider无法从PKCS#12文件获取私钥和证书链_Java_Bouncycastle_Private Key_Pkcs#12_Pfx - Fatal编程技术网

Java BouncyCastleProvider无法从PKCS#12文件获取私钥和证书链

Java BouncyCastleProvider无法从PKCS#12文件获取私钥和证书链,java,bouncycastle,private-key,pkcs#12,pfx,Java,Bouncycastle,Private Key,Pkcs#12,Pfx,我在Windows上导出了文件cert.pfx。这个文件包含我的证书。在Ubuntu上,我可以用密码打开它,然后看到证书。但当我加载此文件时: BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = ks = KeyStore.getInstance("pkcs12", provider.getName()); ks.load(new Fil

我在Windows上导出了文件
cert.pfx
。这个文件包含我的证书。在Ubuntu上,我可以用密码打开它,然后看到证书。但当我加载此文件时:

BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
KeyStore ks = ks = KeyStore.getInstance("pkcs12", provider.getName());
ks.load(new FileInputStream("/home/test/.cert.pfx", "xxxxxx".toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, pts.getRandom());
Certificate[] chain = ks.getCertificateChain(alias);
在这个操作之后,我在
pk
chain
中有
null


此外,我已在
java.security中注册了BC provider。我将
BouncyCastleProvider
更改为
SunJSSE
。现在我的正确代码是:

String providerName = "SunJSSE";
KeyStore ks = ks = KeyStore.getInstance("pkcs12", providerName);
ks.load(new FileInputStream("/home/test/.cert.pfx", "xxxxxx".toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, pts.getRandom());
Certificate[] chain = ks.getCertificateChain(alias);
现在,
pk
中是我的私钥,
chain
中是证书链。
我不知道为什么BC不起作用。我有其他证书,BC提供商工作正常