Created
January 8, 2014 01:14
-
-
Save jyap808/8309947 to your computer and use it in GitHub Desktop.
Decrypting an ASCII armored GPG encrypted string using a passphrase protected private key in ASCII armor format
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() { | |
// 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 private key using passphrase | |
passphrase := []byte("golang") | |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { | |
fmt.Println("Decrypting private key using passphrase") | |
err := entity.PrivateKey.Decrypt(passphrase) | |
if err != nil { | |
fmt.Println("failed to decrypt key") | |
} | |
} | |
for _, subkey := range entity.Subkeys { | |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { | |
err := subkey.PrivateKey.Decrypt(passphrase) | |
if err != nil { | |
fmt.Println("failed to decrypt subkey") | |
} | |
} | |
} | |
// Decrypt armor encrypted message using decrypted 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 /* no prompt */, nil) | |
if err != nil { | |
fmt.Println("error reading message", err) | |
} | |
bytes, err := ioutil.ReadAll(md.UnverifiedBody) | |
fmt.Println("md:", string(bytes)) | |
} | |
// pub 1024R/7F98BBCE 2014-01-04 | |
// uid Golang Test (Private key password is 'golang') <[email protected]> | |
// sub 1024R/5F34A320 2014-01-04 | |
const privateKey = `-----BEGIN PGP PRI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TOWHomDXyQGDAULAZnjaOj8/pDe6poxyBluSjMJUzfD | |
pp0B/gRSx22GAQQArUMDqkGng9Cppk73UBWBd7jhhbtk0eaRQh/goUHhKJerZ4LM | |
Q21IKyIX+GQbscDpccpXMI6eThXxrL+D8G4cNb4ewvT0zc20+T91ztgT9A/4Vifc | |
EPQCErTqY/oZphAzZM1p6sRenc22e42iT0Iibd5gCs2wnSNeUzybDcuQi2EAEQEA | |
Af4DAwKScTsHLHwoQWCYayWqio8purPTonYogZSN3QwaheS2Y0NE7skdLOvP97vi | |
Rh7BktS6Dkgu0T3D39+q0O6ZO7XErvTVoas1F0HXzId4tiIicmx4tYNyWI4NrSO7 | |
6TQPz/bQe8ZN+plG5cgZowts6g6RSfQxoW21LrP8Lh+OEdcYwWf7BTukAYmD3oq9 | |
RxdfYI7hnbVGFdOqQUQNcxZkbdrsF9ITjQb/KRln5/99E1Kp1D45VpPOs7NT3orA | |
mnfSslJXVNm1uK6FDBX2iUe3JaAmgh+RLGXQXRZKJW4DGDTyYdwR4hO8cYix2+8z | |
+XuwdVDPKBnzKn190m6xpdLyvKfj1BQhX14NShPQZ3QJiMU0k4Js23XSsWs9NSxI | |
FjjE9/mOFVUH25KN+X7rzBPo2S0pMQLqyQxSLIdI2LPDxzlknctT6OoBPKPJjb7S | |
Lt5GhIA5Cz+cohfX6LePG4FkvwU32tTRBz5YNhFBizmS+YifBBgBAgAJBQJSx22G | |
AhsMAAoJEFVKIId/mLvOulED/2uUh/qjOT468XoK6Xt837w45JQPpLqiGH9KJgqF | |
rUxJMw1bIE2G606OY6hCgeE+YC8qny29hQtXhKIquUI/0A1qK3aCZhwqyqT+QjvF | |
6Xi0i/HrgQwCyBopY3uGndMbvthxU0KO0d6seMZltHDr8YaU1JvDwNFDQVuw+Rqy | |
57ET | |
=nvLl | |
-----END PGP PRIVATE KEY BLOCK-----` | |
// Encrypted by public key message for | |
// 1024R/5F34A320 2014-01-04 "Golang Test (Private key password is 'golang') <[email protected]>" | |
const encryptedMessage = `-----BEGIN PGP MESSAGE----- | |
Version: GnuPG v1 | |
hIwDBZMeL180oyABA/9DnQDx2QSk5k7f1JsqmK1kgoeEzBdNJ30DYlF0CBtHi5pQ | |
yx0Y8EOaCXC7GLKyJXqTf+3KeVpXhC3YhVjHB4W5mDv9ifM91TRFyLm8OCmYLqV1 | |
U9OANW6l3aQPG4FH/b/JnS74NCu2uEmWuBve8Qb808KSiDMclRHsAomV6XmNfdJh | |
Af37lgu/6AFbgVMBmpfkPLtn2BYLitoeKh5IbF+Fzz5dk38Ij6P94nvPN3chaq8f | |
/GsRWGttUkTsCE1YMZgPrr/uSGpxlNrdAY7KEhFHovLtsdJvMth5/n9IrDUVagwt | |
nQ== | |
=lNST | |
-----END PGP MESSAGE-----` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment