|
本帖最后由 myperworld 于 2022-2-10 17:36 编辑
最近在用vb.net摸索vsto,其中一段hamc256加密的功能,卡了好几天,先上能够通过服务器认证的结果,使用python写的:
- message = bytes("{"sap_params":{"para1":"20001593051","para2":"201"}}" + "\n1644455141" ,'utf-8')
- secret = "123456".encode('utf-8')
- hash = base64.b64encode(hmac.new(secret, message, digestmod = sha256).digest()).decode('ascii')
复制代码
输出:
message = b'{"sap_params":{"para1":"20001593051","para2":"201"}}\n1644455141'
hash = "wRt+XIZTxWUwVYEWRekkLcNVGUhW+iLsk/nZ5/blcLk="
但是在visual studio中使用vb.net同样的参数加密:
- Dim message as String = "{""sap_params"":{""para1"":""20001593051"",""para2"":""201""}}\n1644455141"
- ' 生成hash
- Dim secret As String = "123456"
- Dim myEncoder As New System.Text.UTF32Encoding
- Dim Key() As Byte = myEncoder.GetBytes(secret)
- Dim XML() As Byte = myEncoder.GetBytes(message)
- Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
- Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
- hash = Convert.ToBase64String(HashCode)
复制代码
输出:
message = "{""sap_params"":{""para1"":""20001593051"",""para2"":""201""}}\n1644455141"
hash = "+PW4kw3IsB1v/iGQfG47AqNtbBdKpmZ0bf9JDmwg3gg="
两者hash结果不一样,是不时vb.net把两个"号转义为一个",message中多了"的关系,但又怎么去除呢?不知道哪位老师解决过,忘赐教,不胜感谢。
如果实在解决不了,那么同一个vsto项目中,可以同时使用vb.net和python语言吗?
|
|