Java 如何使用bouncy castle库从PGP公钥中获取密钥ID?
我正在尝试从PGP公钥获取密钥id。经过大量的谷歌搜索,我找到了一个使用BouncyCastleJavaAPI读取公钥的解决方案 其代码段如下所示:Java 如何使用bouncy castle库从PGP公钥中获取密钥ID?,java,cryptography,bouncycastle,public-key,pgp,Java,Cryptography,Bouncycastle,Public Key,Pgp,我正在尝试从PGP公钥获取密钥id。经过大量的谷歌搜索,我找到了一个使用BouncyCastleJavaAPI读取公钥的解决方案 其代码段如下所示: public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException { in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in); PGPPub
public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);
PGPPublicKey key = null;
//
// iterate through the key rings.
//
Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();
while (key == null && rIt.hasNext()) {
PGPPublicKeyRing kRing = rIt.next();
Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
while (key == null && kIt.hasNext()) {
PGPPublicKey k = kIt.next();
if (k.isEncryptionKey()) {
key = k;
}
}
}
if (key == null) {
throw new IllegalArgumentException("Can't find encryption(public) key in key ring.");
}
return key;
}
需要替换为
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator());
或
我尝试了BC和JCA键盘指纹计算器,它们都给出了相同的键盘ID
所以问题是,
- 这里应该使用哪个指纹计算器
- 是否存在相关/需要选择其中一个的特定场景? 我检查了两个计算器类的javadocs,但没有提供太多信息
- 是否有其他解决方案可以获取java中PGP公钥的密钥id?如果是,请建议
谢谢。我有完全相同的问题,这似乎没有记录在API中。简而言之,BC实现是针对BC的,并且具有一些标准Java实现JCE所不具备的特性和算法。
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator());
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new BcKeyFingerprintCalculator());