I need to create a nested message in OpenPGP (my understanding is that the protocol itself should support it).

Let's say I have Payload ("Time in Lordran is convoluted").

I do the usual PGP encryption routine with a public key PK1, and as a result I get encrypted session key SK1 and encrypted payload EncPY1. I can combine this output OUT1 as a single PGP message.

Now, I want to do the same thing again: encrypt OUT1 but with public key PK2 to get OUT2. The problem here is that OUT1 is going to generate a Literal data packet instead of Symmetrically Encrypted Data Packet.

It means that to retrieve the Payload, the decryption of OUT2 by GnuPG will happen in two passes (gpg -d OUT2.gpg | gpg -d) instead of one (gpg -d OUT2.gpg).

Excerpt from the RFC that confirms theoretical possibility:

   The Symmetrically Encrypted Data packet contains data encrypted with
   a symmetric-key algorithm.  When it has been decrypted, it contains
   other packets (usually a literal data packet or compressed data
   packet, but in theory other Symmetrically Encrypted Data packets or
   sequences of packets that form whole OpenPGP messages).

I have tried OpenPGP implementation in Go and there is no API to facilitate what I need. I would like to know if there is a library in Java, Python or any other language that fits my use case.

So far I haven't seen anyone in the libraries that I looked at that has tested creation of nested messages (except for compression).


1 Answer 1


I figured it out. All you have to do is to use Bouncy Castle Java library. All other libraries are absolute garbage.

None of the libraries have docs, but at least this one is not a binding to a thirty-year-old C code.

  • 1
    $\begingroup$ Moderator note: please tone it down a notch and reserve your strongest adjectives for much worse code, which does exist (trust me on that one). Also, have you reviewed all libraries? $\endgroup$
    – fgrieu
    Commented Oct 26, 2020 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.