12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /**
- * Copyright (c) 2022 Yansen Zhang
- * wxcomponent is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- **/
-
- package encrypt
-
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "errors"
- )
-
- // AESEncode 使用 AES CBC 模式加密数据
- // data 为待加密数据
- func AESEncode(data, key []byte) ([]byte, error) {
- if nil == data || len(data) == 0 {
- return nil, errors.New("待加密 消息 为空")
- }
-
- if nil == key || len(key) == 0 {
- return nil, errors.New("加密 AESKey 为空")
- }
-
- block, e2 := aes.NewCipher(key)
- if e2 != nil {
- return nil, e2
- }
-
- blockSize := block.BlockSize()
- cipherData := pkcs7(data, blockSize)
- dist := make([]byte, len(cipherData))
-
- // CBC
- mode := cipher.NewCBCEncrypter(block, key[:blockSize])
- mode.CryptBlocks(dist, cipherData)
-
- // 如果需要拿到字符串, 请使用
- // fmt.Sprintf("%x", xxx)
- // 或者使用 base64 转码
- return dist, nil
- }
-
- // AESDecode 是 AESEncode 的反操作, 用来解密数据
- // data 为待解密数据
- func AESDecode(data, key []byte) ([]byte, error) {
- if nil == data || len(data) == 0 {
- return nil, errors.New("待解密 消息 为空")
- }
-
- if nil == key || len(key) == 0 {
- return nil, errors.New("解密 AESKey 为空")
- }
-
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
-
- blockSize := block.BlockSize()
- dist := make([]byte, len(data))
-
- mode := cipher.NewCBCDecrypter(block, key[:blockSize])
- mode.CryptBlocks(dist, data)
-
- // 如果需要拿到字符串, 请使用
- // fmt.Sprintf("%s", xxx)
- return unpkcs7(dist), nil
- }
-
- // pkcs7 填充
- func pkcs7(data []byte, blockSize int) []byte {
- paddingBlock := blockSize - len(data)%blockSize
- padding := bytes.Repeat([]byte{byte(paddingBlock)}, paddingBlock)
- return append(data, padding...)
- }
-
- // unpkcs7 取消 pkcs7 填充
- func unpkcs7(data []byte) []byte {
- length := len(data)
- unpadding := int(data[length-1])
- return data[:(length - unpadding)]
- }
|