猿问

openpgp golang gpg 库的问题

所以我对 golang 还是很陌生,我正在努力获得一个工作示例,使用 openpgp 加密一些文本并再次解密它。


这是我到目前为止所拥有的:(https://gist.github.com/93750a142d3de4e8fdd2.git)


package main


import (

    "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 passphrase


const 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]

万千封印
浏览 354回答 2
2回答
随时随地看视频慕课网APP

相关分类

Go
我要回答