在VB.NET中使用HmacSHA256加密字符串的正确方法

发布于 2024-07-18 09:18:16 字数 657 浏览 13 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-07-25 09:18:16

(阅读我的评论)
如果 My.Settings.PortalHASH 是 Base64 编码密钥,您需要执行以下操作:

Dim Key() As Byte = Convert.FromBase64String(My.Settings.PortalHASH)

(Read my comments)
If My.Settings.PortalHASH is the Base64 encoded key you need to do this:

Dim Key() As Byte = Convert.FromBase64String(My.Settings.PortalHASH)
今天小雨转甜 2024-07-25 09:18:16

我无法评论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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文