//RSA公钥私钥产生 func GenRsaKey(bits int) (publicKeyStr, privateKeyStr string, err error) { // 生成私钥文件 privateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return } derStream := x509.MarshalPKCS1PrivateKey(privateKey) block := &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: derStream, } bufferPrivate := new(bytes.Buffer) err = pem.Encode(bufferPrivate, block) if err != nil { return } privateKeyStr = bufferPrivate.String() // 生成公钥文件 publicKey := &privateKey.PublicKey derPkix, err := x509.MarshalPKIXPublicKey(publicKey) if err != nil { return } block = &pem.Block{ Type: "PUBLIC KEY", Bytes: derPkix, } bufferPublic := new(bytes.Buffer) err = pem.Encode(bufferPublic, block) if err != nil { return } publicKeyStr = bufferPublic.String() log.Debug("-------------公钥----------------") log.Debug("\r", publicKeyStr) log.Debug("--------------私钥---------------") log.Debug("\r", privateKeyStr) return }