naza/pkg/crypto/aes_cbc.go

46 lines
1.1 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package crypto
import (
"crypto/aes"
"crypto/cipher"
)
// TODO(chef): package crypto和std重复重命名加上naza前缀
var CommonIv = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
// @param key 16字节 -> AES128
// 24字节 -> AES192
// 32字节 -> AES256
func EncryptAesWithCbc(in []byte, key []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
encrypter := cipher.NewCBCEncrypter(block, iv)
out := make([]byte, len(in))
copy(out, in)
encrypter.CryptBlocks(out, out)
return out, nil
}
func DecryptAesWithCbc(in []byte, key []byte, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
decrypter := cipher.NewCBCDecrypter(block, iv)
out := make([]byte, len(in))
copy(out, in)
decrypter.CryptBlocks(out, out)
return out, nil
}