劲创营---任务项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

192 lines
5.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. "errors"
  10. "fmt"
  11. "strings"
  12. )
  13. func AesEncrypt(rawData, key []byte) ([]byte, error) {
  14. block, err := aes.NewCipher(key)
  15. if err != nil {
  16. return nil, err
  17. }
  18. blockSize := block.BlockSize()
  19. rawData = PKCS5Padding(rawData, blockSize)
  20. // rawData = ZeroPadding(rawData, block.BlockSize())
  21. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  22. encrypted := make([]byte, len(rawData))
  23. // 根据CryptBlocks方法的说明,如下方式初始化encrypted也可以
  24. // encrypted := rawData
  25. blockMode.CryptBlocks(encrypted, rawData)
  26. return encrypted, nil
  27. }
  28. func AesDecrypt(encrypted, key []byte) ([]byte, error) {
  29. block, err := aes.NewCipher(key)
  30. if err != nil {
  31. return nil, err
  32. }
  33. blockSize := block.BlockSize()
  34. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  35. rawData := make([]byte, len(encrypted))
  36. // rawData := encrypted
  37. blockMode.CryptBlocks(rawData, encrypted)
  38. rawData = PKCS5UnPadding(rawData)
  39. // rawData = ZeroUnPadding(rawData)
  40. return rawData, nil
  41. }
  42. func ZeroPadding(cipherText []byte, blockSize int) []byte {
  43. padding := blockSize - len(cipherText)%blockSize
  44. padText := bytes.Repeat([]byte{0}, padding)
  45. return append(cipherText, padText...)
  46. }
  47. func ZeroUnPadding(rawData []byte) []byte {
  48. length := len(rawData)
  49. unPadding := int(rawData[length-1])
  50. return rawData[:(length - unPadding)]
  51. }
  52. func PKCS5Padding(cipherText []byte, blockSize int) []byte {
  53. padding := blockSize - len(cipherText)%blockSize
  54. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  55. return append(cipherText, padText...)
  56. }
  57. func PKCS5UnPadding(rawData []byte) []byte {
  58. length := len(rawData)
  59. // 去掉最后一个字节 unPadding 次
  60. unPadding := int(rawData[length-1])
  61. return rawData[:(length - unPadding)]
  62. }
  63. // 填充0
  64. func zeroFill(key *string) {
  65. l := len(*key)
  66. if l != 16 && l != 24 && l != 32 {
  67. if l < 16 {
  68. *key = *key + fmt.Sprintf("%0*d", 16-l, 0)
  69. } else if l < 24 {
  70. *key = *key + fmt.Sprintf("%0*d", 24-l, 0)
  71. } else if l < 32 {
  72. *key = *key + fmt.Sprintf("%0*d", 32-l, 0)
  73. } else {
  74. *key = string([]byte(*key)[:32])
  75. }
  76. }
  77. }
  78. type AesCrypt struct {
  79. Key []byte
  80. Iv []byte
  81. }
  82. func (a *AesCrypt) Encrypt(data []byte) ([]byte, error) {
  83. aesBlockEncrypt, err := aes.NewCipher(a.Key)
  84. if err != nil {
  85. println(err.Error())
  86. return nil, err
  87. }
  88. content := pKCS5Padding(data, aesBlockEncrypt.BlockSize())
  89. cipherBytes := make([]byte, len(content))
  90. aesEncrypt := cipher.NewCBCEncrypter(aesBlockEncrypt, a.Iv)
  91. aesEncrypt.CryptBlocks(cipherBytes, content)
  92. return cipherBytes, nil
  93. }
  94. func (a *AesCrypt) Decrypt(src []byte) (data []byte, err error) {
  95. decrypted := make([]byte, len(src))
  96. var aesBlockDecrypt cipher.Block
  97. aesBlockDecrypt, err = aes.NewCipher(a.Key)
  98. if err != nil {
  99. println(err.Error())
  100. return nil, err
  101. }
  102. aesDecrypt := cipher.NewCBCDecrypter(aesBlockDecrypt, a.Iv)
  103. aesDecrypt.CryptBlocks(decrypted, src)
  104. return pKCS5Trimming(decrypted), nil
  105. }
  106. func pKCS5Padding(cipherText []byte, blockSize int) []byte {
  107. padding := blockSize - len(cipherText)%blockSize
  108. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  109. return append(cipherText, padText...)
  110. }
  111. func pKCS5Trimming(encrypt []byte) []byte {
  112. padding := encrypt[len(encrypt)-1]
  113. return encrypt[:len(encrypt)-int(padding)]
  114. }
  115. // AesAdminCurlPOST is 与后台接口加密交互
  116. func AesAdminCurlPOST(aesData string, url string, uid int, isAgain ...bool) ([]byte, error, bool) {
  117. adminKey := cfg.Admin.AesKey
  118. adminVI := cfg.Admin.AesIV
  119. crypto := AesCrypt{
  120. Key: []byte(adminKey),
  121. Iv: []byte(adminVI),
  122. }
  123. encrypt, err := crypto.Encrypt([]byte(aesData))
  124. if err != nil {
  125. return nil, err, false
  126. }
  127. // 发送请求到后台
  128. postData := map[string]string{
  129. "postData": base64.StdEncoding.EncodeToString(encrypt),
  130. }
  131. fmt.Println(adminKey)
  132. fmt.Println(adminVI)
  133. fmt.Println("=======ADMIN请求=====")
  134. fmt.Println(postData)
  135. postDataByte, _ := json.Marshal(postData)
  136. rdata, err := CurlPost(url, postDataByte, nil)
  137. fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>", err)
  138. if err != nil {
  139. return nil, err, false
  140. }
  141. retry := false
  142. if len(isAgain) > 0 {
  143. retry = isAgain[0]
  144. }
  145. //todo::判断是否为异常,则错误处理
  146. if strings.Index(string(rdata), "Notice") != -1 || strings.Index(string(rdata), "Fatal error") != -1 {
  147. FilePutContents("admin_curl_error", fmt.Sprintf("[请求失败]curl Result返回:%s uid:%d, >>>>>>>>>>>>>>>>>>>>", string(rdata), uid))
  148. return nil, errors.New("支付宝提现异常,请稍后重试!"), false
  149. }
  150. //todo::判断是否为响应超时(且不是重新请求)
  151. if strings.Index(string(rdata), "504 Gateway Time-out") != -1 && retry == true {
  152. FilePutContents("time_out", fmt.Sprintf("[请求超时]curl Result返回:%s uid:%d, >>>>>>>>>>>>>>>>>>>>", string(rdata), uid))
  153. return nil, err, true
  154. }
  155. FilePutContents("cash_out", fmt.Sprintf("curl Result返回:%s %s uid:%d, >>>>>>>>>>>>>>>>>>>>", url, string(rdata), uid))
  156. pass, err := base64.StdEncoding.DecodeString(string(rdata))
  157. if err != nil {
  158. return nil, err, false
  159. }
  160. fmt.Println(pass)
  161. decrypt, err := crypto.Decrypt(pass)
  162. fmt.Println(err)
  163. if err != nil {
  164. return nil, err, false
  165. }
  166. return decrypt, nil, false
  167. }