Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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
PKCS#12在Android设备上创建密钥库_Android_Bouncycastle_Keystore_Pfx_Android Keystore - Fatal编程技术网

PKCS#12在Android设备上创建密钥库

PKCS#12在Android设备上创建密钥库,android,bouncycastle,keystore,pfx,android-keystore,Android,Bouncycastle,Keystore,Pfx,Android Keystore,我目前正在开发一个Android应用程序,它生成RSA密钥对,构建并向一个CA发送PKCS#10认证请求,等待CA的响应(其中包含证书链,包括为最终实体颁发的证书),然后构建PKCS#12密钥库,安装在Android密钥库中 我只是在下面向您展示一些代码: // It removes Android-BC Security.removeProvider("BC"); // I've tried with SpongyCastle but it also fails. // I've previo

我目前正在开发一个Android应用程序,它生成RSA密钥对,构建并向一个CA发送PKCS#10认证请求,等待CA的响应(其中包含证书链,包括为最终实体颁发的证书),然后构建PKCS#12密钥库,安装在Android密钥库中

我只是在下面向您展示一些代码:

// It removes Android-BC
Security.removeProvider("BC");
// I've tried with SpongyCastle but it also fails.
// I've previously imported BC jars.
Security.addProvider(new BouncyCastleProvider()); 
KeyStore ks = KeyStore.getInstance("PKCS12","BC");
ks.load(null);

// KeyPair generated by RSA with BC, and certificates obtained from CA.
PrivateKey prvK;
Certificate eeCert, caCert;

PKCS12BagAttributeCarrier caCertBagAttr = 
   (PKCS12BagAttributeCarrier) caCert;
caCertBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("CA"));

PKCS12BagAttributeCarrier certBagAttr =
   (PKCS12BagAttributeCarrier) eeCert;
certBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("EE Certificate"));
certBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_localKeyId,
   new SubjectKeyIdentifierStructure(eeCert.getPublicKey()));

Certificate[] chain = new Certificate[2];
chain[1] = caCert;
chain[0] = eeCert;

PKCS12BagAttributeCarrier privBagAttr = 
   (PKCS12BagAttributeCarrier) prvK;
privBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_friendlyName,
   new DERBMPString("EE Certificate Key"));
privBagAttr.setBagAttribute(
   PKCSObjectIdentifiers.pkcs_9_at_localKeyId,
   new SubjectKeyIdentifierStructure(eeCert.getPublicKey()));

// Finally, keystore is saved to file.
ks.setKeyEntry("P12", prvK, null, chain);
saveP12File(ks);
问题是,当我在我的Windows PC(BouncyCastle 1.49)上运行它时,一切都正常,但当我在Android上运行它时创建的PKCS#12文件不能用于SSL握手或签名,因为Logcat说它没有任何相关的私钥

如果我在加载文件后在Java上打印密钥库的全部内容,似乎一切都正常,但当我在Android浏览器上使用密钥库或在Windows上安装密钥库时,它的私钥无法使用

Android和PC上的相同代码,BC的相同版本,PKCS#12似乎没有格式错误

有人能帮我吗? 提前谢谢

编辑

我构建了一个servlet,它生成一个RSA密钥对,并以PEM格式发送回我的应用程序。 看起来一切正常:

  • 我可以在Android设备上或通过外部servlet生成有效的RSA密钥对(使用公共加密,解密测试效果良好)
  • 我从CA收到CA和EE证书
  • 我能够构建一个“有效的”PKCS12(我称之为有效的,因为它可以被Android和Windows读取)
但是:

  • 我无法使用EE证书作为握手的客户端证书打开SSL通道。在Windows或Android上
我已上载到我的Dropbox:

  • 在信任库为空的情况下对Tomcat进行测试(无论哪个CA颁发了客户端证书)未成功


    有人看到什么可以解释吗?

    如果你在PC和手机上分别创建了两个密钥存储,然后在生成的文件上尝试二进制差异,会怎么样?照你说的做,我发现问题不在于PKCS#12创建。这是RSA密钥对的生成。我不知道为什么,但当我在Android上生成RSA密钥对,保存到PEM文件,然后加载到Java PC应用程序时,CRT效率是不同的。有人知道为什么吗?你能展示演示问题的代码吗?我在我的问题中添加了一些新的细节…问题解决了。我用错误的私钥创建了PKCS#12密钥库。BC和Android均未显示故障消息。谢谢迪瓦诺夫的帮助。