获取 PGP PublicKey 指纹时出错,UserID 为空

Error getting fingerprint of PGP PublicKey, UserIDs is empty

提问人:Lei 提问时间:10/18/2023 更新时间:11/2/2023 访问量:32

问:

使用 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 为空。不知道是不是和弹力城堡的版本有关。我怎样才能获得正确的信息?

java 充气城堡 pgp openpgp eddsa

评论

0赞 Lei 10/19/2023
指纹读取代码:3f2312ab13109c287c2458f256c046a0ec7e8377
0赞 Lei 10/19/2023
克列奥帕特拉读取的指纹(Gpg4win-4.1.0):5148D08942E2C2FD2484EF04A87C2A160954B683

答:

0赞 Lei 11/2/2023 #1

由于 k.isEncryptionKey(),发现了一个子项,该子项被修改为使用 PGPPublicKeyRing.getPublicKey() 方法来解决此问题。