我想生成 ssh 密钥,公钥和私钥,并以字符串形式返回,但我不知道如何将类型 *pem.Block 转换为字符串。
这是我当前的代码:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"fmt"
"bytes"
"bufio"
)
func Keymaker() {
reader := rand.Reader
bitSize := 2048
key, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
//return nil, nil, err
}
publicKey := key.PublicKey
var privateKey = &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
asn1Bytes, err := asn1.Marshal(publicKey)
if err != nil {
//return nil, nil, err
}
var pemkey = &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
var PublicKeyRow bytes.Buffer
err = pem.Encode(bufio.NewWriter(&PublicKeyRow), pemkey)
fmt.Println("public_key : ", PublicKeyRow)
fmt.Println("private_key : ", privateKey )
return
}
func main() {
Keymaker()
}
这是我当前的错误:
# command-line-arguments
./dkim.go:46:38: cannot convert privateKey (type *pem.Block) to type string
我需要字符串格式,因为我想将密钥存储在数据库中,如何将 (type *pem.Block) 转换为 string 类型?以及如何将(类型 bytes.Buffer)转换为类型字符串?
慕桂英4014372
相关分类