If I want to encrypt a text content file using a symmetric algorithm like AES, is there a standard format gpg will expect, where it can detect a symmetric key (encrypted with recipient gpg public key RSA) in my output? E.g. should the encrypted key be a fixed length field at the beginning of my output?

I am not able to use a suite gpg tools or command line. I am restricted to manually using a subset of cryptography APIs supported in a legacy system.

From reading up at e.g. https://futureboy.us/pgp.html#Symmetric, https://www.gnupg.org/gph/en/manual/x110.html, https://www.rfc-editor.org/rfc/rfc4880 I see that this approach seems to be in use.

But I am struggling to find any reference that would help me manually construct an appropriately structured "package".


1 Answer 1


If you encrypt a text file with the GPG utility, it randomly chooses a symmetric key, encrypts the file using that key, then encrypts the symmetric key using an asymmetric algorithm, and embeds the encrypted symmetric key in the file. There is a format that GPG uses and expects, but it is not easy to work with.

There is an RFC that describes the OpenPGP file format. It's not fun reading, but with a lot of hex dumps and a lot of time with the GPG source, I did figure out the basics of a GPG-encrypted file.

I did take the openssh project and carve out the sftp piece to build a package that encrypts the files when it puts them on the remote server and decrypts them when it reads from the remote server. The format of the files is GPG-compatible, without using the GPG utility. You might find some useful code in there for generating GPG-readable files, mostly in the iron subdirectory. IronCoreLabs/ironssh GitHub project

  • $\begingroup$ Thanks for your reply @Bob Wall. I will take a look at that GPG-readable related code as you suggest. $\endgroup$
    – MikeRoger
    Commented Dec 5, 2018 at 9:00
  • 1
    $\begingroup$ @MikeRoger, it's been quite a while since I looked at it, but you can contact me via GitHub if you have any questions - might be able to dredge up some memories of the file format and some of its quirks and foibles. $\endgroup$
    – Bob Wall
    Commented Dec 5, 2018 at 17:54

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.