encryptionbouncycastlepgppgpainlesspgpainless-sop

"No key data found" when using pgpainless to import key


I'm on Mac and I've created and exported a key:

% gpg --generate-key
% gpg --export --armor --output new.pub B42B1AF5 

the contents of which you can see here:

-----BEGIN PGP PUBLIC KEY BLOCK-----

mDMEZ1qw5BYJKwYBBAHaRw8BAQdAnS7bhQ3Knq2lrnLs/OLjaubtBX8hV2asKd0q
2aSfoPO0HHNhaGFuZCA8c2FoYW5kekBob3RtYWlsLmNvbT6ImQQTFgoAQRYhBLXY
surbx74eVf/7uUewZ0+0Kxr1BQJnWrDkAhsDBQkFo5qABQsJCAcCAiICBhUKCQgL
AgQWAgMBAh4HAheAAAoJEEewZ0+0Kxr1GAYA/jGm3n7zzXU7XzkJKDMOitECB1OS
St/OeIDpIjg0PL9gAQDySb5KgFBEslg+TvJ7GwYqZVaTXZoNVg7+VTvVXxvDAbg4
BGdasOQSCisGAQQBl1UBBQEBB0B2OXM9tUPz/4yakILx8PtrikrqxuOSIN3I/wEU
vmvrTQMBCAeIfgQYFgoAJhYhBLXYsurbx74eVf/7uUewZ0+0Kxr1BQJnWrDkAhsM
BQkFo5qAAAoJEEewZ0+0Kxr1jtsBAPYjwX9Q+CXJW5adGz67N9zZzKSsRv+gd4Xc
1g8PbD5BAQD3rIqPWvU5XjDQ8o877XiXoOCLs+vFWk06pOF0Tj+bBw==
=sKDL
-----END PGP PUBLIC KEY BLOCK-----

My code, using the PGPainless-SOP library:

import sop.SOP;

SOP sop = new SOPImpl();
byte[] cert = sop.extractCert()
        .key(new FileInputStream(encryptionKeyPath))
        .getBytes();

The dependency:

<dependency>
    <groupId>org.pgpainless</groupId>
    <artifactId>pgpainless-sop</artifactId>
    <version>1.7.2</version>
</dependency>

The exception:

sop.exception.SOPGPException$BadData: org.bouncycastle.openpgp.PGPException: No key data found.

    at org.pgpainless.sop.KeyReader$Companion.readSecretKeys(KeyReader.kt:39)
    at org.pgpainless.sop.ExtractCertImpl.key(ExtractCertImpl.kt:22)
    at con.com.pkg.uploader.utils.app.encryptInputStream(Utils.java:94)
    at con.com.pkg.uploader.utils.app.encryptInputStreamTest(UtilsTest.java:22)
Caused by: org.bouncycastle.openpgp.PGPException: No key data found.
    ... 45 more

Why is no key data found? I've tried with another key too, same result.


Solution

  • Looking at the documentation reveals the mistake. Using the terminology of OpenPGP and the package authors, the extractCert() method gets a public key (certificate) from a secret key, but you don't have that. Instead, you have an armored public key.

    To get a certificate from the armored public key, use PGPainless.readKeyRing().publicKeyRing():

    PGPPublicKeyRing cert = PGPainless.readKeyRing().publicKeyRing(PGP_PUBKEY);