153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
package test
|
||
|
||
import (
|
||
"crypto/rand"
|
||
"crypto/rsa"
|
||
"crypto/x509"
|
||
"encoding/base64"
|
||
"encoding/pem"
|
||
"fmt"
|
||
"go-admin/app/admin/models/bus_models"
|
||
"go-admin/tools/crypto"
|
||
"io"
|
||
"os"
|
||
|
||
"testing"
|
||
//"text/template"
|
||
)
|
||
|
||
func TestGoModelTemplate(t *testing.T) {
|
||
//t1, err := template.ParseFiles("model.go.template")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//table := tools.SysTables{}
|
||
//table.TBName = "sys_tables"
|
||
//tab, err := table.Get()
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//file, err := os.Create("models/" + table.PackageName + ".go")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//defer file.Close()
|
||
//
|
||
//_ = t1.Execute(file, tab)
|
||
//t.Log("")
|
||
|
||
// 加密
|
||
data := "{\"import_serial_number\":\"8766f066-2880-48f0-a295-121f981a779b\",\"phone_list\":[\"15019236666\"],\"sms_content\":\"123\",\"send_time\":\"2025-04-17T15:30:00+08:00\"}"
|
||
encryptedData, err := crypto.AESEncryptJson(bus_models.AESKey, data)
|
||
fmt.Println("加密后的文本:", encryptedData)
|
||
|
||
// 解密
|
||
decryptedText, err := crypto.AESDecryptJson(bus_models.AESKey, encryptedData)
|
||
if err != nil {
|
||
fmt.Println("解密失败:", err)
|
||
return
|
||
}
|
||
fmt.Println("解密后的文本:", decryptedText)
|
||
|
||
//// 解密
|
||
//encryptedText := "C0ItV2oeoDcr+T0ySc0ol5ux2uDiE3KAB6I0oqX8P6lb4FPaY4eQhB/BlEvQM9YnSeGmV/xJVtpai2tyHsg25K/zuaep24FPjDY/KZ4T0iwmTRpDREI34FjLT2QY/NHh7DFdgVfloWkW3KkuAcEoQuEWUZOajHObXyQKcdiYeBgONM8tyuCAU+fd9oC2lUImuOU8x50xsccSkouqliit8LhSNtlnnlRfUhMuPJ36sIa4+3IBgCtHrXaFv1X9L3a15xQXtq8xhmTLSNo3KUHvo1YsjvWbyjgERlmBby6avq3tmCehXhqUMAk0QUMZQBaWwRbDCpCEk6F+ZF/eUz+3eMCeLPj2dK7L71iIIQ49tjgiSW9QTALHXOt1vCExKcDHXC8lCJ3wbcI1jj4hRylu4M4W6JJxb1an2n6JPViiwes6uTFDjOBWxEk53G64oo2UdCdlHvFfPgVMUqrHJ2cXH4CwXomv3hMXRA+KCDWUaCCQo9jQnH5Q11VUBn+7Ji08MIDNcBGwLP8E54YQSslLtOxLsWLIZWMV/MYB/QmXz+yeLprt+qL9DZNBvrk/pDjBXKL+haO3034qxsbx8vOiPe4Utcmh4mZqCZJqyDzQdTo="
|
||
//
|
||
//decryptedText, err := crypto.AESDecryptJson(bus_models.AESKey, encryptedText)
|
||
//if err != nil {
|
||
// fmt.Println("解密失败:", err)
|
||
// return
|
||
//}
|
||
//fmt.Println("解密后的文本:", decryptedText)
|
||
|
||
}
|
||
|
||
func TestGoApiTemplate(t *testing.T) {
|
||
//t1, err := template.ParseFiles("api.go.template")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//table := tools.SysTables{}
|
||
//table.TBName = "sys_tables"
|
||
//tab, _ := table.Get()
|
||
//file, err := os.Create("apis/" + table.PackageName + ".go")
|
||
//if err != nil {
|
||
// t.Error(err)
|
||
//}
|
||
//defer file.Close()
|
||
//
|
||
//_ = t1.Execute(file, tab)
|
||
//t.Log("")
|
||
|
||
dir, err := os.Getwd()
|
||
if err != nil {
|
||
fmt.Println("Error getting current directory:", err)
|
||
}
|
||
fmt.Println("Current working directory:", dir)
|
||
|
||
// 生成 AES 密钥
|
||
aesKey, err := GenerateAESKey()
|
||
if err != nil {
|
||
fmt.Printf("生成 AES 密钥失败: %v", err)
|
||
}
|
||
|
||
// 打印 AES 密钥
|
||
fmt.Printf("生成的 AES 密钥: %x\n", aesKey)
|
||
|
||
publicKeyPath := "/Users/max/Documents/code/deovo/telco_server/config/sms/public.pem"
|
||
|
||
// 使用公钥加密 AES 密钥
|
||
encryptedKey, err := EncryptWithRSA(publicKeyPath, aesKey)
|
||
if err != nil {
|
||
fmt.Printf("加密 AES 密钥失败: %v", err)
|
||
}
|
||
|
||
// 打印加密后的 AES 密钥(Base64 编码)
|
||
fmt.Printf("加密后的 AES 密钥(Base64 编码): %s\n", encryptedKey)
|
||
}
|
||
|
||
// GenerateAESKey 生成随机的 AES 密钥
|
||
func GenerateAESKey() ([]byte, error) {
|
||
// 生成 256 位(32 字节)的 AES 密钥
|
||
key := make([]byte, 32)
|
||
_, err := rand.Read(key)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return key, nil
|
||
}
|
||
|
||
// EncryptWithRSA 公钥加密 AES 密钥
|
||
func EncryptWithRSA(publicKeyPath string, aesKey []byte) (string, error) {
|
||
// 读取公钥文件
|
||
pubFile, err := os.Open(publicKeyPath)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer pubFile.Close()
|
||
|
||
// 解析公钥
|
||
pubBytes, err := io.ReadAll(pubFile)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
block, _ := pem.Decode(pubBytes)
|
||
if block == nil {
|
||
return "", fmt.Errorf("failed to parse PEM block containing the public key")
|
||
}
|
||
|
||
pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 使用公钥加密 AES 密钥
|
||
encryptedKey, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, aesKey)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
// 返回加密后的密钥(base64 编码)
|
||
return base64.StdEncoding.EncodeToString(encryptedKey), nil
|
||
}
|