156 lines
4.0 KiB
Go
156 lines
4.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/hex"
|
|
"gateway/internal/otelTrace"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func AesDecrypt(ctx context.Context, src, key []byte) []byte {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("Joker: AesDecrypt failed to NewCipher", zap.Error(err))
|
|
}
|
|
blockMode := NewECBDecrypter(block)
|
|
origData := make([]byte, len(src))
|
|
blockMode.CryptBlocks(origData, src)
|
|
origData = PKCS5UnPadding(origData)
|
|
return origData
|
|
}
|
|
|
|
/*aes编码*/
|
|
func AesEncrypt(ctx context.Context, src []byte, key string) []byte {
|
|
decodeString, err := hex.DecodeString(key)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("Joker: AesEncrypt failed to hex key", zap.Error(err))
|
|
}
|
|
block, err := aes.NewCipher(decodeString)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("Joker: AesEncrypt failed to NewCipher")
|
|
}
|
|
if len(src) < 0 {
|
|
otelTrace.Logger.WithContext(ctx).Error("Joker: AesEncrypt`s input is null ")
|
|
}
|
|
ecb := NewECBEncrypter(block)
|
|
src = PKCS5Padding(src, block.BlockSize())
|
|
crypted := make([]byte, len(src))
|
|
ecb.CryptBlocks(crypted, src)
|
|
// 普通base64编码加密 区别于urlsafe base64
|
|
// fmt.Println("base64 result:", base64.StdEncoding.EncodeToString(crypted))
|
|
// fmt.Println("base64UrlSafe result:", Base64UrlSafeEncode(crypted))
|
|
return crypted
|
|
}
|
|
|
|
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 {
|
|
return
|
|
// otel.Logger.WithContext(ctx).Error("Joker: CryptBlocks`s input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
return
|
|
}
|
|
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 {
|
|
return
|
|
// otel.Logger.WithContext(ctx).Error("Joker: CryptBlocks`s input not full blocks")
|
|
}
|
|
if len(dst) < len(src) {
|
|
return
|
|
}
|
|
for len(src) > 0 {
|
|
x.b.Decrypt(dst, src[:x.blockSize])
|
|
src = src[x.blockSize:]
|
|
dst = dst[x.blockSize:]
|
|
}
|
|
}
|
|
|
|
func PadType(text string) []byte {
|
|
blockSize := aes.BlockSize
|
|
bytesNumToPad := blockSize - (len(text) % blockSize)
|
|
byteToPad := byte(bytesNumToPad)
|
|
padding := make([]byte, bytesNumToPad)
|
|
for i := range padding {
|
|
padding[i] = byteToPad
|
|
}
|
|
padded := append([]byte(text), padding...)
|
|
return padded
|
|
}
|
|
|
|
// 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...)
|
|
}
|