Blame view

common/components/aes.go 2.62 KB
b499527e   zhangqijia   feat: 消息包用aes加密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  package components
  
  import (
  	"bytes"
  	"crypto/aes"
  	"crypto/cipher"
  	"encoding/base64"
  	"errors"
  )
  
  var (
  	PwdKey = []byte("luluszhaozhaoAAA") //RC4 zhaolu2333
  )
  
  type AesEncipher struct {
  }
  
  func NewAesEncipher() IEncipher {
  	return &AesEncipher{}
  }
  
  //pkcs7Padding 填充
  func (a *AesEncipher) 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 (a *AesEncipher) 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 (a *AesEncipher) Encrypt(data []byte) ([]byte, error) {
  	//创建加密实例
  	block, err := aes.NewCipher(PwdKey)
  	if err != nil {
  		return nil, err
  	}
  	//判断加密快的大小
  	blockSize := block.BlockSize()
  	//填充
  	encryptBytes := a.pkcs7Padding(data, blockSize)
  	if len(encryptBytes)%blockSize != 0 {
  		return nil, errors.New("crypto/cipher: input not full blocks")
  	}
  	//初始化加密数据接收切片
  	crypted := make([]byte, len(encryptBytes))
  	//使用cbc加密模式
  	blockMode := cipher.NewCBCEncrypter(block, PwdKey[:blockSize])
  	//执行加密
  	blockMode.CryptBlocks(crypted, encryptBytes)
  	return crypted, nil
  }
  
  //AesDecrypt 解密
  func (a *AesEncipher) Decrypt(data []byte) ([]byte, error) {
  	//创建实例
  	block, err := aes.NewCipher(PwdKey)
  	if err != nil {
  		return nil, err
  	}
  	//获取块的大小
  	blockSize := block.BlockSize()
  	//使用cbc
  	blockMode := cipher.NewCBCDecrypter(block, PwdKey[:blockSize])
  	//初始化解密数据接收切片
  	dataLen := len(data)
  	if dataLen%blockSize != 0 {
  		return nil, errors.New("crypto/cipher: input not full blocks")
  	}
  	crypted := make([]byte, dataLen)
  	//执行解密
  	blockMode.CryptBlocks(crypted, data)
  	//去除填充
  	crypted, err = a.pkcs7UnPadding(crypted)
  	if err != nil {
  		return nil, err
  	}
  	return crypted, nil
  }
  
  //EncryptByAes Aes加密 后 base64 再加
  func (a *AesEncipher) EncryptByAes(data []byte) (string, error) {
  	res, err := a.Encrypt(data)
  	if err != nil {
  		return "", err
  	}
  	return base64.StdEncoding.EncodeToString(res), nil
  }
  
  //DecryptByAes Aes 解密
  func (a *AesEncipher) DecryptByAes(data string) ([]byte, error) {
  	dataByte, err := base64.StdEncoding.DecodeString(data)
  	if err != nil {
  		return nil, err
  	}
  	return a.Decrypt(dataByte)
  }