处理微信支付退款结果通知时,需要对返回数据进行AES-256-ECB解密,golang标准库未支持AES ECB加解密,特此记录。

一、解密方式

解密步骤如下

  1. 对加密串A做base64解码,得到加密串B
  2. 对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)–>账户设置–>API安全–>密钥设置 )
  3. 用key*对加密串B做AES-256-ECB解密

二、Golang 示例代码

package main

import (
	"encoding/base64"
	"fmt"
	"strings"
	"github.com/nanjishidu/gomini/gocrypto"
)

func main() {
	var (
		paykey   = "ziR0QKsTUfMOuochC9RfCdmfHECorQAP"
		req_info = `YYwp8C48th0wnQzTqeI+41pflB26v+smFj9z6h9RPBgxTyZyxc+4YNEz7QEgZNWj/6rIb2MfyWMZmCc41CfjKSssoSZPXxOhUayb6KvNSZ1p6frOX1PDWzhyruXK7ouNND+gDsG4yZ0XXzsL4/pYNwLLba/71QrnkJ/BHcByk4EXnglju5DLup9pJQSnTxjomI9Rxu57m9jg5lLQFxMWXyeASZJNvof0ulnHlWJswS4OxKOkmW7VEyKyLGV6npoOm03Qsx2wkRxLsSa9gPpg4hdaReeUqh1FMbm7aWjyrVYT/MEZWg98p4GomEIYvz34XfDncTezX4bf/ZiSLXt79aE1/YTZrYfymXeCrGjlbe0rg/T2ezJHAC870u2vsVbY1/KcE2A443N+DEnAziXlBQ1AeWq3Rqk/O6/TMM0lomzgctAOiAMg+bh5+Gu1ubA9O3E+vehULydD5qx2o6i3+qA9ORbH415NyRrQdeFq5vmCiRikp5xYptWiGZA0tkoaLKMPQ4ndE5gWHqiBbGPfULZWokI+QjjhhBmwgbd6J0VqpRorwOuzC/BHdkP72DCdNcm7IDUpggnzBIy0+seWIkcHEryKjge3YDHpJeQCqrAH0CgxXHDt1xtbQbST1VqFyuhPhUjDXMXrknrGPN/oE1t0rLRq+78cI+k8xe5E6seeUXQsEe8r3358mpcDYSmXWSXVZxK6er9EF98APqHwcndyEJD2YyCh/mMVhERuX+7kjlRXSiNUWa/Cv/XAKFQuvUYA5ea2eYWtPRHa4DpyuF1SNsaqVKfgqKXZrJHfAgslVpSVqUpX4zkKszHF4kwMZO3M7J1P94Mxa7Tm9mTOJePOoHPXeEB+m9rX6pSfoi3mJDQ5inJ+Vc4gOkg/Wd/lqiy6TTyP/dHDN6/v+AuJx5AXBo/2NDD3fWhHjkqEKIuARr2ClZt9ZRQO4HkXdZo7CN06sGCHk48Tg8PmxnxKcMZm7Aoquv5yMIM2gWSWIRJhwJ8cUpafIHc+GesDlbF6Zbt+/KXkafJAQq2RklEN+WvZ/zFz113EPgWPjp16TwBoziq96MMekvWKY/vdhjol8VFtGH9F61Oy1Xwf6DJtPw==`
	)
	b, err := base64.StdEncoding.DecodeString(req_info)
	if err != nil {
		fmt.Println(b, err)
		return
	}
	gocrypto.SetAesKey(strings.ToLower(gocrypto.Md5(paykey)))
	plaintext, err := gocrypto.AesECBDecrypt(b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(plaintext))
}

三、Golang 第三方库

支持常见加解密

github.com/nanjishidu/gomini/gocrypto