1 Star 0 Fork 0

wdxtub/gocdt

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rsa.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
dawang 提交于 2021-08-30 12:16 +08:00 . 修复引用问题
package encrypt
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"gitee.com/wdxtub/gocdt/file"
)
// RSAEncrypt RSA 加密
func RSAEncrypt(plainText []byte, pubKeyPath string) ([]byte, error) {
//打开文件
buf, err := file.ReadFileContent(pubKeyPath)
if err != nil {
return nil, err
}
//pem解码
block, _ := pem.Decode(buf)
//x509解码
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
//类型断言
publicKey := publicKeyInterface.(*rsa.PublicKey)
//对明文进行加密
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
if err != nil {
return nil, err
}
//返回密文
return cipherText, nil
}
// RSADecrypt RSA 解密
func RSADecrypt(cipherText []byte, privKeyPath string) ([]byte, error) {
//打开文件
buf, err := file.ReadFileContent(privKeyPath)
if err != nil {
return nil, err
}
//pem解码
block, _ := pem.Decode(buf)
//X509解码
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
//对密文进行解密
plainText, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText)
if err != nil {
return nil, err
}
//返回明文
return plainText, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wdxtub/gocdt.git
git@gitee.com:wdxtub/gocdt.git
wdxtub
gocdt
gocdt
v0.1.3

搜索帮助