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
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章