|
@@ -0,0 +1,169 @@
|
|
|
+package util
|
|
|
+
|
|
|
+import (
|
|
|
+ log "app.yhyue.com/moapp/jylog"
|
|
|
+ "crypto/rand"
|
|
|
+ "crypto/rsa"
|
|
|
+ "crypto/x509"
|
|
|
+ "encoding/base64"
|
|
|
+ "encoding/pem"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/os/gctx"
|
|
|
+ "io/ioutil"
|
|
|
+ "math/big"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ PublicKey *rsa.PublicKey
|
|
|
+ PrivateKey *rsa.PrivateKey
|
|
|
+ Displacement int64
|
|
|
+)
|
|
|
+
|
|
|
+func init() {
|
|
|
+ initPrivatePublicKey()
|
|
|
+}
|
|
|
+
|
|
|
+func initPrivatePublicKey() {
|
|
|
+ // 读取私钥文件
|
|
|
+ privateKeyBytes, err := ioutil.ReadFile("./etc/rsa_private_key.pem")
|
|
|
+ if err != nil {
|
|
|
+ log.Println("无法读取私钥文件:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解码私钥
|
|
|
+ privateKeyBlock, _ := pem.Decode(privateKeyBytes)
|
|
|
+ if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
|
|
|
+ log.Println("私钥文件格式错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析私钥
|
|
|
+ privateKeys, err := x509.ParsePKCS8PrivateKey(privateKeyBlock.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("私钥解析失败:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为RSA类型的私钥
|
|
|
+ rsaPrivateKey, ok := privateKeys.(*rsa.PrivateKey)
|
|
|
+ if !ok {
|
|
|
+ fmt.Println("无法转换为RSA类型的私钥")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ PrivateKey = rsaPrivateKey
|
|
|
+
|
|
|
+ // 读取公钥文件
|
|
|
+ publicKeyBytes, err := ioutil.ReadFile("./etc/rsa_public_key.pem")
|
|
|
+ if err != nil {
|
|
|
+ log.Println("无法读取公钥文件:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解码公钥
|
|
|
+ publicKeyBlock, _ := pem.Decode(publicKeyBytes)
|
|
|
+ if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
|
|
|
+ log.Println("公钥文件格式错误", publicKeyBlock.Type)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析公钥
|
|
|
+ publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("公钥解析失败:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为公钥类型
|
|
|
+ rsaPublicKey, ok := publicKeyInterface.(*rsa.PublicKey)
|
|
|
+ if !ok {
|
|
|
+ log.Println("无法转换为公钥类型")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ PublicKey = rsaPublicKey
|
|
|
+ Displacement = g.Cfg().MustGet(gctx.New(), "displacement").Int64()
|
|
|
+}
|
|
|
+
|
|
|
+// Encryption wei加密
|
|
|
+func DisplacementEncryption(content string) (string, error) {
|
|
|
+ if content == "" {
|
|
|
+ return "", errors.New("加密内容为空")
|
|
|
+ }
|
|
|
+ plaintext := []byte(content)
|
|
|
+ if Displacement != 0 { // 位移处理的值
|
|
|
+ shiftValue := big.NewInt(Displacement)
|
|
|
+ PublicKey.E = int(shiftValue.Int64())
|
|
|
+ }
|
|
|
+ // 使用公钥加密数据
|
|
|
+ ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("加密失败:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(ciphertext), nil
|
|
|
+}
|
|
|
+
|
|
|
+func DisplacementDecryption(content string) (string, error) {
|
|
|
+ if content == "" {
|
|
|
+ return "", errors.New("解密内容为空")
|
|
|
+ }
|
|
|
+ if PrivateKey == nil {
|
|
|
+ return "", errors.New("无效私钥")
|
|
|
+ }
|
|
|
+ if Displacement != 0 {
|
|
|
+ exponent := big.NewInt(Displacement) // 位移处理的值
|
|
|
+ PrivateKey.D = new(big.Int).Mul(PrivateKey.D, exponent)
|
|
|
+ }
|
|
|
+ ciphertext, err := base64.StdEncoding.DecodeString(content)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("密文解码失败:", err)
|
|
|
+ return "", errors.New("密文解码失败")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用私钥解密数据
|
|
|
+ decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("解密失败:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(decryptedText), nil
|
|
|
+}
|
|
|
+
|
|
|
+// Encryption 加密
|
|
|
+func Encryption(content string) (string, error) {
|
|
|
+ if content == "" {
|
|
|
+ return "", errors.New("加密内容为空")
|
|
|
+ }
|
|
|
+ plaintext := []byte(content)
|
|
|
+ // 使用公钥加密数据
|
|
|
+ ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("加密失败:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(ciphertext), nil
|
|
|
+}
|
|
|
+
|
|
|
+func Decryption(content string) (string, error) {
|
|
|
+ if content == "" {
|
|
|
+ return "", errors.New("解密内容为空")
|
|
|
+ }
|
|
|
+ if PrivateKey == nil {
|
|
|
+ return "", errors.New("无效私钥")
|
|
|
+ }
|
|
|
+ ciphertext, err := base64.StdEncoding.DecodeString(content)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("密文解码失败:", err)
|
|
|
+ return "", errors.New("密文解码失败")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用私钥解密数据
|
|
|
+ decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("解密失败:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(decryptedText), nil
|
|
|
+}
|