mirror of
https://git.oceanpay.cc/danial/kami_script.git
synced 2025-12-18 20:56:47 +00:00
33 lines
703 B
Go
33 lines
703 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
)
|
|
|
|
// 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...)
|
|
}
|