Search code examples
javaencryptionbouncycastlepgpopenpgp

BouncyCastle PGP textmode in Java implementation does not convert to CR/LF


Is there any way to specify the textmode in BouncyCastle PGP encryption Java implementation?

I tried this but no luck (encrypted with UNIX line ending and decrypted in Windows):

PGPLiteralDataGenerator pgpldg = new PGPLiteralDataGenerator(false);
OutputStream ldout = pgpldg.open(compout, PGPLiteralData.TEXT, name, data.length, PGPLiteralData.NOW);

Solution

  • RFC 4880, OpenPGP, 5.9. Literal Data Packet (Tag 11) defines that in text mode, data should be encoded with <CR><LF> line endings:

    Text data is stored with text endings (i.e., network- normal line endings). These should be converted to native line endings by the receiving software.

    GnuPG is doing so (--compress-algo 0 disables compression, --store just wraps the input in a literal data packet):

    $ echo -e "foo\nbar" | gpg2 --textmode --compress-algo 0 --store | hexdump -c
    0000000   � 020   t  \0   X  \b   �   u   f   o   o  \r  \n   b   a   r
    0000010  \r  \n                                                        
    0000012
    

    Reading through BouncyCastle's source code for PGPLiteralDataGenerator and other called classes, I cannot find a single trace BouncyCastle is doing this (required) conversion. All I can find is that they write the encoding into the header (t, u or b). This is a BouncyCastle bug. They might fix it if you report it, otherwise (or until then), you have to add the carriage return on your own.