|
@@ -1,8 +1,8 @@
|
|
|
package util
|
|
|
|
|
|
import (
|
|
|
- log "app.yhyue.com/moapp/jylog"
|
|
|
- "context"
|
|
|
+ "crypto/aes"
|
|
|
+ "crypto/cipher"
|
|
|
"crypto/rand"
|
|
|
"crypto/rsa"
|
|
|
"crypto/x509"
|
|
@@ -11,33 +11,24 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/os/gctx"
|
|
|
+ "io"
|
|
|
"io/ioutil"
|
|
|
- "math/big"
|
|
|
+ "log"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- PublicKey *rsa.PublicKey
|
|
|
- PrivateKey *rsa.PrivateKey
|
|
|
- Displacement int64 = 9
|
|
|
- ctx = context.TODO()
|
|
|
+ PublicKey *rsa.PublicKey
|
|
|
+ PrivateKey *rsa.PrivateKey
|
|
|
)
|
|
|
|
|
|
-func init() {
|
|
|
- //initPrivatePublicKey()
|
|
|
-}
|
|
|
-
|
|
|
-func JyAntiEncrypt(in []byte, pos int) (out string, err error) {
|
|
|
- return base64.StdEncoding.EncodeToString(in), err
|
|
|
-}
|
|
|
-
|
|
|
-func initPrivatePublicKey() {
|
|
|
+func InitPrivatePublicKey() {
|
|
|
// 读取私钥文件
|
|
|
privateKeyBytes, err := ioutil.ReadFile("./etc/rsa_private_key.pem")
|
|
|
if err != nil {
|
|
|
- g.Log().Errorf(ctx, "无法读取私钥文件:%v", err)
|
|
|
+ log.Println("无法读取私钥文件:", err)
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
// 解码私钥
|
|
|
privateKeyBlock, _ := pem.Decode(privateKeyBytes)
|
|
|
if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
|
|
@@ -59,14 +50,13 @@ func initPrivatePublicKey() {
|
|
|
return
|
|
|
}
|
|
|
PrivateKey = rsaPrivateKey
|
|
|
-
|
|
|
// 读取公钥文件
|
|
|
publicKeyBytes, err := ioutil.ReadFile("./etc/rsa_public_key.pem")
|
|
|
if err != nil {
|
|
|
log.Println("无法读取公钥文件:", err)
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
+ log.Println("读取公钥文件")
|
|
|
// 解码公钥
|
|
|
publicKeyBlock, _ := pem.Decode(publicKeyBytes)
|
|
|
if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
|
|
@@ -88,87 +78,69 @@ func initPrivatePublicKey() {
|
|
|
return
|
|
|
}
|
|
|
PublicKey = rsaPublicKey
|
|
|
- //Displacement = g.Cfg().MustGet(gctx.New(), "displacement", 9).Int64()
|
|
|
-}
|
|
|
-
|
|
|
-// DisplacementEncryption 位移加密
|
|
|
-func DisplacementEncryption(content string) (string, error) {
|
|
|
- if content == "" {
|
|
|
- return "", errors.New("加密内容为空")
|
|
|
- }
|
|
|
- if PrivateKey == nil {
|
|
|
- return "", errors.New("无效私钥")
|
|
|
- }
|
|
|
+ log.Println("初始化公钥成功", PublicKey)
|
|
|
+ //对称key加密
|
|
|
+ //AesEncDecKey = g.Cfg().MustGet(gctx.New(), "aesEncDecKey").String()
|
|
|
+ //使用非对称加密key
|
|
|
+ //shiftValue := big.NewInt(g.Cfg().MustGet(ctx, "pos").Int64())
|
|
|
+ //PublicKey.E = int(shiftValue.Int64())
|
|
|
|
|
|
- plaintext := []byte(content)
|
|
|
- shiftValue := big.NewInt(Displacement)
|
|
|
- PrivateKey.D = new(big.Int).Mul(PrivateKey.D, shiftValue)
|
|
|
- ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &PrivateKey.PublicKey, plaintext)
|
|
|
- if err != nil {
|
|
|
- fmt.Println("加密失败:", err)
|
|
|
- return "", err
|
|
|
- }
|
|
|
- return string(ciphertext), nil
|
|
|
}
|
|
|
|
|
|
-// DisplacementDecryption 位移解密
|
|
|
-func DisplacementDecryption(content string) (string, error) {
|
|
|
- if content == "" {
|
|
|
- return "", errors.New("解密内容为空")
|
|
|
+func JyAntiEncrypt(plaintext []byte) (out, PKeyStr string, err error) {
|
|
|
+ if plaintext == nil {
|
|
|
+ return
|
|
|
}
|
|
|
- if PrivateKey == nil {
|
|
|
- return "", errors.New("无效私钥")
|
|
|
+ if PublicKey == nil {
|
|
|
+ err = errors.New("无效公钥")
|
|
|
+ return
|
|
|
}
|
|
|
- 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("密文解码失败")
|
|
|
+ aesEncDecKey := g.Cfg().MustGet(gctx.New(), "aesEncDecKey").String()
|
|
|
+ if aesEncDecKey == "" {
|
|
|
+ err = errors.New("密钥获取失败")
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
- // 使用私钥解密数据
|
|
|
- decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
|
|
|
+ out, err = Encrypt(plaintext, []byte(aesEncDecKey))
|
|
|
if err != nil {
|
|
|
- log.Println("解密失败:", err)
|
|
|
- return "", err
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //非对称加密
|
|
|
+ var pKeyByte []byte
|
|
|
+ pKeyByte, err = rsa.EncryptPKCS1v15(rand.Reader, PublicKey, []byte(aesEncDecKey))
|
|
|
+ if err == nil {
|
|
|
+ PKeyStr = base64.StdEncoding.EncodeToString(pKeyByte)
|
|
|
}
|
|
|
- return string(decryptedText), nil
|
|
|
+ log.Println("key加密前===", aesEncDecKey)
|
|
|
+ log.Println("key加密后===", PKeyStr)
|
|
|
+ //log.Println("加密后内容===", out)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
-// Encryption 加密
|
|
|
-func Encryption(content string) (string, error) {
|
|
|
- if content == "" {
|
|
|
- return "", errors.New("加密内容为空")
|
|
|
- }
|
|
|
- plaintext := []byte(content)
|
|
|
- // 使用公钥加密数据
|
|
|
- ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
|
|
|
+// 对称加密
|
|
|
+func Encrypt(plaintext []byte, key []byte) (string, error) {
|
|
|
+ block, err := aes.NewCipher(key)
|
|
|
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("密文解码失败")
|
|
|
- }
|
|
|
+ ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
|
|
|
|
|
- // 使用私钥解密数据
|
|
|
- decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
|
|
|
- if err != nil {
|
|
|
- log.Println("解密失败:", err)
|
|
|
+ iv := ciphertext[:aes.BlockSize]
|
|
|
+ if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
- return string(decryptedText), nil
|
|
|
+
|
|
|
+ stream := cipher.NewCTR(block, iv)
|
|
|
+ stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
|
|
|
+
|
|
|
+ return base64.StdEncoding.EncodeToString(ciphertext), nil
|
|
|
}
|
|
|
+
|
|
|
+/*func JyAntiDoc(plaintext []byte, pos int) (out string, err error) {
|
|
|
+ if plaintext == nil {
|
|
|
+ return "", errors.New("加密内容为空")
|
|
|
+ }
|
|
|
+ //exponent := big.NewInt(int64(pos)) // 位移处理的值
|
|
|
+ //PrivateKey.D = new(big.Int).Mul(PrivateKey.D, exponent)
|
|
|
+ return string(PrivateKey.D.Bytes()), nil
|
|
|
+}*/
|