Skip to content

Instantly share code, notes, and snippets.

@rjmcguire
Forked from jyap808/decrypt_gpg_armor.go
Created February 17, 2017 20:51
Show Gist options
  • Save rjmcguire/43c6fc393020be856a8c19ff9f82d796 to your computer and use it in GitHub Desktop.
Save rjmcguire/43c6fc393020be856a8c19ff9f82d796 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