temp
This commit is contained in:
89
aes.go
89
aes.go
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
@@ -19,6 +20,19 @@ func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
|
||||
// 去除PKCS7填充
|
||||
func PKCS7UnPadding(origData []byte) ([]byte, error) {
|
||||
length := len(origData)
|
||||
if length == 0 {
|
||||
return nil, errors.New("input data error")
|
||||
}
|
||||
unpadding := int(origData[length-1])
|
||||
if unpadding > length {
|
||||
return nil, errors.New("unpadding size is invalid")
|
||||
}
|
||||
return origData[:(length - unpadding)], nil
|
||||
}
|
||||
|
||||
// AES CBC模式加密,Base64传入传出
|
||||
func AesEncrypt(plainText, key []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
@@ -41,9 +55,39 @@ func AesEncrypt(plainText, key []byte) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
// AES CBC模式解密,Base64传入传出
|
||||
func AesDecrypt(cipherTextBase64 string, key []byte) ([]byte, error) {
|
||||
cipherText, err := base64.StdEncoding.DecodeString(cipherTextBase64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockSize := block.BlockSize()
|
||||
if len(cipherText) < blockSize {
|
||||
return nil, errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
iv := cipherText[:blockSize]
|
||||
cipherText = cipherText[blockSize:]
|
||||
|
||||
if len(cipherText)%blockSize != 0 {
|
||||
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, cipherText)
|
||||
|
||||
plainText, err := PKCS7UnPadding(cipherText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -52,25 +96,34 @@ func main() {
|
||||
|
||||
var data interface{}
|
||||
|
||||
data = map[string]interface{}{
|
||||
"id_card": "45212220000827423X",
|
||||
"name": "张荣宏",
|
||||
}
|
||||
|
||||
// 将结构体转为 JSON 字符串
|
||||
jsonData, err := json.Marshal(data)
|
||||
decrypt, err := AesDecrypt("AEQHGRyEolE4o8GoZaWuEFGpGRLEvEMCNM54KzZ/HdnvfOtyFDfn1PVpD6Cb37rQpJm3CqZYLDJWmEcU0f7IeD0MG8Zh1PIvSEwJVEyI2OkZjooZNjuchARpqeG40sS8hFjQt/xVP3qBVsF6+tdnI8GNc8EdLpZ8ja4c/xVprhjr9yRmRHZOVvECFiJkqwrve8jq2vuC5CowNjc4THK8zRmJI+qWDazUL4vntdWFmqG4YmHDZm/UFecevDNMhWRfdeWE2aI9lTXZ/X/gtHEcVyzbx8cltdn+4KuR3t6VPf3edWDSDM0PTxSNbWwPgaTC1sMbBJiFJLW+cUgxJhfQqE1FF5yY+t6V7l8vfW6cUV4xRCFZ/clFvidnYev2CocvMKzf6gkbO/El2a4VA5ustRwL1G3hxMf9fd+5yGT5dEGci+cbysGHTKzNeHf75az1RgW2WEYUBUc7czNiZgkaIA==", key)
|
||||
if err != nil {
|
||||
fmt.Println("JSON 序列化错误:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 对 JSON 数据进行加密
|
||||
encryptedData, err := AesEncrypt(jsonData, key)
|
||||
err = json.Unmarshal(decrypt, &data)
|
||||
if err != nil {
|
||||
fmt.Println("加密错误:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 输出加密后的结果
|
||||
fmt.Println("加密后的数据:", encryptedData)
|
||||
fmt.Println(data)
|
||||
//data = map[string]interface{}{
|
||||
// "id_card": "45212220000827423X",
|
||||
// "name": "张荣宏",
|
||||
//}
|
||||
//
|
||||
//// 将结构体转为 JSON 字符串
|
||||
//jsonData, err := json.Marshal(data)
|
||||
//if err != nil {
|
||||
// fmt.Println("JSON 序列化错误:", err)
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//// 对 JSON 数据进行加密
|
||||
//encryptedData, err := AesEncrypt(jsonData, key)
|
||||
//if err != nil {
|
||||
// fmt.Println("加密错误:", err)
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//// 输出加密后的结果
|
||||
//fmt.Println("加密后的数据:", encryptedData)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user