feat: 加密工具AES-CBC,配合前端CryptoJS加解密
This commit is contained in:
@@ -10,12 +10,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StringEncryptByAES 字符串AES加密
|
// AESEncryptBase64 AES加密转Base64字符串
|
||||||
func StringEncryptByAES(text string) (string, error) {
|
func AESEncryptBase64(text, key string) (string, error) {
|
||||||
if len(text) == 0 {
|
if len(text) == 0 {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
xpass, err := aesEncryptWithSalt([]byte(text))
|
xpass, err := AESEncrypt([]byte(text), []byte(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -23,8 +23,8 @@ func StringEncryptByAES(text string) (string, error) {
|
|||||||
return pass64, nil
|
return pass64, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringDecryptByAES 字符串AES解密
|
// AESDecryptBase64 AES解密解Base64字符串
|
||||||
func StringDecryptByAES(text string) (string, error) {
|
func AESDecryptBase64(text, key string) (string, error) {
|
||||||
if len(text) == 0 {
|
if len(text) == 0 {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
@@ -32,21 +32,16 @@ func StringDecryptByAES(text string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
tpass, err := AESDecrypt(bytesPass, []byte(key))
|
||||||
tpass, err := aesDecryptWithSalt(bytesPass)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return string(tpass), nil
|
return string(tpass), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// aesKey 字符串AES加解密密钥
|
// AESEncrypt AES加密
|
||||||
const aesKey = "AGT66VfY4SMaiT97a7df0aef1704d5c5"
|
func AESEncrypt(plaintext, aeskey []byte) ([]byte, error) {
|
||||||
|
block, err := aes.NewCipher(aeskey)
|
||||||
// const aesKey = "AGT66VfY4SMaiT97"
|
|
||||||
// aesEncryptWithSalt AES加密
|
|
||||||
func aesEncryptWithSalt(plaintext []byte) ([]byte, error) {
|
|
||||||
block, err := aes.NewCipher([]byte(aesKey))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -68,8 +63,8 @@ func aesEncryptWithSalt(plaintext []byte) ([]byte, error) {
|
|||||||
return ciphertext, nil
|
return ciphertext, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// aesDecryptWithSalt AES解密
|
// AESDecrypt AES解密
|
||||||
func aesDecryptWithSalt(ciphertext []byte) ([]byte, error) {
|
func AESDecrypt(ciphertext, aeskey []byte) ([]byte, error) {
|
||||||
blockSize := aes.BlockSize
|
blockSize := aes.BlockSize
|
||||||
if len(ciphertext) < blockSize {
|
if len(ciphertext) < blockSize {
|
||||||
return nil, fmt.Errorf("ciphertext too short")
|
return nil, fmt.Errorf("ciphertext too short")
|
||||||
@@ -77,12 +72,14 @@ func aesDecryptWithSalt(ciphertext []byte) ([]byte, error) {
|
|||||||
|
|
||||||
iv := ciphertext[:blockSize]
|
iv := ciphertext[:blockSize]
|
||||||
ciphertext = ciphertext[blockSize:]
|
ciphertext = ciphertext[blockSize:]
|
||||||
|
block, err := aes.NewCipher([]byte(aeskey))
|
||||||
|
|
||||||
block, err := aes.NewCipher([]byte(aesKey))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(ciphertext) == 0 {
|
||||||
|
return nil, fmt.Errorf("ciphertext is invalid")
|
||||||
|
}
|
||||||
if len(ciphertext)%blockSize != 0 {
|
if len(ciphertext)%blockSize != 0 {
|
||||||
return nil, fmt.Errorf("ciphertext is not a multiple of the block size")
|
return nil, fmt.Errorf("ciphertext is not a multiple of the block size")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"be.ems/src/framework/config"
|
||||||
"be.ems/src/framework/constants/common"
|
"be.ems/src/framework/constants/common"
|
||||||
"be.ems/src/framework/logger"
|
"be.ems/src/framework/logger"
|
||||||
"be.ems/src/framework/utils/cmd"
|
"be.ems/src/framework/utils/cmd"
|
||||||
@@ -68,7 +69,8 @@ func codeFileRead() (map[string]any, error) {
|
|||||||
}
|
}
|
||||||
content := string(bytes)
|
content := string(bytes)
|
||||||
// 解密
|
// 解密
|
||||||
contentDe, err := crypto.StringDecryptByAES(content)
|
hostKey := config.Get("aes.hostKey").(string)
|
||||||
|
contentDe, err := crypto.AESDecryptBase64(content, hostKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("CodeFileRead decrypt: %v", err.Error())
|
logger.Errorf("CodeFileRead decrypt: %v", err.Error())
|
||||||
return mapData, fmt.Errorf("decrypt fail")
|
return mapData, fmt.Errorf("decrypt fail")
|
||||||
@@ -86,7 +88,8 @@ func codeFileRead() (map[string]any, error) {
|
|||||||
func codeFileWrite(data map[string]any) error {
|
func codeFileWrite(data map[string]any) error {
|
||||||
jsonByte, _ := json.Marshal(data)
|
jsonByte, _ := json.Marshal(data)
|
||||||
// 加密
|
// 加密
|
||||||
contentEn, err := crypto.StringEncryptByAES(string(jsonByte))
|
hostKey := config.Get("aes.hostKey").(string)
|
||||||
|
contentEn, err := crypto.AESEncryptBase64(string(jsonByte), hostKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("insert encrypt: %v", err.Error())
|
logger.Errorf("insert encrypt: %v", err.Error())
|
||||||
return fmt.Errorf("encrypt fail")
|
return fmt.Errorf("encrypt fail")
|
||||||
|
|||||||
Reference in New Issue
Block a user