aes.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "errors"
  8. )
  9. //加密过程:
  10. // 1、处理数据,对数据进行填充,采用PKCS7(当密钥长度不够时,缺几位补几个几)的方式。
  11. // 2、对数据进行加密,采用AES加密方法中CBC加密模式
  12. // 3、对得到的加密数据,进行base64加密,得到字符串
  13. // 解密过程相反
  14. //16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法
  15. //key不能泄露
  16. var PwdKey = []byte("ABCDABCDABCDABCD")
  17. //pkcs7Padding 填充
  18. func pkcs7Padding(data []byte, blockSize int) []byte {
  19. //判断缺少几位长度。最少1,最多 blockSize
  20. padding := blockSize - len(data)%blockSize
  21. //补足位数。把切片[]byte{byte(padding)}复制padding个
  22. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  23. return append(data, padText...)
  24. }
  25. //pkcs7UnPadding 填充的反向操作
  26. func pkcs7UnPadding(data []byte) ([]byte, error) {
  27. length := len(data)
  28. if length == 0 {
  29. return nil, errors.New("加密字符串错误!")
  30. }
  31. //获取填充的个数
  32. unPadding := int(data[length-1])
  33. return data[:(length - unPadding)], nil
  34. }
  35. //AesEncrypt 加密
  36. func AesEncrypt(data []byte, key []byte, isDebug ...bool) ([]byte, error) {
  37. if len(isDebug) > 0 && isDebug[0] {
  38. return data, nil
  39. }
  40. //创建加密实例
  41. block, err := aes.NewCipher(key)
  42. if err != nil {
  43. return nil, err
  44. }
  45. //判断加密快的大小
  46. blockSize := block.BlockSize()
  47. //填充
  48. encryptBytes := pkcs7Padding(data, blockSize)
  49. //初始化加密数据接收切片
  50. crypted := make([]byte, len(encryptBytes))
  51. //使用cbc加密模式
  52. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  53. //执行加密
  54. blockMode.CryptBlocks(crypted, encryptBytes)
  55. return crypted, nil
  56. }
  57. //AesDecrypt 解密
  58. func AesDecrypt(data []byte, key []byte, isDebug ...bool) ([]byte, error) {
  59. if len(isDebug) > 0 && isDebug[0] {
  60. return data, nil
  61. }
  62. //创建实例
  63. block, err := aes.NewCipher(key)
  64. if err != nil {
  65. return nil, err
  66. }
  67. //获取块的大小
  68. blockSize := block.BlockSize()
  69. //使用cbc
  70. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  71. //初始化解密数据接收切片
  72. crypted := make([]byte, len(data))
  73. //执行解密
  74. blockMode.CryptBlocks(crypted, data)
  75. //去除填充
  76. crypted, err = pkcs7UnPadding(crypted)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return crypted, nil
  81. }
  82. //EncryptByAes Aes加密 后 base64 再加
  83. func EncryptByAes(data, key []byte, isDebug ...bool) (string, error) {
  84. if len(isDebug) > 0 && isDebug[0] {
  85. return string(data), nil
  86. }
  87. res, err := AesEncrypt(data, key)
  88. if err != nil {
  89. return "", err
  90. }
  91. return base64.StdEncoding.EncodeToString(res), nil
  92. }
  93. //DecryptByAes Aes 解密
  94. func DecryptByAes(data string, key []byte, isDebug ...bool) ([]byte, error) {
  95. if len(isDebug) > 0 && isDebug[0] {
  96. return []byte(data), nil
  97. }
  98. dataByte, err := base64.StdEncoding.DecodeString(data)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return AesDecrypt(dataByte, key)
  103. }