-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_key_decrypt_gpg_base64.go
106 lines (96 loc) · 4.56 KB
/
public_key_decrypt_gpg_base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bytes"
"code.google.com/p/go.crypto/openpgp"
// "github.com/perkeep/perkeep/third_party/code.google.com/p/go.crypto/openpgp"
// "github.com/perkeep/perkeep/third_party/code.google.com/p/go.crypto/openpgp" "v0.0.0-20140806175519-2f1a4f06cb96"
// "github.com/perkeep/[email protected]/third_party/code.google.com/p/go.crypto/openpgp"
// "golang.org/x/crypto/openpgp"
"encoding/base64"
"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 base64 encoded encrypted message using decrypted private key
dec, err := base64.StdEncoding.DecodeString(base64EncryptedMessage)
if err != nil {
fmt.Println("error:", err)
return
}
md, err := openpgp.ReadMessage(bytes.NewBuffer(dec), 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 PRIVATE KEY BLOCK-----
Version: GnuPG v1
lQH+BFLHbYYBBADCjgKHmPmwBxI3c3DPVoSdu0+EJl/EsS2HEaN63dnLkGsMAs+4
32wsywmMrzKqCL40sbhJVYBcfe0chL+cry4O54DX7+gA0ZSVzFUN2EGocnkaHzyS
fuUtBdCTmoWZZAGFiBwlIS7aE/86SOyHksFo8LRC9W/GIWQS2PbcadvUywARAQAB
/gMDApJxOwcsfChBYCCmhOAvotKdYcy7nuG7dyGDBlpclLJtH/PaakKSE33NtEj4
1fyixQOdwApxvuQ2P0VX3pie/De1KpbeqXfnPLsmsXQwrRPOo38T5zeJ5ToWUGDC
Oia69ep3kmHbAW41EBH/uk/nMM91QUdl4mkYsc3dhVOXbmf0xyRoP/Afqha4UhdZ
0XKlIZP1a5+3NF/Q6dAVG0+FlO5Hcai8n98jW0id8Yf6zI+1gFGvYYKhlifkdJeK
Nf4YEvOXALEvaQqkcJOxEca+BmqsgCIFctJe9Bahx97Ep5hP7AH0aBmtZfmGmZwB
GYoevUtKa4ASVmK8RaddBvIjcrWsoAsYMpDGYaE0fcdtxsBf3uT1Q8IMsT+ZRjjV
TfvJ8aW14ZrLI98KdtXaOPZs91mML+3iw1c/1O/IEJfwxrUni2p/fDmCYU9eHR3u
Q0PwVR0MCUHI1fGuUoetW2gYIxfklvBtEFWW1BD6fCpCtERHb2xhbmcgVGVzdCAo
UHJpdmF0ZSBrZXkgcGFzc3dvcmQgaXMgJ2dvbGFuZycpIDxnb2xhbmd0ZXN0QHRl
c3QuY29tPoi4BBMBAgAiBQJSx22GAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIX
gAAKCRB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w+Rqy
57ET
=nvLl
-----END PGP PRIVATE KEY BLOCK-----`
// Encrypted by public key message for
// pub 1024R/7F98BBCE 2014-01-04
// uid Golang Test (Private key password is 'golang') <[email protected]>
// echo -n "Test base64 GPG public key encrypted string" | gpg -er 7F98BBCE | base64
const base64EncryptedMessage = `hIwDBZMeL180oyABA/96IfZ30BGsXIUD2Wul/vgWSFY/1fhVZAbpuoqLCZdXiYfnR3nXejmJb3kW5fKM+FsDku4T0p+Ax+hBZSfA51qzX8kECh04ASZpUIcaFQ+lpw8b6DjZf3EaTNqyWymqEAaupWUIm9U+K64GzzJlxtgEqxKGywLzajDuY5l+y0t+UdJmAVKZsJU8jM7HXgvv3CwT67mmFmbrjKATragcgDJ8lcyHObT2HAFLAWXqKIO3WaZwrNCMVaOdj0LUcpVk1/ho1lNqbmdbjgwnnT4e5bPl3JVUHjUuRnCDg0vdsRptHv+AOTQi2l9I`