GO实现非对称加密--RSA加密解密算法

package main

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

func main()  {
	str := "&^%$#@___Oneck___@#$%^&"
	fmt.Println("初始字符串",str)
	cipherBytes,err := RSAEncrypt([]byte(str),"./files/public.pem")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("----------------")
	originalBytes,err := RSADecrypt(cipherBytes,"./files/private.pem")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("解密后的字符串",string((originalBytes)))
	fmt.Println("----------------")
	fmt.Println("----------------")
	fmt.Println("----------------")
	str = "&^%$#@___Peter___@#$%^&"
	fmt.Println("初始字符串",str)
	cipherText,err := RSAEncryptString(str,"./files/public.pem")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("加密后",cipherText)
	fmt.Println("----------------")
	originalText,err := RSADecryptString(cipherText,"./files/private.pem")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("解密后的字符串",originalText)
}

func RSAEncrypt(originalBytes []byte,filename string) ([]byte,error) {
	//1.读取公钥文件,解析公钥对象
	publicKey,err := ReadParsePublicKey(filename)
	if err != nil {
		return nil,err
	}
	//2.RSA加密,参数是随机数、公钥对象、需要加密的字节
	return  rsa.EncryptPKCS1v15(rand.Reader,publicKey,originalBytes)
}

func RSADecrypt(cipherBytes []byte,filename string) ([]byte,error) {
	//1.读取私钥文件,解析私钥对象
	privateKey,err := ReadParsePrivateKey(filename)
	if err != nil {
		return nil,err
	}
	//2.RSA解密,参数是随机数、私钥对象、需要解密的字节
	return rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherBytes)
}
//读取公钥文件,解析出公钥对象
func ReadParsePublicKey(filename string) (*rsa.PublicKey,error) {
	//--1.读取公钥文件,获取公钥字节
	publicKeyBytes,err := ioutil.ReadFile(filename)
	if err != nil {
		return nil,err
	}
	//--2.解码公钥字节,生成加密对象
	block,_ := pem.Decode(publicKeyBytes)
	if block == nil {
		return  nil,errors.New("公钥信息错误")
	}
	//--3.解析DER编码的公钥,生成公钥接口
	publicKeyInterface,err :=x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return nil,err
	}
	//--4.公钥接口转型成公钥对象
	publicKey := publicKeyInterface.(*rsa.PublicKey)
	return publicKey,nil
}
//读取私钥文件,解析出私钥对象
func ReadParsePrivateKey(filename string) (*rsa.PrivateKey,error) {
	//--1.读取私钥文件,获取私钥字节
	privateKeyBytes,_ := ioutil.ReadFile(filename)
	//--2.对私钥文件进行编码,生成加密对象
	block,_ := pem.Decode(privateKeyBytes)
	if block == nil {
		return nil,errors.New("私钥信息错误")
	}
	//3.解析DER编码的私钥,生成私钥对象
	privateKey,err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return nil,err
	}
	return privateKey,err
}

//RSA加密字符串,返回base64处理的字符串
func RSAEncryptString(originalText,filename string) (string,error) {
	cipherBytes,err := RSAEncrypt([]byte(originalText),filename)
	if err != nil {
		return "",err
	}
	return  base64.StdEncoding.EncodeToString(cipherBytes),nil
}

//RSA解密经过base64处理的加密字符串,返回加密前的明文
func RSADecryptString(cipherText,filename string) (string,error) {
	cipherBytes,_ := base64.StdEncoding.DecodeString(cipherText)
	originalBytes,err := RSADecrypt(cipherBytes,filename)
	if err != nil {
		return "",err
	}
	return string(originalBytes),nil
}

在这里插入图片描述

发布了19 篇原创文章 · 获赞 85 · 访问量 1319

猜你喜欢

转载自blog.csdn.net/qq_45828877/article/details/103951958