github.com/kaydxh/golang@v0.0.131/go/crypto/aes/aes_cbc.go (about)

     1  /*
     2  MIT License
     3  
     4  Copyright (c) 2020 kay
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  SOFTWARE.
    23  */
    24  package aes
    25  
    26  import (
    27  	"bytes"
    28  	"crypto/aes"
    29  	"crypto/cipher"
    30  	"crypto/rand"
    31  	"io"
    32  )
    33  
    34  func AesCbcEncrypt(plainText, key []byte) ([]byte, error) {
    35  	if len(key) != 16 && len(key) != 32 {
    36  		return nil, ErrKeyLength
    37  	}
    38  
    39  	block, err := aes.NewCipher(key)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	paddingText := pad(plainText, aes.BlockSize)
    45  	cipherText := make([]byte, aes.BlockSize+len(paddingText))
    46  	iv := cipherText[:aes.BlockSize]
    47  	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	blockMode := cipher.NewCBCEncrypter(block, iv)
    52  	blockMode.CryptBlocks(cipherText[aes.BlockSize:], paddingText)
    53  	return cipherText, nil
    54  }
    55  
    56  func AesCbcDecrypt(cipherText, key []byte) ([]byte, error) {
    57  	if len(key) != 16 && len(key) != 24 {
    58  		return nil, ErrKeyLength
    59  	}
    60  
    61  	if len(cipherText) < aes.BlockSize {
    62  		return nil, ErrCipherTextLength
    63  	}
    64  
    65  	block, err := aes.NewCipher(key)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	iv := cipherText[:aes.BlockSize]
    70  	cb := cipherText[aes.BlockSize:]
    71  
    72  	blockMode := cipher.NewCBCDecrypter(block, iv)
    73  	blockMode.CryptBlocks(cb, cb)
    74  	plainText, err := unpad(cb)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return plainText, nil
    80  }
    81  
    82  func pad(plainText []byte, blockSize int) []byte {
    83  	padding := blockSize - (len(plainText) % blockSize)
    84  	padText := bytes.Repeat([]byte{byte(padding)}, padding)
    85  	newText := append(plainText, padText...)
    86  	return newText
    87  }
    88  
    89  func unpad(plainText []byte) ([]byte, error) {
    90  	length := len(plainText)
    91  	unpadding := int(plainText[length-1])
    92  	if unpadding > length {
    93  		return nil, ErrPaddingSize
    94  	}
    95  
    96  	return plainText[:length-unpadding], nil
    97  }