12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package encryption
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "fmt"
- )
- type AesCBCWithPadding struct {
- key []byte
- iv []byte
- }
- func NewAesCBCWithPadding(key, iv []byte) *AesCBCWithPadding {
- return &AesCBCWithPadding{
- key: key,
- iv: iv,
- }
- }
- // ZeroPadding 填充(填充0x00到块大小)
- func zeroPad(data []byte, blockSize int) []byte {
- padding := blockSize - len(data)%blockSize
- padText := bytes.Repeat([]byte{0}, padding)
- return append(data, padText...)
- }
- // ZeroUnPadding 去除填充的0x00
- func zeroUnPad(data []byte) []byte {
- return bytes.TrimRightFunc(data, func(r rune) bool {
- return r == rune(0)
- })
- }
- // AES加密函数
- func (a *AesCBCWithPadding) Encrypt(plaintext []byte) ([]byte, error) {
- block, err := aes.NewCipher(a.key)
- if err != nil {
- return nil, err
- }
- //if len(plaintext)%aes.BlockSize != 0 {
- // return nil, fmt.Errorf("plaintext is not a multiple of the block size")
- //}
- plaintext = zeroPad(plaintext, aes.BlockSize)
- ciphertext := make([]byte, aes.BlockSize+len(plaintext))
- copy(ciphertext[:aes.BlockSize], a.iv)
- mode := cipher.NewCBCEncrypter(block, a.iv)
- mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
- return ciphertext[aes.BlockSize:], nil
- }
- // AES解密函数
- func (a *AesCBCWithPadding) Decrypt(ciphertext []byte) ([]byte, error) {
- block, err := aes.NewCipher(a.key)
- if err != nil {
- return nil, err
- }
- if len(ciphertext)%aes.BlockSize != 0 {
- return nil, fmt.Errorf("ciphertext is not a multiple of the block size")
- }
- decrypted := make([]byte, len(ciphertext))
- copy(decrypted, ciphertext)
- mode := cipher.NewCBCDecrypter(block, a.iv)
- mode.CryptBlocks(decrypted, ciphertext)
- plaintext := zeroUnPad(decrypted)
- return plaintext, nil
- }
|