This commit is contained in:
kony
2025-09-22 17:05:21 +08:00
parent 314648ebde
commit ec603bf2b8
3 changed files with 5 additions and 78 deletions

View File

@@ -1,73 +0,0 @@
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func getKey(key string) []byte {
if len(key) > 24 {
return []byte(key[:24])
}
// 填充key
for len(key) < 24 {
key = key + key[0:1]
}
return []byte(key)
}
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
padding := blocksize - len(ciphertext)%blocksize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func Encrypt(origData []byte, key string) string {
k := getKey(key)
block, _ := aes.NewCipher(k)
blockSize := block.BlockSize()
origData = PKCS7Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return base64.StdEncoding.EncodeToString(cryted)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func Decrypt(cryted []byte, key string) []byte {
crytedByte, _ := base64.StdEncoding.DecodeString(string(cryted))
k := getKey(key)
block, _ := aes.NewCipher(k)
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
orig := make([]byte, len(crytedByte))
blockMode.CryptBlocks(orig, crytedByte)
return PKCS7UnPadding(orig)
}
func AesTest() {
text := []byte("hello world")
key := "1234567812345677777777777781238"
temp1 := Encrypt(text, key)
fmt.Printf("加密后: %s\n", temp1)
temp2 := Decrypt([]byte(temp1), key)
fmt.Printf("解密后: %s\n", temp2)
key = "123456781238"
temp1 = Encrypt(text, key)
fmt.Printf("加密后: %s\n", temp1)
temp2 = Decrypt([]byte(temp1), key)
fmt.Printf("解密后: %s\n", temp2)
}

View File

@@ -4,7 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"go2"
"goodlink/aes"
go2aes "go2/aes"
"io"
"net/http"
"time"
@@ -57,7 +57,7 @@ func Init() error {
}
}
if err = json.Unmarshal(aes.Decrypt(res, "goodlink"), &configInfo); err != nil {
if err = json.Unmarshal(go2aes.Decrypt7(res, "goodlink"), &configInfo); err != nil {
return err
}

View File

@@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"goodlink/aes"
go2aes "go2/aes"
"goodlink/config"
"goodlink/stun2"
"goodlink/utils"
@@ -115,7 +115,7 @@ func RedisSet(time_out time.Duration, redisJson *RedisJsonType) error {
return errors.New("Redis为初始化")
}
if jsonByte, err := json.Marshal(*redisJson); err == nil {
m_redis_db.Set(m_md5_tun_key, aes.Encrypt(jsonByte, m_tun_key), time_out)
m_redis_db.Set(m_md5_tun_key, go2aes.Encrypt7(jsonByte, m_tun_key), time_out)
}
return nil
}
@@ -130,7 +130,7 @@ func RedisGet(redisJson *RedisJsonType) error {
return fmt.Errorf("获取信令数据失败: %v", err)
}
if err = json.Unmarshal(aes.Decrypt(aes_res, m_tun_key), redisJson); err != nil {
if err = json.Unmarshal(go2aes.Decrypt7(aes_res, m_tun_key), redisJson); err != nil {
return fmt.Errorf("解析信令数据失败: %v", err)
}