encryptUtils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "errors"
  7. )
  8. // AESSecretKey AES密钥
  9. const AESSecretKey = "0d299ce2d4105282f7471074cb0f9f9d"
  10. // AESEncrypt AES加密
  11. func AESEncrypt(plaintext, key []byte) ([]byte, error) {
  12. block, err := aes.NewCipher(key)
  13. if err != nil {
  14. return nil, errors.New("错误的数据或密钥")
  15. }
  16. ecb := NewECBEncrypter(block)
  17. content := plaintext[:]
  18. content = PKCS5Padding(content, block.BlockSize())
  19. crypted := make([]byte, len(content))
  20. ecb.CryptBlocks(crypted, content)
  21. return crypted, nil
  22. }
  23. // AESDecrypt AES
  24. func AESDecrypt(crypted, key []byte) ([]byte, error) {
  25. block, err := aes.NewCipher(key)
  26. if err != nil {
  27. return nil, errors.New("错误的数据或密钥")
  28. }
  29. blockMode := NewECBDecrypter(block)
  30. origData := make([]byte, len(crypted))
  31. blockMode.CryptBlocks(origData, crypted)
  32. origData = PKCS5UnPadding(origData)
  33. return origData, nil
  34. }
  35. // PKCS5Padding ECB PKCS5Padding
  36. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  37. padding := blockSize - len(ciphertext)%blockSize
  38. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  39. return append(ciphertext, padtext...)
  40. }
  41. // PKCS5UnPadding ECB PKCS5Unpadding
  42. func PKCS5UnPadding(origData []byte) []byte {
  43. length := len(origData)
  44. // 去掉最后一个字节 unpadding 次
  45. unpadding := int(origData[length-1])
  46. // 防止数据越界崩溃
  47. if length-unpadding >= len(origData) {
  48. return origData
  49. }
  50. return origData[:(length - unpadding)]
  51. }
  52. type ecb struct {
  53. b cipher.Block
  54. blockSize int
  55. }
  56. func newECB(b cipher.Block) *ecb {
  57. return &ecb{
  58. b: b,
  59. blockSize: b.BlockSize(),
  60. }
  61. }
  62. type ecbEncrypter ecb
  63. // NewECBEncrypter returns a BlockMode which encrypts in electronic code book
  64. // mode, using the given Block.
  65. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  66. return (*ecbEncrypter)(newECB(b))
  67. }
  68. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  69. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  70. if len(src)%x.blockSize != 0 {
  71. panic("crypto/cipher: input not full blocks")
  72. }
  73. if len(dst) < len(src) {
  74. panic("crypto/cipher: output smaller than input")
  75. }
  76. for len(src) > 0 {
  77. x.b.Encrypt(dst, src[:x.blockSize])
  78. src = src[x.blockSize:]
  79. dst = dst[x.blockSize:]
  80. }
  81. }
  82. type ecbDecrypter ecb
  83. // NewECBDecrypter returns a BlockMode which decrypts in electronic code book
  84. // mode, using the given Block.
  85. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  86. return (*ecbDecrypter)(newECB(b))
  87. }
  88. func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
  89. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  90. if len(src)%x.blockSize != 0 {
  91. panic("crypto/cipher: input not full blocks")
  92. }
  93. if len(dst) < len(src) {
  94. panic("crypto/cipher: output smaller than input")
  95. }
  96. for len(src) > 0 {
  97. x.b.Decrypt(dst, src[:x.blockSize])
  98. src = src[x.blockSize:]
  99. dst = dst[x.blockSize:]
  100. }
  101. }