gpt4 book ai didi

java - 将 PGP(公共(public)) key 存储在 java keystore 中 - BouncycaSTLe

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:06 25 4
gpt4 key购买 nike

我在 SSO 的实现中使用 bouncycaSTLe (JAVA) 进行签名、加密、解密和签名验证。我有原始的 PGP 公钥和私钥,我需要将它们存储在 Java keystore 中。这些 PGP 公钥没有证书。

我知道对于公钥(根据 Keystore 的 javadoc:http://docs.oracle.com/javase/6/docs/api/java/security/KeyStore.html)我必须创建证书。创建证书后,我可以将其作为 KeyStore.TrustedCertificateEntry 导入 keystore 。但是,我无法为类型 org.bouncycaSTLe.openpgp.PGPPublicKey 创建证书条目。

我在网上搜索过,但找不到任何有效的例子:

  1. BouncycaSTLe 文档:http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation为 X.509 key 生成证书 -
  2. BouncycaSTLe 示例 - org.bouncycaSTLe.openpgp.examples.DirectKeySignature:将证书(PGPSignature 类型的对象)直接添加到 PGPPublicKey。总而言之 - 我已经签署(认证)PGPPublicKey,但我无法将这种类型的 key 存储到 java keystore 中。

    OutputStream out = new ByteArrayOutputStream();

    if (armor)
    {
    out = new ArmoredOutputStream(out);
    }

    PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(secretKeyPass.toCharArray(), "BC");

    PGPSignatureGenerator sGen = new PGPSignatureGenerator(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");

    sGen.initSign(PGPSignature.DIRECT_KEY, pgpPrivKey);

    BCPGOutputStream bOut = new BCPGOutputStream(out);

    sGen.generateOnePassVersion(false).encode(bOut);

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

    boolean isHumanReadable = true;

    spGen.setNotationData(true, isHumanReadable, notationName, notationValue);

    PGPSignatureSubpacketVector packetVector = spGen.generate();
    sGen.setHashedSubpackets(packetVector);

    bOut.flush();

    return PGPPublicKey.addCertification(keyToBeSigned, sGen.generate()).getEncoded();

我主要对编程解决方案(java 源代码)感兴趣,但使用一些工具的示例也会有所帮助。

谢谢!

最佳答案

我认为你应该从你的 PGPPublicKey 中提取一个 java.security.PublicKey 并用它来构造一个 X509Certificate ,它可以存储在一个 keystore 。

JcaPGPKeyConverter c = new JcaPGPKeyConverter();
PublicKey publicKey = c.getPublicKey(pgpPublicKey);
// ... Use Bouncy's X509V3CertificateGenerator or X509v3CertificateBuilder
// ... to construct a self-signed cert
X509Certificate x509Certificate = // ...
// ... add cert to KeyStore

要从 PublicKey 创建一个 X509Certificate,请参阅:Generate random certificates .

关于java - 将 PGP(公共(public)) key 存储在 java keystore 中 - BouncycaSTLe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10029987/

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