使用 AndroidKeyStoreProvider 生成证书签名请求的最佳方法是什么?

Yeu*_*ung 5 java android certificate keystore csr

我读了这篇文章

它说明了如何生成一个KeyPair,但是它没有指定如何根据生成的密钥生成证书签名请求。

根据我的研究,要在 Java 中生成 CSR,来自 Web 的示例通常使用包sun.*或 BouncyCastle 库。似乎没有办法使用标准java.securityAPI生成 CSR 。我读了这个,它似乎在说同样的事情。

我别无选择,只能使用 BouncyCastle 吗?很难想象 Android 开发者不会考虑这种用法。

顺便说一下,文章还提到:

生成新的 PrivateKey 还要求您指定自签名证书将具有的初始 X.509 属性。您可以稍后用证书颁发机构签署的证书替换该证书

假设我最终获得了由证书颁发机构签署的证书。我究竟应该怎么做才能“稍后更换证书”?

Jos*_*eia 5

在 Android 上创建 CSR 的最佳方法是使用SpongyCastle ,它是BouncyCastle for Android的实现。SpongyCastle 已经为您完成了很多繁重的工作,因此它将让您的生活变得更加轻松。


我的实现很大程度上基于此处找到的答案,但使用 Android KeyStore 来确保安全性,并使用 SpongyCastle 的JcaContentSignerBuilder()而不是自定义的ContentSigner

将 SpongyCastle 添加到您的build.gradle文件中:

compile 'com.madgag.spongycastle:core:1.51.0.0'
compile 'com.madgag.spongycastle:pkix:1.51.0.0'
Run Code Online (Sandbox Code Playgroud)

在Android KeyStore中创建密钥对:

compile 'com.madgag.spongycastle:core:1.51.0.0'
compile 'com.madgag.spongycastle:pkix:1.51.0.0'
Run Code Online (Sandbox Code Playgroud)

使用所述密钥对创建 CSR:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore"); // store the key in the Android KeyStore for security purposes
keyGen.initialize(new KeyGenParameterSpec.Builder(
                  "key1",
                  KeyProperties.PURPOSE_SIGN)
                  .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                  .setDigests(KeyProperties.DIGEST_SHA256,
                                KeyProperties.DIGEST_SHA384,
                                KeyProperties.DIGEST_SHA512)
                  .build()); // defaults to RSA 2048
KeyPair keyPair = keyGen.generateKeyPair();
Run Code Online (Sandbox Code Playgroud)

就是这样,现在您有了PKCS10CertificationRequest可以发送到服务器的文件。


cyb*_*acy 2

关于在 Android 手机上生成 CSR(证书签名请求),我认为使用Spongycastle来代替是相当简单的。它是 Bouncycastle 的 Android 端口。

假设我最终获得了由证书颁发机构签名的证书。“稍后更换证书”具体应该怎么做?

一旦您获得了应从 CA(证书颁发机构)获得的实际签名证书,您就不再需要 CSR;您应该将签名的证书存储在手机上。在哪里保存它们 - 我想你可以在这里获得帮助。