Files

32 lines
698 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package encrypt
import (
"bytes"
"crypto/aes"
"crypto/cipher"
)
// PKCS5 填充
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padText...)
}
func AesEncryptCBCPKCS5(plainText, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// PKCS5 填充(与 PKCS7 相同,只是 block size 为 8
paddedText := pkcs5Padding(plainText, block.BlockSize())
cipherText := make([]byte, len(paddedText))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText, paddedText)
return cipherText, nil
}