1 Star 0 Fork 0

tech4good/common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rsa.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
v_chaoni 提交于 2022-04-02 14:57 +08:00 . --other=merge
package utils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
)
//密钥为pem格式
//公钥加密,输入string格式的明文,输出hex string格式的密文
func RsaEncrypt(msg string, key string) (string, error) {
//解析公钥
block, _ := pem.Decode([]byte(key))
if block == nil {
return "", errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return "", err
}
pub := pubInterface.(*rsa.PublicKey)
//加密
cyphertext, err := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(msg))
if err != nil {
return "", err
}
//返回hex string格式的密文
return hex.EncodeToString(cyphertext), nil
}
//私钥解密,输入hex string格式的密文,输出string格式的明文
func RsaDecrypt(cypher string, key string) (string, error) {
if cypher == "" {
return "", nil
}
//hex string转[]byte
msg, err := hex.DecodeString(cypher)
if err != nil {
return "", err
}
//解析私钥
block, _ := pem.Decode([]byte(key))
if block == nil {
return "", errors.New("empty key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", err
}
//解密
plaintext, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, msg)
if err != nil {
return "", err
}
return string(plaintext), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tech4good/common.git
git@gitee.com:tech4good/common.git
tech4good
common
common
v1.0.2

搜索帮助