Skip to content

Instantly share code, notes, and snippets.

@jyap808
Created January 4, 2014 01:05
Show Gist options
  • Save jyap808/8250067 to your computer and use it in GitHub Desktop.
Save jyap808/8250067 to your computer and use it in GitHub Desktop.
Decrypting an ASCII armored GPG encrypted string in Golang
package main
import (
"bytes"
"code.google.com/p/go.crypto/openpgp"
"code.google.com/p/go.crypto/openpgp/armor"
"fmt"
"io/ioutil"
"log"
)
func main() {
decbuf := bytes.NewBuffer([]byte(encryptedMessage))
result, err := armor.Decode(decbuf)
if err != nil {
log.Fatal(err)
}
md, err := openpgp.ReadMessage(result.Body, nil, func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
return []byte("golang"), nil
}, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("dec version:", result.Header["Version"])
fmt.Println("dec type:", result.Type)
bytes, err := ioutil.ReadAll(md.UnverifiedBody)
fmt.Println("md:", string(bytes))
}
const encryptedMessage = `-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.15 (Darwin)
jA0EAwMCSk50dj2NcPtgySLEBzaZ+zgxODr+/7BeQPHyW4JsOrYXptQKFgQtewBg
HBi7
=dz85
-----END PGP MESSAGE-----`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment