jchavannes / go-pgp

Layer on top of golang.org/x/crypto/openpgp to handle a few PGP use cases.
MIT License
60 stars 17 forks source link

Failed to decrypt message #1

Closed Nhoya closed 1 year ago

Nhoya commented 6 years ago

Hi, after i generate the encrypted message i try to decrypt it with gpg -d but it fails with gpg: no valid OpenPGP data found gpg: decrypt_message failed

the encryption is done with

func encryptStuff(person User) {
        fmt.Println(person.stuff)
        key, _ := ioutil.ReadFile(person.keypath)
        pubEntity, _ := pgp.GetEntity(key, []byte{})
        encrypted, _ := pgp.Encrypt(pubEntity, []byte(person.stuff))
        fmt.Println(string(encrypted))
}
jchavannes commented 6 years ago

@Nhoya there seem to be some discrepancies between this pgp library and command line gpg. You are not the first person to run into this.

If encrypting / decrypting both using the library it works fine. I am going to look into what is causing this issue. Thanks for reporting.

woohgit commented 6 years ago

Same issue here.

It is failing with

[wooh@carbonx katsubushiman]$  git:(master) 4M 3A gpg --version
gpg (GnuPG) 2.2.8
libgcrypt 1.8.3
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/wooh/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
woohgit commented 6 years ago

Nah, the issue is that the HEADER/FOOTER should be:

-----BEGIN PGP MESSAGE-----
...
-----END PGP MESSAGE-----

It is easy to change it. Without this lib, with the core opengpg lib it looks like:

// Encrypt encrypts the message
func Encrypt(entity *openpgp.Entity, message []byte) ([]byte, error) {
    // Create buffer to write output to
    buf := new(bytes.Buffer)

    // Create encoder
    encoderWriter, err := armor.Encode(buf, "PGP MESSAGE", make(map[string]string))
    if err != nil {
        return []byte{}, fmt.Errorf("Error creating OpenPGP armor: %v", err)
    }

    encryptorWriter, err := openpgp.Encrypt(encoderWriter, []*openpgp.Entity{entity}, nil, nil, nil)
    if err != nil {
        return []byte{}, fmt.Errorf("Error creating entity for encryption: %v", err)
    }

    messageReader := bytes.NewReader(message)
    _, err = io.Copy(encryptorWriter, messageReader)
    if err != nil {
        return []byte{}, fmt.Errorf("Error writing data to compressor: %v", err)
    }

    encryptorWriter.Close()
    encoderWriter.Close()

    return buf.Bytes(), nil
}
jchavannes commented 6 years ago

Nice find @woohgit

Auguronomics commented 4 years ago

Hi I got the same issue when try to operate Postgres armor() and the encrypted, err := pgp.Encrypt(pubEntity … 1st I go the header and footer failure : PGP MESSAGE 2nd the base 64 encoding line feed occurs at a different position : the go one :

-----BEGIN Message-----

wYwDElIW/5lzPiUBBADepdubA9ii9+kw85nHXbYx7OtBr3rL6lQym0Xwl6ucVuLq
hAoSSdWbChwnfvnli3QHIYW/kdKJ6JiV5/eQxIfJONdh/ZBm2sYrEgpM627YZFUd
V5aoxqCh6kEVZrbG+whGEtqjpgO4G3X3G1dJQRG6/kPqLyjnzGL0MKD1R6Ay79Lg
AeNqA8lvuT/f0eH2M+A/4MnhWD7gkuJ16tB14ArjoSAg4XV54GzgAeFiheB35mak
VE6c7+Sn4K7rb2izxJ0lN7xcTd0Oh9VeTkU91x+xYXwMqatcib1HYj3qfnfNcsim
f318HENZcmezByxPRF/g8uUINyeLw9at5F5rlDjJDe25j3hP6xzVWoJO5PoatO8Q
vOCd4pjHVNfg6eKmGcPe4ILjEnVK9ED7xOngcuSOCz8opQl3zC85K3aY1dfM4k1j
PeHh9WAA
=VzF7
-----END Message-----

versus the Postgres :

-----BEGIN PGP MESSAGE-----

wYwD7hmm9uYIB9oBA/4+6kkLq5LJzNIauk3U+l7zKs4zfU1s52oVqEkb2stLUHf9cx+Tkg5Tp3CW
D17kB7TDTxtVD1ZypYI9EH4xsxESEawTGWPIW6EwNbKlqwn4ztGygD1l9alxL41YkU6LnGA3Mj0w
pPkKKcrMLtkqm1pXq62Oj7e4KNPLpwcoa/HC3dKbAVZHldNS0TUyH6eXgI861FNedDMmGY8VTtkD
fCuOOyz6irqPVv7tO7lkvQO640KmJPJz7EZsHiUpBNPxV3NpCe+og+eFrVpOQtITMVfRUbxNhX2Q
xOpAjmDLmPEFX+itPZ+lqzgUhXp3sizfwjDM2yjemubnV8bajdMB3ANQZMR8XTtmixHc5wcjumME
XvoKz3imn1kAekDy6Y0=
=9Rfq
-----END PGP MESSAGE-----

Thanks for your attention great job for the lib