golang 示例 · PHP/Python/前端/Linux 等等 学习笔记 · 看云
[TOC]
> [使用的库 gopenpgp](https://github.com/ProtonMail/gopenpgp)
## goalng示例
```
package main
import (
"fmt"
"github.com/ProtonMail/gopenpgp/v2/helper"
)
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)
mQENBGO2c0ABCADq4cKbunJlzR/lEfQ3NUURT/hFYdvN5zl697RBZG0pLw0OK9jk
KuMJ0/gLscgS1uSaUfU3kghjQTHmqMSXgPAhio4LqthEym5xFOiOuC1i/6bXt20z
2fb+hrq7FSky9Q33wraEOBp0ojLc1mBzJ+FkklD4K0BrglW8ykYo+mucvexBJE/+
zTlY1fNWob5r3XL+A7RfrK4xGzT5FLP7GYKOLdNflzmjtSJLbETPFbvzWowk8PNL
XoCWziYVZCephjNRwFaI5ZTTIQKiWpAcIWeQTCzEpD/L30yyldmjtQP846rqgzrc
PfPtexfaapvk2To8/8tdc1l6tDl+pSo3ScnrABEBAAG0I2JpZ2FudC5jbiAobm9u
ZSkgPGJpZ2FudEBiaWdhbnQuY24+iQE5BBMBAgAjBQJjtnNAAhsDBwsJCAcDAgEG
FQgCCQoLBBYCAwECHgECF4AACgkQ5/ZYAQdME679kgf+O1OR1S820fTippTsmf0N
b6FFRPJcacni3ciquRy25Uc6z5C/Bvxk6cjYYhWXgFAJjUZUo0rpTxABDBzmg8SY
8UCcz9sz4eLG7wvkLE2PuHqYPveoBv9s00/Vn18Z8lLdvvigJZiNKnouIt/E8NxG
LPCpMNWiNFuFAMP92wR6UqA6Fael0qK+6nwPVCOFR2I0vmGUxn1sRLvQKznwYvcd
xKo3fIlfCTbMf171imPmGLuUneMAwObmBG9Oaldmvgao96LW3EVloUu/ijrHSzDA
An1TAhRMe4j4MlrjwSQ7gMDR1ALMBzBGNugsB05sMiVlXxHpENSBbPvwgdyCiKv4
zrkBDQRjtnNAAQgAzXCz1MlWa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K+RzxxcimlL
=IF1G
-----END PGP PRIVATE KEY BLOCK-----` // encrypted private key
var passphrase = []byte(`bigant.cn`) // Passphrase of the privKey
func main() {
// with_pass()
// with_key()
// with_signa()
}
func with_signa() {
// encrypt message using public key, sign with the private key
armor, err := helper.EncryptSignMessageArmored(pubkey, privkey, passphrase, "plain text")
if err != nil {
panic(err)
}
fmt.Printf("armor %+v\n", armor)
// decrypt armored encrypted message using the private key, verify with the public key
// err != nil if verification fails
decrypted, err := helper.DecryptVerifyMessageArmored(pubkey, privkey, passphrase, armor)
if err != nil {
panic(err)
}
fmt.Printf("decrypted %+v\n", decrypted)
}
func with_key() {
// encrypt plain text message using public key
armor, err := helper.EncryptMessageArmored(pubkey, "plain text")
if err != nil {
panic(err)
}
fmt.Printf("加密后的值:%+v\n", armor)
// decrypt armored encrypted message using the private key and obtain plain text
decrypted, err := helper.DecryptMessageArmored(privkey, passphrase, armor)
if err != nil {
panic(err)
}
fmt.Printf("解密后的值:%+v\n", decrypted)
// encrypt binary message using public key
armor2, err := helper.EncryptBinaryMessageArmored(pubkey, []byte("plain text"))
if err != nil {
panic(err)
}
fmt.Printf("加密后的二进制:%+v\n", armor2)
// decrypt armored encrypted message using the private key expecting binary data
decrypted2, err := helper.DecryptBinaryMessageArmored(privkey, passphrase, armor2)
if err != nil {
panic(err)
}
fmt.Printf("解密后的值%+v\n", decrypted2)
}
func with_pass() {
password := []byte("hunter2")
// Encrypt data with_pass password
armor, err := helper.EncryptMessageWithPassword(password, "my message")
if err != nil {
panic(err)
}
// Decrypt data with_pass password
message, err := helper.DecryptMessageWithPassword(password, armor)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", message)
}
```
加密后的数据格式未
```
-----BEGIN PGP MESSAGE-----
Version: GopenPGP 2.5.0
Comment: https://gopenpgp.org
wy4ECQMIfwvOe50uLDjgKNY8AAgm/g0jS7v4x0sCm5juqovDJcbAtwMeHGfNVndo
0jsBSPnyQ26ehpX+jPNJbspOdr1z2wnCL5Zy4U9+1EdaAW5OXnvaPw5lf1y3ajh+
isIjYqVniuxjulV53A==
=XcH/
-----END PGP MESSAGE-----
```
与一般的加密有差异