在VB.NET中使用HmacSHA256加密字符串的正确方法
我需要为 XML 字符串创建一个键控哈希以发送给第三方。 这是我正在使用的代码,但它生成的哈希值与第 3 方发送给我的示例不同。 我已经完成了我能找到的所有教程,并一遍又一遍地重新阅读 MSDN。 我究竟做错了什么? 或者我应该怀疑另一端有问题吗?
Public Shared Function HashString(ByVal StringToHash As String) As String
Dim myEncoder As New System.Text.UTF32Encoding
Dim Key() As Byte = myEncoder.GetBytes(My.Settings.PortalHASH)
Dim XML() As Byte = myEncoder.GetBytes(StringToHash)
Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
Return Convert.ToBase64String(HashCode)
End Function
它需要进行 base-64 编码,这就是为什么我有最后一行。
谢谢
I need to create a keyed hash for a string of XML to send to a 3rd party. This is the code I am using but it is producing a different hash than the example that the 3rd party has sent me. I have been through all the tutorials I can find and re-read MSDN again and again. What am I doing wrong? Or should I suspect a problem at the other end?
Public Shared Function HashString(ByVal StringToHash As String) As String
Dim myEncoder As New System.Text.UTF32Encoding
Dim Key() As Byte = myEncoder.GetBytes(My.Settings.PortalHASH)
Dim XML() As Byte = myEncoder.GetBytes(StringToHash)
Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
Return Convert.ToBase64String(HashCode)
End Function
It needs to be base-64 encoded, which is why I have the last line.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(阅读我的评论)
如果 My.Settings.PortalHASH 是 Base64 编码密钥,您需要执行以下操作:
(Read my comments)
If My.Settings.PortalHASH is the Base64 encoded key you need to do this:
我无法评论VB的正确性,但从命名的明显性来看,我可以说它看起来是正确的。
所以这里没有足够的信息来说明哪里出了问题; 你可能已经说出了你所知道的一切。 因此,为你验证这一点的人将是你的第三方。 他们能够说出哪里出了问题。
PS:奇怪,对方没有发送nonce作为挑战?
I can't comment on the correctness of the VB, but from the obviousness of the naming, I can say it looks correct.
So there's not enough information here to say whats wrong; and you've likely said everything you know. Therefore, the people to verify this for you would be your third party. They'd be able to say what was wrong.
PS: strange that there is no nonce sent by the other party as a challenge?