- dingding callback
This commit is contained in:
43
utils/utils_crypt.go
Normal file
43
utils/utils_crypt.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
)
|
||||
|
||||
func AESCBCEncpryt(data, aesKey, iv []byte) (encryptedData []byte, err error) {
|
||||
c, err := aes.NewCipher(aesKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfbdec := cipher.NewCBCEncrypter(c, iv[:c.BlockSize()])
|
||||
data = PKCSPadding(data, c.BlockSize())
|
||||
encryptedData = make([]byte, len(data))
|
||||
cfbdec.CryptBlocks(encryptedData, data)
|
||||
return encryptedData, nil
|
||||
}
|
||||
|
||||
func AESCBCDecpryt(encryptedData, aesKey, iv []byte) (decryptedData []byte, err error) {
|
||||
c, err := aes.NewCipher(aesKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfbdec := cipher.NewCBCDecrypter(c, iv[:c.BlockSize()])
|
||||
decryptedData = make([]byte, len(encryptedData))
|
||||
cfbdec.CryptBlocks(decryptedData, encryptedData)
|
||||
decryptedData = PKCSUnPadding(decryptedData)
|
||||
return decryptedData, nil
|
||||
}
|
||||
|
||||
func PKCSUnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
return origData[:(length - unpadding)]
|
||||
}
|
||||
|
||||
func PKCSPadding(ciphertext []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(ciphertext)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(ciphertext, padtext...)
|
||||
}
|
||||
Reference in New Issue
Block a user