Golang 实现RSA加密解密
生成RSA证书:
openssl方式生成
- 生成私钥
openssl genrsa -out rsa_private_key.pem 1024
- 生成公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
Go代码方式生成
package main import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/pem" "os" ) //生成RSA私钥和公钥,保存到文件中 // bits 证书大小 func GenerateRSAKey(bits int) { //GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥 //Reader是一个全局、共享的密码用强随机数生成器 privateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { panic(err) } //保存私钥 //通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串 X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) //使用pem格式对x509输出的内容进行编码 //创建文件保存私钥 privateFile, err := os.Create("private.pem") if err != nil { panic(err) } defer privateFile.Close() //构建一个pem.Block结构体对象 privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey} //将数据保存到文件 pem.Encode(privateFile, &privateBlock) //保存公钥 //获取公钥的数据 publicKey := privateKey.PublicKey //X509对公钥编码 X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey) if err != nil { panic(err) } //pem格式编码 //创建用于保存公钥的文件 publicFile, err := os.Create("public.pem") if err != nil { panic(err) } defer publicFile.Close() //创建一个pem.Block结构体对象 publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey} //保存到文件 pem.Encode(publicFile, &publicBlock) } func main() { //生成密钥对,保存到文件 GenerateRSAKey(2048) }
RSA数据加/解密
package main import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/pem" "fmt" "os" ) //RSA加密 // plainText 要加密的数据 // path 公钥匙文件地址 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) //pem解码 block, _ := pem.Decode(buf) //x509解码 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解密 // cipherText 需要解密的byte数据 // path 私钥文件路径 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) //pem解码 block, _ := pem.Decode(buf) //X509解码 privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err!=nil{ panic(err) } //对密文进行解密 plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey,cipherText) //返回明文 return plainText } func main() { //加密 data := []byte("hello world") encrypt := RSA_Encrypt(data, "public.pem") fmt.Println(string(encrypt)) // 解密 decrypt := RSA_Decrypt(encrypt, "private.pem") fmt.Println(string(decrypt)) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构