javabouncycastlepgpopenpgpeddsa

Error getting fingerprint of PGP PublicKey, UserIDs is empty


Use bouncycastle to parse PGP publicKey. The publicKey generated using RSA encryption can read the information correctly, but the publicKey generated using EdDsa encryption cannot read the information correctly.

this is my code:

<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============");
        }
    }

The fingerprint obtained is different from the Kleopatra details. The userIDs are empty. I don’t know if it is related to the version of bouncycastle. How can I get the correct information?


Solution

  • Due to k.isEncryptionKey(), a subkey was found, which was modified to use the PGPPublicKeyRing.getPublicKey() method to solve the problem.