为了账号安全,请及时绑定邮箱和手机立即绑定

openpgp golang gpg 库的问题

openpgp golang gpg 库的问题

Go
万千封印 2021-08-16 16:02:07
所以我对 golang 还是很陌生,我正在努力获得一个工作示例,使用 openpgp 加密一些文本并再次解密它。这是我到目前为止所拥有的:(https://gist.github.com/93750a142d3de4e8fdd2.git)package mainimport (    "log"    "bytes"    "code.google.com/p/go.crypto/openpgp"    "encoding/base64"    "io/ioutil"    "os")// create gpg keys with// $ gpg --gen-key// ensure you correct paths and passphraseconst mysecretstring = "this is so very secret!"const secretKeyring = "/Users/stuart-warren/.gnupg/secring.gpg"const publicKeyring = "/Users/stuart-warren/.gnupg/pubring.gpg"const passphrase = "1234"func main() {    log.Printf("Secret: ", mysecretstring)    log.Printf("Secret Keyring: ", secretKeyring)    log.Printf("Public Keyring: ", publicKeyring)    log.Printf("Passphrase: ", passphrase)    // Read in public key    keyringFileBuffer, _ := os.Open(publicKeyring)    defer keyringFileBuffer.Close()    entitylist, _ := openpgp.ReadKeyRing(keyringFileBuffer)    // encrypt string    buf := new(bytes.Buffer)    w, _ := openpgp.Encrypt(buf, entitylist, nil, nil, nil)    w.Write([]byte(mysecretstring))    // Encode to base64    bytesp, _ := ioutil.ReadAll(buf)    encstr := base64.StdEncoding.EncodeToString(bytesp)    // Output encrypted/encoded string    log.Printf("Encrypted Secret: ", encstr)    // Here is where I would transfer the encrypted string to someone else     // but we'll just decrypt it in the same code    // init some vars    var entity2 *openpgp.Entity    var entitylist2 openpgp.EntityList    // Open the private key file    keyringFileBuffer2, _ := os.Open(secretKeyring)    defer keyringFileBuffer2.Close()    entitylist2, _ = openpgp.ReadKeyRing(keyringFileBuffer2)    entity2 = entitylist2[0]
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 353 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信