Skip to content

RSA加解密 in Golang

go
package main

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

func GenerateRSAKey(bits int){
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err!=nil{
		panic(err)
	}
	X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
	privateFile, err := os.Create("private.pem")
	if err!=nil{
		panic(err)
	}
	defer privateFile.Close()
	privateBlock:= pem.Block{Type: "RSA Private Key",Bytes:X509PrivateKey}
	pem.Encode(privateFile,&privateBlock)
	publicKey:=privateKey.PublicKey
	X509PublicKey,err:=x509.MarshalPKIXPublicKey(&publicKey)
	if err!=nil{
		panic(err)
	}
	publicFile, err := os.Create("public.pem")
	if err!=nil{
		panic(err)
	}
	defer publicFile.Close()
	publicBlock:= pem.Block{Type: "RSA Public Key",Bytes:X509PublicKey}
	pem.Encode(publicFile,&publicBlock)
}

//RSA加密
func RSA_Encrypt(plainText []byte,path string)[]byte{
	file,err:=os.Open(path)
	if err!=nil{
		panic(err)
	}
	defer file.Close()
	info, _ := file.Stat()
	buf:=make([]byte,info.Size())
	file.Read(buf)
	block, _ := pem.Decode(buf)
	publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err!=nil{
		panic(err)
	}
	publicKey:=publicKeyInterface.(*rsa.PublicKey)
	cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
	if err!=nil{
		panic(err)
	}
	return cipherText
}

//RSA解密
func RSA_Decrypt(cipherText []byte,path string) []byte{
	file,err:=os.Open(path)
	if err!=nil{
		panic(err)
	}
	defer file.Close()
	info, _ := file.Stat()
	buf:=make([]byte,info.Size())
	file.Read(buf)
	block, _ := pem.Decode(buf)
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err!=nil{
		panic(err)
	}
	plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherText)
	return plainText
}

func main(){
	GenerateRSAKey(2048)
	message:=[]byte("hello world")
	cipherText:=RSA_Encrypt(message,"public.pem")
	fmt.Println("加密后为:",string(cipherText))
	plainText := RSA_Decrypt(cipherText, "private.pem")
	fmt.Println("解密后为:",string(plainText))
}

Released under the MIT License.