您当前的位置: 首页 > 

慕晨sekurlsa

暂无认证

  • 2浏览

    0关注

    82博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Go实现RSA加解密

慕晨sekurlsa 发布时间:2022-08-25 23:58:45 ,浏览量:2

Go实现RSA加解密

主要用到的库有crypto/rand、crypto/rsa、crypto/x509、encoding/pem。

分为三个文件:

  • RSAGeneratekey.go //负责生成pem格式的私钥和公钥文件。
  • RSAEncrypt.go //负责对明文进行加密,并将密文输出到文件。
  • RSADecrypt.go //负责对密文进行解密,并将明文输出到文件。
RSAGeneratekey.go
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"log"
	"os"
	"path/filepath"
	"runtime"
)

func RSAGenerate(bits int)  {
	//get current path
	_, currentpath, _, _ := runtime.Caller(0)
	currentpath = filepath.Dir(currentpath)

	//----------------------------------------------private key

	// GenerateKey generates an RSA keypair of the given bit size using the
	// random source random (for example, crypto/rand.Reader).
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		log.Fatal(err)
	}

	//serialize privatekey to ASN.1 der by x509.MarshalPKCS1PrivateKey
	x509privatekey := x509.MarshalPKCS1PrivateKey(privateKey)

	//encode x509 to pem and save to file
	//1. create privatefile
	privatekeyfile, err := os.Create(currentpath + "/privatekey.pem")
	if err != nil {
		log.Fatal(err)
	}
	defer privatekeyfile.Close()
	//2. new a pem block struct object
	privatekeyblock := pem.Block{
		Type:    "RSA Private Key",
		Headers: nil,
		Bytes:   x509privatekey,
	}
	//3. save to file
	pem.Encode(privatekeyfile, &privatekeyblock)


	//----------------------------------------------public key

	//get public key
	publickey := privateKey.PublicKey
	//serialize publickey to ASN.1 der by x509.MarshalPKCS1PublicKey
	x509publickey, _ := x509.MarshalPKIXPublicKey(&publickey)

	//encode x509 to pem and save to file
	//1. create publickeyfile
	publickeyfile, err := os.Create(currentpath + "/publickey.pem")
	if err != nil {
		log.Fatal(err)
	}
	defer publickeyfile.Close()

	//2. new a pem block struct object
	publickeyblock := pem.Block{
		Type:    "RSA Public Key",
		Headers: nil,
		Bytes:   x509publickey,
	}

	//3. save to file
	pem.Encode(publickeyfile, &publickeyblock)
}

func main() {
	RSAGenerate(1024)
}

RSAEncrypt.go
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"runtime"
)

func RsaEncrypt(plainText []byte, publickeypath string) []byte{
	//open publickeyfile
	publickeyfile, err := os.Open(publickeypath)
	if err != nil {
		log.Fatal(err)
	}
	defer publickeyfile.Close()

	//get publickeyfile info
	publickeyfileInfo, _ := publickeyfile.Stat()

	//read publickeyfile content
	//1. make size
	buf := make([]byte, publickeyfileInfo.Size())
	//2. read file to buf
	publickeyfile.Read(buf)
	//3. decode pem
	publickeyDecodeBlock, _ := pem.Decode(buf)
	//4. x509 decode
	publicKeyInterface, err := x509.ParsePKIXPublicKey(publickeyDecodeBlock.Bytes)
	if err != nil {
		log.Fatal(err)
	}
	//assert
	publicKey := publicKeyInterface.(*rsa.PublicKey)

	//encrypt plainText
	cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
	if err != nil {
		log.Fatal(err)
	}


	return cipherText
}

func main() {
	//get current path
	_, currentpath, _, _ := runtime.Caller(0)
	currentpath = filepath.Dir(currentpath)
	fmt.Println(currentpath)
	//encode plainText
	plainText := []byte("yes this is me,do you miss me?")
	publickeypath := currentpath + "/publickey.pem"
	cipher := RsaEncrypt(plainText, publickeypath)
	fmt.Println(cipher)
	fmt.Println(string(cipher))

	//save cipherText to file
	cipherfile, _ := os.Create(currentpath + "/CipherText")
	defer cipherfile.Close()
	cipherfile.Write(cipher)

}

RSADecrypt.go
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"runtime"
)

func RsaDecrypt(cipherText []byte, privatekeypath string) []byte {

	//open privatekeyfile
	privatekeyfile,err:=os.Open(privatekeypath)
	if err!=nil{
		log.Fatal(err)
	}
	defer privatekeyfile.Close()
	//get privatekeyfile content
	privatekeyinfo, _ := privatekeyfile.Stat()
	buf:=make([]byte,privatekeyinfo.Size())
	privatekeyfile.Read(buf)
	//pem decode
	privatekeyblock, _ := pem.Decode(buf)
	//X509 decode
	privateKey, err := x509.ParsePKCS1PrivateKey(privatekeyblock.Bytes)
	if err!=nil{
		log.Fatal(err)
	}
	//decrypt the cipher
	plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherText)

	return plainText
}

func main() {
	//get currentpath
	_, currentpath, _, _ := runtime.Caller(0)
	currentpath = filepath.Dir(currentpath)

	//read cipher to buf
	cipherpath := currentpath + "/CipherText"
	cipherfile, err := os.Open(cipherpath)
	if err != nil {
		log.Fatal(err)
	}
	defer cipherfile.Close()
	cipherfileInfo , _:= cipherfile.Stat()
	buf := make([]byte, cipherfileInfo.Size())
	cipherfile.Read(buf)

	//privatekeypath
	privatekeypath := currentpath + "/privatekey.pem"
	//decrypt cipherText
	plainText := RsaDecrypt(buf, privatekeypath)
	fmt.Println(plainText)
	fmt.Println(string(plainText))

	PlainText, err := os.Create(currentpath + "/PlainText")
	if err != nil {
		log.Fatal(err)
	}
	defer PlainText.Close()
	PlainText.Write(plainText)

}

关注
打赏
1663680270
查看更多评论
立即登录/注册

微信扫码登录

0.0357s