2023-06-30 04:00:53 +00:00
|
|
|
package encryption_decryption
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2023-07-28 04:15:56 +00:00
|
|
|
"crypto/rand"
|
2023-06-30 04:00:53 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2023-10-12 03:05:05 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-06-30 04:00:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 必须16字节
|
2023-07-28 04:15:56 +00:00
|
|
|
var cbckey = "fusen20230405145"
|
2023-06-30 04:00:53 +00:00
|
|
|
|
2023-10-12 03:05:05 +00:00
|
|
|
func Base64StrToNumberStr(base64Str string) string {
|
|
|
|
s := strings.Builder{}
|
|
|
|
for _, v := range base64Str {
|
|
|
|
s.WriteString(fmt.Sprintf("%d.", v))
|
|
|
|
}
|
|
|
|
return strings.TrimRight(s.String(), ".")
|
|
|
|
}
|
|
|
|
func NumberStrToBase64Str(numberStr string) (string, error) {
|
|
|
|
s := strings.Split(numberStr, ".")
|
|
|
|
b := make([]int32, 0, len(s))
|
|
|
|
for _, v := range s {
|
|
|
|
c, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
b = append(b, int32(c))
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 04:00:53 +00:00
|
|
|
// 加密(key必须16字节),前端加解密需要先把base64转字符串再取前16字节作为iv
|
|
|
|
func CBCEncrypt(data string) (string, error) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println("cbc decrypt err:", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-07-28 04:15:56 +00:00
|
|
|
block, err := aes.NewCipher([]byte(cbckey))
|
2023-06-30 04:00:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-28 04:15:56 +00:00
|
|
|
blockSize := len(cbckey)
|
2023-06-30 04:00:53 +00:00
|
|
|
padding := blockSize - len(data)%blockSize // 填充字节
|
|
|
|
if padding == 0 {
|
|
|
|
padding = blockSize
|
|
|
|
}
|
|
|
|
// 填充 padding 个 byte(padding) 到 data
|
|
|
|
data += string(bytes.Repeat([]byte{byte(padding)}, padding))
|
|
|
|
ciphertext := make([]byte, aes.BlockSize+len(data))
|
|
|
|
iv := ciphertext[:aes.BlockSize]
|
|
|
|
if _, err = rand.Read(iv); err != nil { // 将同时写到 ciphertext 的开头
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
mode := cipher.NewCBCEncrypter(block, iv)
|
|
|
|
mode.CryptBlocks(ciphertext[aes.BlockSize:], []byte(data))
|
|
|
|
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 解密(key必须16字节)
|
|
|
|
func CBCDecrypt(src string) (string, error) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println("cbc decrypt err:", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-07-28 04:15:56 +00:00
|
|
|
block, err := aes.NewCipher([]byte(cbckey))
|
2023-06-30 04:00:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ciphercode, err := base64.StdEncoding.DecodeString(src)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
iv := ciphercode[:aes.BlockSize] // 密文的前 16 个字节为 iv
|
|
|
|
ciphercode = ciphercode[aes.BlockSize:] // 正式密文
|
|
|
|
mode := cipher.NewCBCDecrypter(block, iv)
|
|
|
|
mode.CryptBlocks(ciphercode, ciphercode)
|
|
|
|
plaintext := string(ciphercode) // ↓ 减去 padding
|
|
|
|
return plaintext[:len(plaintext)-int(plaintext[len(plaintext)-1])], nil
|
|
|
|
}
|