1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package wxcomponent
  13. import (
  14. "encoding/base64"
  15. "encoding/xml"
  16. "gitee.com/yansen_zh/wxcomponent/utils/encrypt"
  17. "gitee.com/yansen_zh/wxcomponent/utils/log"
  18. )
  19. // EncryptMessage 待解密数据
  20. type EncryptMessage struct {
  21. XMLName xml.Name `xml:"xml"`
  22. ToUserName string `xml:"ToUserName"`
  23. Encrypt string `xml:"Encrypt"`
  24. }
  25. // DecodeMessage 解密消息
  26. func DecodeMessage(src []byte) (*encrypt.XMLMap, error) {
  27. log.Info("解码 xml 数据: ", string(src))
  28. msg := EncryptMessage{}
  29. if err := xml.Unmarshal(src, &msg); err != nil {
  30. log.Error("解码 xml 数据失败: ", err.Error())
  31. return nil, err
  32. }
  33. bt1, e1 := base64.StdEncoding.DecodeString(msg.Encrypt)
  34. if e1 != nil {
  35. log.Error("解码 base 数据失败: ", e1.Error())
  36. log.Error("原始 base64 数据: ", msg.Encrypt)
  37. return nil, e1
  38. }
  39. bt2, e2 := encrypt.MsgDecode(bt1, aesKey)
  40. if e2 != nil {
  41. log.Error("解码加密数据失败: ", e2.Error())
  42. log.Error("待解密数据: ", string(bt1))
  43. return nil, e2
  44. }
  45. res := encrypt.XMLMap{}
  46. if err := xml.Unmarshal(bt2, &res); err != nil {
  47. log.Error("解码 xml 数据失败: ", err.Error())
  48. log.Info("待解码 xml 数据: ", string(bt2))
  49. return nil, err
  50. }
  51. return &res, nil
  52. }