提问人:Lei 提问时间:10/18/2023 更新时间:11/2/2023 访问量:32
获取 PGP PublicKey 指纹时出错,UserID 为空
Error getting fingerprint of PGP PublicKey, UserIDs is empty
问:
使用 bouncycastle 解析 PGP publicKey。使用 RSA 加密生成的 publicKey 可以正确读取信息,但使用 EdDsa 加密生成的 publicKey 无法正确读取信息。
这是我的代码:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.72</version>
</dependency>
@Test
public void test() throws NoSuchProviderException, IOException, PGPException {
InputStream in = PGPUtil.getDecoderStream(new FileInputStream("public_EdDsa.asc"));
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator());
List<PGPPublicKey> keys = new ArrayList<>();
Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();
while (CollectionUtils.isEmpty(keys) && rIt.hasNext()) {
PGPPublicKeyRing kRing = rIt.next();
Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
while (CollectionUtils.isEmpty(keys) && kIt.hasNext()) {
PGPPublicKey k = kIt.next();
if (k.isEncryptionKey()) {
keys.add(k);
}
}
}
for (PGPPublicKey pgpPublicKey : keys) {
System.out.println(pgpPublicKey.getKeyID());
byte[] fingerprint = pgpPublicKey.getFingerprint();
for (byte b : fingerprint) {
System.out.print(b);
}
System.out.println();
System.out.println(byte2Hex(fingerprint));
Iterator<String> userIDs = pgpPublicKey.getUserIDs();
while (userIDs.hasNext()){
String next = userIDs.next();
System.out.println(next);
}
System.out.println("============end============");
}
}
获得的指纹与克列奥帕特拉的细节不同。userID 为空。不知道是不是和弹力城堡的版本有关。我怎样才能获得正确的信息?
答:
0赞
Lei
11/2/2023
#1
由于 k.isEncryptionKey(),发现了一个子项,该子项被修改为使用 PGPPublicKeyRing.getPublicKey() 方法来解决此问题。
评论