123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package utils
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "encoding/base64"
- "errors"
- )
- //加密过程:
- // 1、处理数据,对数据进行填充,采用PKCS7(当密钥长度不够时,缺几位补几个几)的方式。
- // 2、对数据进行加密,采用AES加密方法中CBC加密模式
- // 3、对得到的加密数据,进行base64加密,得到字符串
- // 解密过程相反
- //16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法
- //key不能泄露
- var PwdKey = []byte("ABCDABCDABCDABCD")
- //pkcs7Padding 填充
- func pkcs7Padding(data []byte, blockSize int) []byte {
- //判断缺少几位长度。最少1,最多 blockSize
- padding := blockSize - len(data)%blockSize
- //补足位数。把切片[]byte{byte(padding)}复制padding个
- padText := bytes.Repeat([]byte{byte(padding)}, padding)
- return append(data, padText...)
- }
- //pkcs7UnPadding 填充的反向操作
- func pkcs7UnPadding(data []byte) ([]byte, error) {
- length := len(data)
- if length == 0 {
- return nil, errors.New("加密字符串错误!")
- }
- //获取填充的个数
- unPadding := int(data[length-1])
- return data[:(length - unPadding)], nil
- }
- //AesEncrypt 加密
- func AesEncrypt(data []byte, key []byte, isDebug ...bool) ([]byte, error) {
- if len(isDebug) > 0 && isDebug[0] {
- return data, nil
- }
- //创建加密实例
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
- //判断加密快的大小
- blockSize := block.BlockSize()
- //填充
- encryptBytes := pkcs7Padding(data, blockSize)
- //初始化加密数据接收切片
- crypted := make([]byte, len(encryptBytes))
- //使用cbc加密模式
- blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
- //执行加密
- blockMode.CryptBlocks(crypted, encryptBytes)
- return crypted, nil
- }
- //AesDecrypt 解密
- func AesDecrypt(data []byte, key []byte, isDebug ...bool) ([]byte, error) {
- if len(isDebug) > 0 && isDebug[0] {
- return data, nil
- }
- //创建实例
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
- //获取块的大小
- blockSize := block.BlockSize()
- //使用cbc
- blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
- //初始化解密数据接收切片
- crypted := make([]byte, len(data))
- //执行解密
- blockMode.CryptBlocks(crypted, data)
- //去除填充
- crypted, err = pkcs7UnPadding(crypted)
- if err != nil {
- return nil, err
- }
- return crypted, nil
- }
- //EncryptByAes Aes加密 后 base64 再加
- func EncryptByAes(data, key []byte, isDebug ...bool) (string, error) {
- if len(isDebug) > 0 && isDebug[0] {
- return string(data), nil
- }
- res, err := AesEncrypt(data, key)
- if err != nil {
- return "", err
- }
- return base64.StdEncoding.EncodeToString(res), nil
- }
- //DecryptByAes Aes 解密
- func DecryptByAes(data string, key []byte, isDebug ...bool) ([]byte, error) {
- if len(isDebug) > 0 && isDebug[0] {
- return []byte(data), nil
- }
- dataByte, err := base64.StdEncoding.DecodeString(data)
- if err != nil {
- return nil, err
- }
- return AesDecrypt(dataByte, key)
- }
|