-
-
Save rjmcguire/43c6fc393020be856a8c19ff9f82d796 to your computer and use it in GitHub Desktop.
Decrypting an ASCII armored GPG encrypted string in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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