Golang - RSA公钥加密和私钥解密

代码示例:

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"fmt"
)

var publicKeyStr = `-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAv4o7vRNFC/D5C+qGIU0afPsQexjpZa1e2cKBc6Wx4t6W3F0AIk7H
74mYNIy4c2lJXvjo6Bxp/L0/C7hH3yKnNf+jhbyPWbkfyiZf+ORjNc3yDU1+gio2
8BL4XaZNkgfLMlEwgH75pZ7M4Zi91tj89TrW4tyNmdEyNIFuJMu35SkO6MNNQzUj
+jYX+H+ossnOLsfc1SE4AosrIVwwL8m6eB72Au/yabGc9tabUiz6HMG01j8pJfEc
uZv7dWUxPGqG5k75I0ilvIUVMFIehSwQhSK9z7Yhh0iWt9NT5TpCv35TJ8jlVm91
jw8Cqpcsj6/mBy8kAXeklV3xXElDeewX3wIDAQAB
-----END RSA PUBLIC KEY-----
`
var privateKeyStr = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAv4o7vRNFC/D5C+qGIU0afPsQexjpZa1e2cKBc6Wx4t6W3F0A
Ik7H74mYNIy4c2lJXvjo6Bxp/L0/C7hH3yKnNf+jhbyPWbkfyiZf+ORjNc3yDU1+
gio28BL4XaZNkgfLMlEwgH75pZ7M4Zi91tj89TrW4tyNmdEyNIFuJMu35SkO6MNN
QzUj+jYX+H+ossnOLsfc1SE4AosrIVwwL8m6eB72Au/yabGc9tabUiz6HMG01j8p
JfEcuZv7dWUxPGqG5k75I0ilvIUVMFIehSwQhSK9z7Yhh0iWt9NT5TpCv35TJ8jl
Vm91jw8Cqpcsj6/mBy8kAXeklV3xXElDeewX3wIDAQABAoIBAQC3e3gWe6sc8U3h
eFvc8rt5FhiHkymh2R6Pg5/2ZLevGINzvTMvqkAk2q4PSO44wJckf1S9ZNqy3abT
V0iiG9QKCkVfC25XCNvHDlPJXR8cn5hL3fxePIo8GDALpg+Q4B5qqRPJQ9lqK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ugCjsvInutmTk8FqEUNL
-----END RSA PRIVATE KEY-----
`

func main() {
	//fmt.Println("privateKeyStr: ", privateKeyStr)
	// 解析 PEM 格式的私钥数据
	block, _ := pem.Decode([]byte(privateKeyStr))
	//fmt.Println("block: ", block)

	if block == nil {
		panic("failed to parse PEM block containing the private key")
	}
	// 解析 RSA 私钥
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		fmt.Println("jie xi si yao err: ", err)
		panic(err)
	}
	//fmt.Println(privateKey)

	// 解析pem格式的公钥数据
	blockPub, _ := pem.Decode([]byte(publicKeyStr))
	if blockPub == nil {
		panic("failed to parse PEM block containing the public key")
	}
	//fmt.Println("blockPub: ", blockPub)
	// 解析 DER 编码的公钥,得到一个 *rsa.PublicKey
	publicKey, err := x509.ParsePKCS1PublicKey(blockPub.Bytes)
	//publicKey, err := x509.ParsePKIXPublicKey(blockPub.Bytes)
	if err != nil {
		panic(err)
	}
	//fmt.Println("publicKey: ", publicKey)

	//publicKeyLast := publicKey.(*rsa.PublicKey)

	//根据公钥加密
	myPasswd := []byte("yWdslfXfaFghVG6m")
	fmt.Println("myPasswd: ", myPasswd)
	fmt.Println("myPasswd: ", string(myPasswd))
	// 加密1
	ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, myPasswd)
	if err != nil {
		fmt.Println("err: ", err)
	}
	//fmt.Println("加密后:", ciphertext)
	//加密1

	JiaMiHouStr := base64.StdEncoding.EncodeToString(ciphertext)
	fmt.Println("加密后在base64 : ", JiaMiHouStr)

	// 对base64编码的字符串进行解码
	decoded, err := base64.StdEncoding.DecodeString(JiaMiHouStr)
	if err != nil {
		fmt.Println("Error decoding string:", err.Error())
		return
	}
	//fmt.Println("base64解码后: ", decoded)

	//根据私钥解密
	decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decoded)
	if err != nil {
		panic(err)
	}
	fmt.Println("Origin passwd msg:", string(myPasswd))
	fmt.Println("decrypted message: ", string(decrypted))
}

输出:

myPasswd:  [121 87 100 115 108 102 88 102 97 70 103 104 86 71 54 109] 
myPasswd:  yWdslfXfaFghVG6m
加密后在base64 :  sLfTGmNIDt3/q0gvEQXmDkv2O8m/4IeAiw6B/7UUxxqI2oTh1rRC/TaC5kehKcdlH3FAgj/tyoPFR1VOXiANrycp9DeAir7WaMw7tPOan+eNHERZsjO+hMJ33rDk5G/UW9j7eu8I4L6cxHbgXxa+d6wA7gtGgWy2AkGUIgc+uRnII0H8fRXeQnnA5NercuiICTdOv/Pd0FmhQPo+FZ2alSDOlJa5+S4Xo2kwiAfh7lNucktiJdX
Qd6IHQyLWXbFu3Jp0Eo0bIAeK4pit8JE4Jue/PFipEUvIkarIk4GcXJpvF94+ybCXXDYGLSuHngtmwONuxHSwPbt/ucRyq0H9ew==
Origin passwd msg: yWdslfXfaFghVG6m
decrypted message:  yWdslfXfaFghVG6m

你可能感兴趣的:(Golang,Golang,rsa,base64)