golang rsa 用法举例

发布时间:2024-07-02 22:02:59

首先,我们先了解一下Golang中的RSA加密算法。RSA是一种非对称加密算法,可以用于数据的加密和解密。它利用了两个大素数的乘积难以分解这个数的特点,保证了加密的安全性。在Golang中,我们可以使用crypto/rsa包来实现RSA加密。

生成密钥对

Golang中生成RSA密钥对的函数是rsa.GenerateKey()。该函数接受一个随机数生成器和密钥长度作为参数,并返回一个rsa.PrivateKey类型的私钥和一个rsa.PublicKey类型的公钥。

下面是一个生成RSA密钥对的示例代码:

package main

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

func main() {
	// 生成2048位的RSA密钥对
	privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 将私钥保存到磁盘文件
	privateFile, err := os.Create("private.pem")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer privateFile.Close()
	
	privateBlock := &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
	}
	err = pem.Encode(privateFile, privateBlock)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 将公钥保存到磁盘文件
	publicKey := &privateKey.PublicKey
	publicFile, err := os.Create("public.pem")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer publicFile.Close()
	
	publicBlock := &pem.Block{
		Type:  "RSA PUBLIC KEY",
		Bytes: x509.MarshalPKCS1PublicKey(publicKey),
	}
	err = pem.Encode(publicFile, publicBlock)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	fmt.Println("RSA密钥对生成成功!")
}

在上面的代码中,我们先使用rsa.GenerateKey()函数生成一个2048位的RSA密钥对。然后将私钥和公钥保存到磁盘文件private.pem和public.pem中。注意,在保存私钥和公钥时,我们使用了pem.Encode()函数将其转换为PEM格式。

加密数据

Golang中RSA加密数据的函数是rsa.EncryptPKCS1v15()。该函数接受公钥和待加密的数据作为参数,并返回加密后的数据。

下面是一个使用RSA加密数据的示例代码:

package main

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

func main() {
	// 读取公钥文件
	publicKeyFile, err := ioutil.ReadFile("public.pem")
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 解码公钥
	publicBlock, _ := pem.Decode(publicKeyFile)
	publicKey, err := x509.ParsePKCS1PublicKey(publicBlock.Bytes)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 加密数据
	message := []byte("Hello, RSA!")
	ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, message)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	fmt.Printf("加密后的数据:%x\n", ciphertext)
}

在上面的代码中,我们先使用ioutil.ReadFile()函数读取公钥文件public.pem。然后使用pem.Decode()函数解码公钥,并使用x509.ParsePKCS1PublicKey()函数将其转换为rsa.PublicKey类型。最后,使用rsa.EncryptPKCS1v15()函数对待加密的数据进行加密,加密后的结果存储在变量ciphertext中。

解密数据

Golang中RSA解密数据的函数是rsa.DecryptPKCS1v15()。该函数接受私钥和待解密的数据作为参数,并返回解密后的数据。

下面是一个使用RSA解密数据的示例代码:

package main

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

func main() {
	// 读取私钥文件
	privateKeyFile, err := ioutil.ReadFile("private.pem")
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 解码私钥
	privateBlock, _ := pem.Decode(privateKeyFile)
	privateKey, err := x509.ParsePKCS1PrivateKey(privateBlock.Bytes)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	// 解密数据
	ciphertext := []byte{0x7e, 0x9c, 0xc6, 0x8a, 0xe3, 0xa7, 0x44, 0x9d, 0x5f, 0xf1, 0x29, 0xf6, 0xe5, 0x35, 0x78, 0x3c, 0x0b, 0x9a, 0x93, 0x34, 0x0e, 0xff, 0xee, 0xa3, 0x11, 0x2f, 0x3b, 0x66, 0x42, 0xdd, 0x2b, 0xd4, 0x78, 0x03, 0x67, 0x81, 0x43, 0x62, 0xda, 0x97, 0x6a, 0x3d, 0xbe, 0x94, 0x0d, 0x55, 0x6a, 0x44, 0xd9, 0xdb, 0x51, 0x82, 0xb7, 0x38, 0x2e, 0x15, 0x09, 0xae, 0xb8, 0xbb, 0xba, 0xb4, 0x8b, 0xaf, 0x9f}
	message, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, ciphertext)
	if err != nil {
		fmt.Println(err)
		return
	}
	
	fmt.Printf("解密后的数据:%s\n", message)
}

在上面的代码中,我们先使用ioutil.ReadFile()函数读取私钥文件private.pem。然后使用pem.Decode()函数解码私钥,并使用x509.ParsePKCS1PrivateKey()函数将其转换为rsa.PrivateKey类型。最后,使用rsa.DecryptPKCS1v15()函数对待解密的数据进行解密,解密后的结果存储在变量message中。

总结

本文介绍了Golang中RSA加密算法的用法,包括生成密钥对、加密数据和解密数据三部分内容。通过以上示例代码,我们可以了解到如何在Golang中使用crypto/rsa包进行RSA加密和解密操作。希望本文对你理解和使用Golang中的RSA加密算法有所帮助。

相关推荐