encryptUtils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. return origData[:(length - unpadding)]
  47. }
  48. type ecb struct {
  49. b cipher.Block
  50. blockSize int
  51. }
  52. func newECB(b cipher.Block) *ecb {
  53. return &ecb{
  54. b: b,
  55. blockSize: b.BlockSize(),
  56. }
  57. }
  58. type ecbEncrypter ecb
  59. // NewECBEncrypter returns a BlockMode which encrypts in electronic code book
  60. // mode, using the given Block.
  61. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  62. return (*ecbEncrypter)(newECB(b))
  63. }
  64. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  65. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  66. if len(src)%x.blockSize != 0 {
  67. panic("crypto/cipher: input not full blocks")
  68. }
  69. if len(dst) < len(src) {
  70. panic("crypto/cipher: output smaller than input")
  71. }
  72. for len(src) > 0 {
  73. x.b.Encrypt(dst, src[:x.blockSize])
  74. src = src[x.blockSize:]
  75. dst = dst[x.blockSize:]
  76. }
  77. }
  78. type ecbDecrypter ecb
  79. // NewECBDecrypter returns a BlockMode which decrypts in electronic code book
  80. // mode, using the given Block.
  81. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  82. return (*ecbDecrypter)(newECB(b))
  83. }
  84. func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
  85. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  86. if len(src)%x.blockSize != 0 {
  87. panic("crypto/cipher: input not full blocks")
  88. }
  89. if len(dst) < len(src) {
  90. panic("crypto/cipher: output smaller than input")
  91. }
  92. for len(src) > 0 {
  93. x.b.Decrypt(dst, src[:x.blockSize])
  94. src = src[x.blockSize:]
  95. dst = dst[x.blockSize:]
  96. }
  97. }