Java实现中的BouncyCastle PGP textmode不会转换为CR / LF

时间:2016-10-20 03:18:00

标签: java encryption bouncycastle pgp openpgp

有没有办法在BouncyCastle PGP加密Java实现中指定textmode?

我尝试了这个,但没有运气(在Windows中以UNIX行结束和解密加密):

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

1 个答案:

答案 0 :(得分:1)

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.