golang 的 rabbitmq 消费项目
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

173 linhas
4.2 KiB

  1. package utils
  2. import (
  3. "applet/app/cfg"
  4. "bytes"
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "encoding/base64"
  8. "encoding/json"
  9. "fmt"
  10. )
  11. func AesEncrypt(rawData, key []byte) ([]byte, error) {
  12. block, err := aes.NewCipher(key)
  13. if err != nil {
  14. return nil, err
  15. }
  16. blockSize := block.BlockSize()
  17. rawData = PKCS5Padding(rawData, blockSize)
  18. // rawData = ZeroPadding(rawData, block.BlockSize())
  19. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  20. encrypted := make([]byte, len(rawData))
  21. // 根据CryptBlocks方法的说明,如下方式初始化encrypted也可以
  22. // encrypted := rawData
  23. blockMode.CryptBlocks(encrypted, rawData)
  24. return encrypted, nil
  25. }
  26. func AesDecrypt(encrypted, key []byte) ([]byte, error) {
  27. block, err := aes.NewCipher(key)
  28. if err != nil {
  29. return nil, err
  30. }
  31. blockSize := block.BlockSize()
  32. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  33. rawData := make([]byte, len(encrypted))
  34. // rawData := encrypted
  35. blockMode.CryptBlocks(rawData, encrypted)
  36. rawData = PKCS5UnPadding(rawData)
  37. // rawData = ZeroUnPadding(rawData)
  38. return rawData, nil
  39. }
  40. func ZeroPadding(cipherText []byte, blockSize int) []byte {
  41. padding := blockSize - len(cipherText)%blockSize
  42. padText := bytes.Repeat([]byte{0}, padding)
  43. return append(cipherText, padText...)
  44. }
  45. func ZeroUnPadding(rawData []byte) []byte {
  46. length := len(rawData)
  47. unPadding := int(rawData[length-1])
  48. return rawData[:(length - unPadding)]
  49. }
  50. func PKCS5Padding(cipherText []byte, blockSize int) []byte {
  51. padding := blockSize - len(cipherText)%blockSize
  52. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  53. return append(cipherText, padText...)
  54. }
  55. func PKCS5UnPadding(rawData []byte) []byte {
  56. length := len(rawData)
  57. // 去掉最后一个字节 unPadding 次
  58. unPadding := int(rawData[length-1])
  59. return rawData[:(length - unPadding)]
  60. }
  61. // 填充0
  62. func zeroFill(key *string) {
  63. l := len(*key)
  64. if l != 16 && l != 24 && l != 32 {
  65. if l < 16 {
  66. *key = *key + fmt.Sprintf("%0*d", 16-l, 0)
  67. } else if l < 24 {
  68. *key = *key + fmt.Sprintf("%0*d", 24-l, 0)
  69. } else if l < 32 {
  70. *key = *key + fmt.Sprintf("%0*d", 32-l, 0)
  71. } else {
  72. *key = string([]byte(*key)[:32])
  73. }
  74. }
  75. }
  76. type AesCrypt struct {
  77. Key []byte
  78. Iv []byte
  79. }
  80. func (a *AesCrypt) Encrypt(data []byte) ([]byte, error) {
  81. aesBlockEncrypt, err := aes.NewCipher(a.Key)
  82. if err != nil {
  83. println(err.Error())
  84. return nil, err
  85. }
  86. content := pKCS5Padding(data, aesBlockEncrypt.BlockSize())
  87. cipherBytes := make([]byte, len(content))
  88. aesEncrypt := cipher.NewCBCEncrypter(aesBlockEncrypt, a.Iv)
  89. aesEncrypt.CryptBlocks(cipherBytes, content)
  90. return cipherBytes, nil
  91. }
  92. func (a *AesCrypt) Decrypt(src []byte) (data []byte, err error) {
  93. decrypted := make([]byte, len(src))
  94. var aesBlockDecrypt cipher.Block
  95. aesBlockDecrypt, err = aes.NewCipher(a.Key)
  96. if err != nil {
  97. println(err.Error())
  98. return nil, err
  99. }
  100. aesDecrypt := cipher.NewCBCDecrypter(aesBlockDecrypt, a.Iv)
  101. aesDecrypt.CryptBlocks(decrypted, src)
  102. return pKCS5Trimming(decrypted), nil
  103. }
  104. func pKCS5Padding(cipherText []byte, blockSize int) []byte {
  105. padding := blockSize - len(cipherText)%blockSize
  106. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  107. return append(cipherText, padText...)
  108. }
  109. func pKCS5Trimming(encrypt []byte) []byte {
  110. padding := encrypt[len(encrypt)-1]
  111. return encrypt[:len(encrypt)-int(padding)]
  112. }
  113. // AesAdminCurlPOST is 与后台接口加密交互
  114. func AesAdminCurlPOST(aesData string, url string) ([]byte, error) {
  115. adminKey := cfg.Admin.AesKey
  116. adminVI := cfg.Admin.AesIV
  117. crypto := AesCrypt{
  118. Key: []byte(adminKey),
  119. Iv: []byte(adminVI),
  120. }
  121. encrypt, err := crypto.Encrypt([]byte(aesData))
  122. if err != nil {
  123. return nil, err
  124. }
  125. // 发送请求到后台
  126. postData := map[string]string{
  127. "postData": base64.StdEncoding.EncodeToString(encrypt),
  128. }
  129. fmt.Println(adminKey)
  130. fmt.Println(adminVI)
  131. fmt.Println("=======ADMIN请求=====")
  132. fmt.Println(postData)
  133. postDataByte, _ := json.Marshal(postData)
  134. rdata, err := CurlPost(url, postDataByte, nil)
  135. fmt.Println(err)
  136. if err != nil {
  137. return nil, err
  138. }
  139. fmt.Println(rdata)
  140. pass, err := base64.StdEncoding.DecodeString(string(rdata))
  141. if err != nil {
  142. return nil, err
  143. }
  144. fmt.Println(pass)
  145. decrypt, err := crypto.Decrypt(pass)
  146. fmt.Println(err)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return decrypt, nil
  151. }