golang 的 rabbitmq 消费项目
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.

aes.go 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. postDataByte, _ := json.Marshal(postData)
  130. rdata, err := CurlPost(url, postDataByte, nil)
  131. if err != nil {
  132. return nil, err
  133. }
  134. pass, err := base64.StdEncoding.DecodeString(string(rdata))
  135. if err != nil {
  136. return nil, err
  137. }
  138. decrypt, err := crypto.Decrypt(pass)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return decrypt, nil
  143. }