keybase / go-crypto

[mirror] Go supplementary cryptography libraries
https://godoc.org/golang.org/x/crypto
BSD 3-Clause "New" or "Revised" License
50 stars 20 forks source link

file that encrypted by go-openPGP can not be decrypted by GPG (Low probability) #83

Closed izouxv closed 5 years ago

izouxv commented 5 years ago

BUG info

file that encrypted by go-openPGP can not be decrypted by GPG.
file that encrypted by go-openPGP can  be decrypted by go-openPGP.
file that encrypted by PGP can  be decrypted by go-openPGP.

go (encrypt )      =>    gpg ( decrypt )  ERR
gpg (encrypt )    =>  go (decrypt )    OK
go ( encrypt)      =>   go (decrypt )    OK

this test data package have a test file that can reproduce the err

part of code


func encryptFile(t *testing.T, sourcefile, targetfile string, entity *openpgp.Entity) {
    in, err := os.Open(sourcefile)
    if err != nil {
        t.Fatal(err)
    }
    defer in.Close()
    w, err := encryptWithEntitle(t, entity, in)
    if err != nil {
        t.Fatal(err)
    }
    // defer w.Close()
    ww, err := os.Create(targetfile)
    if err != nil {
        t.Fatal(err)
    }
    defer ww.Close()
    len, err := io.Copy(ww, w)
    log.Printf("stream encrypted, len: %v", len)
    if err != nil {
        t.Fatal(err)
    }
}
func encryptWithEntitle(t *testing.T, entity *openpgp.Entity, data io.Reader) (io.Reader, error) {
    entityList := openpgp.EntityList{entity}
    buf := new(bytes.Buffer)
    wc, err := openpgp.Encrypt(buf, entityList, nil, nil, config)
    if err != nil {
        t.Errorf("encrypt err: %v", err)
        return nil, err
    }
    writelen, err := io.Copy(wc, data)
    defer wc.Close()
    t.Logf("encryptStream, len: %v, err: %v", writelen, err)
    return buf, nil
}

func TestOpenPGP_GoEncrypt_GpgDecrypt(t *testing.T) {
    entile, prikey2, err := createPgpKeyAndFile(t)
    if err != nil {
        t.Fatalf("create pgp key err: %v", err)
    }
    importKeyToKeyChain(t, fullPath(kAscName), prikey2)
    t.Log("encrypt with go openpgpg")
    encryptFile(t, fullPath(kOriginName), fullPath(kGpgName), entile)

    t.Log("decrypt with gpg")
    Run(kTestDir, "gpg", []string{"--output", kTestNewName, "--recipient", kEmail, "--decrypt", kGpgName})

    t.Log("file md5 caculating...")
    testNewNameMd5 := MD5(fullPath(kTestNewName))
    originNameMd5 := MD5(fullPath(kOriginName))

    if testNewNameMd5 != originNameMd5 {
        t.Fatalf("origin file md5 not equal the new file md5")
    }
}

gpg command line

gpg --output origin.png --recipient email@email.com --decrypt test.png.gpg

env information

gpg (GnuPG) 2.2.16
libgcrypt 1.8.4
mac os 10.15
go version go1.13.1 darwin/amd64

full test code:

[testdata.zip](https://github.com/keybase/go-crypto/files/3782426/testdata.zip)