- 删除了AesCBCDecrypt函数,不再支持AES/CBC/PKCS7Padding解密方式- 在token.go中引入hex包用于密钥和IV的解码 - 使用hex.DecodeString对secretCfg中的Key和IV进行解码 - 添加日志记录key和iv解码失败的情况 - 加密失败时增加错误日志输出 - 保持原有加密功能不变,仅调整实现细节和依赖处理
151 lines
3.8 KiB
Go
151 lines
3.8 KiB
Go
package utils
|
|
|
|
/***************************************************
|
|
** @Desc : This file for ...
|
|
** @Time : 2018-8-30 13:50
|
|
** @Author : Joker
|
|
** @File : ACE_ECB
|
|
** @Last Modified by : Joker
|
|
** @Last Modified time: 2018-8-30 13:50
|
|
** @Software: GoLand
|
|
****************************************************/
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/hex"
|
|
"github.com/beego/beego/v2/core/logs"
|
|
)
|
|
|
|
// AesDecrypt
|
|
/*
|
|
aes解码
|
|
crypted:要加密的字符串
|
|
key:用来加密的密钥 密钥长度可以是128bit、192bit、256bit中的任意一个
|
|
16位key对应128bit
|
|
*/
|
|
func AesDecrypt(src, key string) []byte {
|
|
plaintext, err := hex.DecodeString(src)
|
|
|
|
if err != nil {
|
|
logs.Error("Joker: AesEncrypt failed to NewCipher")
|
|
logs.Error(err)
|
|
}
|
|
|
|
block, err := aes.NewCipher([]byte(key))
|
|
|
|
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
|
|
iv := ciphertext[:aes.BlockSize] // 初始化向量
|
|
d := cipher.NewCBCDecrypter(block, iv)
|
|
d.CryptBlocks(ciphertext[aes.BlockSize:], ciphertext[aes.BlockSize:])
|
|
|
|
// 去除填充
|
|
padding := int(ciphertext[len(ciphertext)-1])
|
|
plaintext = ciphertext[aes.BlockSize : len(ciphertext)-padding]
|
|
return plaintext
|
|
}
|
|
|
|
// AesCBCEncrypt AES/CBC/PKCS7Padding 加密
|
|
func AesCBCEncrypt(plaintext []byte, key []byte, iv []byte) ([]byte, error) {
|
|
// AES
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// PKCS7 填充
|
|
plaintext = paddingPKCS7(plaintext, aes.BlockSize)
|
|
|
|
// CBC 加密
|
|
mode := cipher.NewCBCEncrypter(block, iv)
|
|
mode.CryptBlocks(plaintext, plaintext)
|
|
|
|
return plaintext, nil
|
|
}
|
|
|
|
// PKCS7 填充
|
|
func paddingPKCS7(plaintext []byte, blockSize int) []byte {
|
|
paddingSize := blockSize - len(plaintext)%blockSize
|
|
paddingText := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
|
|
return append(plaintext, paddingText...)
|
|
}
|
|
|
|
// PKCS7 反填充
|
|
func unPaddingPKCS7(s []byte) []byte {
|
|
length := len(s)
|
|
if length == 0 {
|
|
return s
|
|
}
|
|
unPadding := int(s[length-1])
|
|
return s[:(length - unPadding)]
|
|
}
|
|
|
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
|
padding := blockSize - len(ciphertext)%blockSize
|
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
return append(ciphertext, padtext...)
|
|
}
|
|
|
|
func PKCS5UnPadding(origData []byte) []byte {
|
|
length := len(origData)
|
|
// 去掉最后一个字节 unpadding 次
|
|
unpadding := int(origData[length-1])
|
|
return origData[:(length - unpadding)]
|
|
}
|
|
|
|
type ecb struct {
|
|
b cipher.Block
|
|
blockSize int
|
|
}
|
|
|
|
func newECB(b cipher.Block) *ecb {
|
|
return &ecb{
|
|
b: b,
|
|
blockSize: b.BlockSize(),
|
|
}
|
|
}
|
|
|
|
type ecbEncrypter ecb
|
|
|
|
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
|
|
// mode, using the given Block.
|
|
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
|
|
return (*ecbEncrypter)(newECB(b))
|
|
}
|
|
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
|
|
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
|
if len(src)%x.blockSize != 0 {
|
|
logs.Error("Joker: CryptBlocks`s input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
logs.Error("Joker: CryptBlocks`s output smaller than input")
|
|
}
|
|
for len(src) > 0 {
|
|
x.b.Encrypt(dst, src[:x.blockSize])
|
|
src = src[x.blockSize:]
|
|
dst = dst[x.blockSize:]
|
|
}
|
|
}
|
|
|
|
type ecbDecrypter ecb
|
|
|
|
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
|
|
// mode, using the given Block.
|
|
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
|
|
return (*ecbDecrypter)(newECB(b))
|
|
}
|
|
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
|
|
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
|
if len(src)%x.blockSize != 0 {
|
|
logs.Error("Joker: CryptBlocks`s input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
logs.Error("Joker: CryptBlocks`s output smaller than input")
|
|
}
|
|
for len(src) > 0 {
|
|
x.b.Decrypt(dst, src[:x.blockSize])
|
|
src = src[x.blockSize:]
|
|
dst = dst[x.blockSize:]
|
|
}
|
|
}
|