Skip to content

Instantly share code, notes, and snippets.

@jyap808
Created January 8, 2014 01:25
Show Gist options
  • Save jyap808/8310117 to your computer and use it in GitHub Desktop.
Save jyap808/8310117 to your computer and use it in GitHub Desktop.
Decrypting an ASCII armored GPG encrypted string using a private key (no passphrase) in ASCII armor format
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() {
// Read armored private key into type EntityList
// An EntityList contains one or more Entities.
// This assumes there is only one Entity involved
entitylist, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(privateKey))
if err != nil {
log.Fatal(err)
}
entity := entitylist[0]
fmt.Println("Private key from armored string:", entity.Identities)
// Decrypt armor encrypted message using private key
decbuf := bytes.NewBuffer([]byte(encryptedMessage))
result, err := armor.Decode(decbuf)
if err != nil {
log.Fatal(err)
}
md, err := openpgp.ReadMessage(result.Body, entitylist, nil, nil)
if err != nil {
fmt.Println("error reading message", err)
}
bytes, err := ioutil.ReadAll(md.UnverifiedBody)
fmt.Println("md:", string(bytes))
}
// sec 1024R/1D489768 2014-01-08
// uid Golang Test (Private key has no password) <[email protected]>
// ssb 1024R/A46346C7 2014-01-08
const privateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v1
lQHYBFLMpyYBBACaEwtBnjtwzygaZadrx2ar2NiYc9FtsWtX0tHIA58UjtrrFVgr
SVhxIfFzsk2uIB/aTdZVtEgCJPw4NSwgSjQmzPGf5I47Q+pDWfcJSpiXYZfHSa+K
MV5Pmckw/IY6L0VGjgP/sPI8nPXG3WdQZBoXY0iR0r5I4cdMPGD3RDHlmwARAQAB
AAP8DcSy5k++DxQsCMPZxLtidOvhWypKZkuLBKOdxIpyTlN/w686HFAnG6EFYynl
YDCVuBvpkeoU30Av29QAl/gZldSNRGFVxUSox0W+OIZELICc+j2pWyD5fnv3Ezs1
TNIjtFsiQGtyWZ1QFQsTSLIVtT8UZWxbvDrJw46ndJlG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s/ecZlUskW26GD/4hYEAvkOGLg4BH/oTgVqbwMRzlLtUn2
StPHXlfodc3ClaeIc0kivwivBHiLCML7v4NTB5MCAImcKRUrPGE8ZUM+C0WzTGTd
BJIeM9yRtF+H+MkmAnKJsFbDJx145C0KxANVpagEu7sz7txysWgkd7Mt3mPkKgWX
WoifBBgBAgAJBQJSzKcmAhsMAAoJEMRDjVMdSJdofBQD+gKh9E+kbugDIAejjE0I
1woNp/08ib4nRd7ZshEdykOZuvUEr7L7F6of197/x7IiL3pd+b/GTuv4h5N9rT92
U+ue0CwwhmmxYzu6Jkzet2fvmOdGFrKglnVBsreJkLXyDxAOHmFhulg7ABHVr3Vo
ybM0WVIalA8ZbghCuGrhSb0T
=HP3h
-----END PGP PRIVATE KEY BLOCK-----
`
// Encrypted by public key message for
// 1024R/A46346C7 2014-01-08 "Golang Test (Private key has no password) <[email protected]>"
const encryptedMessage = `-----BEGIN PGP MESSAGE-----
Version: GnuPG v1
hIwDa78K16RjRscBA/9rEXnQ06mAOwrhTXwkps0cYhOBc/dX7BtdEPoRLBd0zNpr
hlU2YPcmQjBaL2Zi2E64K9Ud+R3D4RHt78a1145jjPFBMpWg1z2SMgERaRtwT2++
AinkXavDTuwoyPD7X3z4Jyp+aPcvReUQhq7idb2Kl5cjnXe1Z31a9wy4wMHEItJh
ARDEtteoYHsszGqTdIQOuGrI5P/V4biteh5JpxWqeTWDiY9HR2O31kwRhh9bV3J/
0kFgWpTRqfaSmu8ZiHtfQzTGCHpbLhz2IQgOSjBevzZACbn0z7h5Ro+EfAsonGfH
3w==
=xUUr
-----END PGP MESSAGE-----`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment