繁体   English   中英

将 PGP(公共)密钥存储在 java 密钥库 - Bouncycastle

[英]Store PGP (public) keys in java keystore - Bouncycastle

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

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

我搜索了 web 但找不到任何有效的示例:

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

     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

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 ,请参阅: 生成随机证书

如果只想保存公钥,为什么不能直接将密钥内容保存到Java keystore中呢? 然后检索内容并在需要时转换为 PGPPublicKey object。

首先创建一个包装器 class

public class PgpPublicKeyWrapper implements Key {
    private final String keyContent;
    public PgpPublicKeyWrapper(final String keyContent) {
        this.keyContent = keyContent;
    }
    @Override
    public String getAlgorithm() {
        return "PGP-PublicKey"; // you can call whatever you want
    }
    @Override
    public String getFormat() {
        return "RAW"; // has to be raw format
    }
    @Override
    public byte[] getEncoded() {
        return keyContent.getBytes();
    }
}

然后你可以这样做来保存它

keyStore.setKeyEntry("think a name for alias", new PgpPublicKeyWrapper(key), PASSWORD, null);

当你想找回它

Key key = this.keyStore.getKey(alias, PASSWORD);
InputStream is = new ByteArrayInputStream(key.getEncoded());
PGPPublicKey publicKey = readPublicKey(is); 

对于readPublicKey(),你可以在网上找到很多关于如何读取InputStream到一个PGPPublicKey object的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM