gpt4 book ai didi

java - 以编程方式在 Java 中生成自签名证书 (X509)、私钥和公钥的问题

转载 作者:行者123 更新时间:2023-12-02 12:26:24 55 4
gpt4 key购买 nike

当我尝试使用 BouncyCaSTLe 或 Sun.Security 生成证书时遇到问题。*

要求-Android API 支持 - 适用于 API 15 和 API 8

我尝试了以下方法来做到这一点..

1)我尝试使用 BouncyCaSTLe jar 和以下代码

    X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();

v3CertGen.setSerialNumber(BigInteger.valueOf(new SecureRandom().nextInt()));

v3CertGen.setIssuerDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)));
v3CertGen.setSubjectDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
//
v3CertGen.setPublicKey(KPair.getPublic());
v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption");

X509Certificate PKCertificate = v3CertGen.generateX509Certificate(KPair.getPrivate());

此代码面临的问题:

  • CertificateGenerator 已弃用
  • 未识别 X509V3CertificateGenerator 类
  • 尝试过不同版本的 bouncycaSTLe jar (1.45、1.46、1.47 和 1.57)
  • 尝试使用CertificateBuilder(代码如下)
  • 当我使用此代码时,SubjectPublicKeyInfo 此类未被识别。

        SubjectPublicKeyInfo publicKeyInfo = 
    SubjectPublicKeyInfo.getInstance(kp.getPublic().getEncoded());

    X509v3CertificateBuilder myX509v3CertificateBuilder = new X509v3CertificateBuilder(new X500Name("c=sree"), BigInteger.valueOf(new Random().nextInt(1000000)), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 *365 * 100)), new X500Name("c=sree"), publicKeyInfo);

    ContentSigner signer = new JcaContentSignerBuilder("Sha256withRSA").build(myCAPrivateKey);
    X509CertificateHolder certHolder = myX509v3CertificateBuilder.build(signer);
    X509Certificate cert = (new JcaX509CertificateConverter().getCertificate(certHolder));

    CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
    Certificate certcert = cf.generateCertificate(new ByteArrayInputStream(cert.getEncoded()));

2) 我尝试使用 Sun.Security.* 包并使用以下代码

import java.security.cert.X509Certificate;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;

public class SelfSignedCertificateGeneration {
public static void main(String[] args){
try{
CertAndKeyGen keyGen=new CertAndKeyGen("RSA","SHA1WithRSA",null);
keyGen.generate(1024);

//Generate self signed certificate
X509Certificate[] chain=new X509Certificate[1];
chain[0]=keyGen.getSelfCertificate(new X500Name("CN=ROOT"), (long)365*24*3600);

System.out.println("Certificate : "+chain[0].toString());
}catch(Exception ex){
ex.printStackTrace();
}
}

}

此代码面临的问题:

  • CertAndKeyGen 和其他一些类不可访问

**

还有其他办法吗?请推荐我。

**

最佳答案

旧版本的 Android 附带了 bouncycaSTLe 的精简版本。所以你不能相信你需要的功能是完整的。尝试包含 https://rtyley.github.io/spongycastle/ ,Android 版 Bouncy CaSTLe 的重新打包。

在gradle中指定依赖项

compile 'com.madgag.spongycastle:core:1.56.0.0'
compile 'com.madgag.spongycastle:prov:1.56.0.0'
compile 'com.madgag.spongycastle:pkix:1.56.0.0'
compile 'com.madgag.spongycastle:pg:1.56.0.0'

软件包名称已从 org.bouncycaSTLe.* 更改为 org.spongycaSTLe.*,提供程序名称已从 BC 更改为 SC

这里有一个 using spongycastle to create a selfsigned certificate 的示例

关于java - 以编程方式在 Java 中生成自签名证书 (X509)、私钥和公钥的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45452208/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com