package utils
import (
"github.com/wenzhenxi/gorsa"
"strings"
)
// 私钥加密
func RsaPemPriEncrypt(src string, privateKey string) (string, error) {
var srcArr []string
for true {
if len(src) > 100 {
srcArr = append(srcArr, src[:100])
src = src[100:]
} else {
srcArr = append(srcArr, src)
break
}
}
var encArr []string
for _, seg := range srcArr {
enc, err := gorsa.PriKeyEncrypt(seg, privateKey)
if err != nil {
return "", err
}
encArr = append(encArr, enc)
}
return strings.Join(encArr, "\n"), nil
}
// 公钥解密
func RsaPemPubDecrypt(enc string, publicKey string) (string, error) {
var srcArr []string
encArr := strings.Split(enc, "\n")
for _, seq := range encArr {
src, err := gorsa.PublicDecrypt(seq, publicKey)
if err != nil {
return "", err
}
srcArr = append(srcArr, src)
}
return strings.Join(srcArr, ""), nil
}
// 公钥加密
func RsaPemPubEncrypt(src string, publicKey string) (string, error) {
var srcArr []string
for true {
if len(src) > 100 {
srcArr = append(srcArr, src[:100])
src = src[100:]
} else {
srcArr = append(srcArr, src)
break
}
}
var encArr []string
for _, seg := range srcArr {
enc, err := gorsa.PublicEncrypt(seg, publicKey)
if err != nil {
return "", err
}
encArr = append(encArr, enc)
}
return strings.Join(encArr, "\n"), nil
}
// 私钥解密
func RsaPemPriDecrypt(enc string, privateKey string) (string, error) {
var srcArr []string
encArr := strings.Split(enc, "\n")
for _, seq := range encArr {
src, err := gorsa.PriKeyDecrypt(seq, privateKey)
if err != nil {
return "", err
}
srcArr = append(srcArr, src)
}
return strings.Join(srcArr, ""), nil
}
Go License 公钥 私钥 加密 解密
最新推荐文章于 2024-07-06 22:12:59 发布