我在实施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创建证书条目。
我在网上搜索过但找不到任何有效的例子:
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源代码)感兴趣,但使用某些工具的示例也会有所帮助。
谢谢!
答案 0 :(得分:0)
我认为您应该从java.security.PublicKey
中提取PGPPublicKey
并使用它来构建可以存储在密钥库中的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
要从X509Certificate
创建PublicKey
,请参阅:Generate random certificates。
答案 1 :(得分:0)
如果您只想保存公钥,为什么不能将密钥内容保存到Java密钥库?然后检索内容并在需要时转换为PGPPublicKey对象。
首先创建一个包装类
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对象的示例。