Search code examples
c#gnupgopenpgp

Removing "BEGIN" and "END PGP MESSAGE" strings makes decryption break


I am using the below command to encrypt plain text using GnuPG:

gpg2.exe --encrypt --armor --recipient <<recipient>>

This returns output as

-----BEGIN PGP MESSAGE-----Version: GnuPG v2hQEMAzFXJ94q1Nm8AQf/Tld0/3dAvgFKPQVBS8bmbXChXeApeReo1ydNS+OXeIYwZOwld8ykP3G5EzfxNin2CRpcdgGu7mUFQfd5H80TVjVsCGrlT+0fhOsUrh3P3mTxl9CExVtz0R2e8Ot5aibAMXA5EsAZSJJF0IULCCWSeD49FhwTYWkOZVKrTJ6SKyRU65MLT2sEKb4Bh7EddiK2hmJ4vQx1uJYbeR+rdbyEBKPRHXy1WfJO24KBqSL3e7X1eFNfBiwUU7T1pabLtuCffdNDdBA1EOAPfUgFJi0NPaL0j5eJq2Lqpmoe62EPROIfvEr5b533d4FSHRCiH3Y8Hvea/TXaYYj5i9Ccj3K5VtJlAepwPKN16nT2BLwMPwgMPRdSMfd07g0IxPOq2WarAbgapJkJHyDetE7qWThgmMqxaiCi9mdtdkmAICZnVkDuA+f85kHpESS/WIf0o6v5l69JsHUkgfVJpsJ1FccY6gatfnML38c==DbOJ-----END PGP MESSAGE-----

Now when I decrypt the same string, it is decrypting back, but when I remove -----BEGIN PGP MESSAGE-----Version: GnuPG v2 and -----END PGP MESSAGE----- from this string, following error message is returned:

gpg: no valid OpenPGP data found.
gpg: decrypt_message failed: Unknown system error

Any Ideas?


Solution

  • The -----BEGIN PGP MESSAGE----- and -----END PGP MESSAGE----- strings are strictly required by the OpenPGP ASCII-armored message format as defined by RFC 4880, OpenPGP, 6.2. Forming ASCII Armor. You're not supposed to remove them, OpenPGP implementations like GnuPG will not identify them as ASCII-armored OpenPGP messages any more (that's why GnuPG prints gpg: no valid OpenPGP data found.).

    The version header line Version: GnuPG v2 is optional and may be removed. It is even possible to configure GnuPG so it does not add it in first place.

    You might have confused the binary OpenPGP message format with some kind of ASCII-armored message format without those strings. The binary message format holds the plain OpenPGP messages. As binary data had issues especially in e-mail in earlier days, the ASCII-armored encoding was defined. You can compare it with Base64-encoding (so only basic ASCII characters remain), with the strings you observed added so OpenPGP messages can be identified more easily by humans.