从 VB.net 和 PHP 获得的相同字符串,不同的 SHA1 哈希值 [英] Same string, different SHA1 hash values obtained from VB.net and PHP

查看:23
本文介绍了从 VB.net 和 PHP 获得的相同字符串,不同的 SHA1 哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对字符串的 SHA1 哈希值有一些问题.我正在尝试将文件从用 VB.net 编写的客户端发送到用 PHP 编写的服务器.我的问题是,当我将相同的字符串传递给VB.net和PHP时,VB.net计算的SHA1值与PHP计算的值完全不同.

I have some problems with the SHA1 hash value of a string. I'm trying to send a file from a client written in VB.net to a server written in PHP. My problem is that when I pass the same string to VB.net and PHP, the SHA1 value calculated by VB.net is completely different from the value calculated by PHP.

例如我想用Base64编码一个字符串,然后计算Base64字符串的SHA1 Hash.当计算机从 vb.net 版本和 PHP 版本执行此任务时,我得到两个不同的 SHA1 值,即使显然 Base64 编码的字符串是相同的:

For example, I want to encode a string in Base64 and then calculate the SHA1 Hash of the Base64 string. When the computer executes this task from the vb.net version and from the PHP version I get two different SHA1 values, even if apparently the Base64 encoded string is the same:

VB.net: 2E97A53B09C482A831540B532845BCAC79BFACCFPHP: 350A2080264E2724D4BCBC521C35264D264A1DAF

VB.net: 2E97A53B09C482A831540B532845BCAC79BFACCF PHP: 350A2080264E2724D4BCBC521C35264D264A1DAF

我肯定错过了一些东西,你能指出我正确的方向并告诉我我在这里做错了什么吗?

I'm surely missing something, could you point me in the right direction and tell me what I'm doing wrong here?

非常感谢

这是 VB.net 代码:

Here's the VB.net Code:

Dim cInput As String
Dim cBase64 As String
Dim objSHA1 As New SHA1CryptoServiceProvider()
Dim abBytesToHash() As Byte
Dim cHash As String


cInput = "the quick brown fox jumps over the lazy dog"
cBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(cInput))

abBytesToHash = System.Text.Encoding.ASCII.GetBytes(cBase64)

abBytesToHash = objSHA1.ComputeHash(abBytesToHash)
cHash = BitConverter.ToString(objSHA1.ComputeHash(abBytesToHash))
cHash = Replace(cHash, "-", "")

MsgBox("BASE64: " + cBase64 + vbNewLine + "SHA1: " + cHash)

' Result is:
' BASE64: dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
' SHA1: 2E97A53B09C482A831540B532845BCAC79BFACCF

这是 PHP 代码:

$cInput = "the quick brown fox jumps over the lazy dog";
$cBase64 = base64_encode($cInput);

echo("BASE64: " . $cBase64 . "<br />" . "SHA1: " . strtoupper(sha1($cBase64)));

// Result is:
// BASE64: dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
// SHA1: 350A2080264E2724D4BCBC521C35264D264A1DAF

推荐答案

嗯,问题是您在 .NET 中使用双散列,而在 PHP 中仅使用单散列.这是您在 .NET 中所做的翻译成 PHP 的操作:

Well, the problem is that you're double hashing in .NET and only single hashing in PHP. Here's what you're doing in .NET translated into PHP:

$cInput = "the quick brown fox jumps over the lazy dog";
$cBase64 = base64_encode($cInput);

$sha = sha1($cBase64, true); // The true param returns the raw bytes instead of hex
$chash = sha1($sha);

所以你是双重哈希它.要修复它,您只需将算法更改为:

So you're double hashing it. To fix it, you just need to change your algorithm to:

cInput = "the quick brown fox jumps over the lazy dog"
cBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(cInput))

abBytesToHash = System.Text.Encoding.ASCII.GetBytes(cBase64)

cHash = BitConverter.ToString(objSHA1.ComputeHash(abBytesToHash))
cHash = Replace(cHash, "-", "")

请注意,我所做的只是删除 abBytesToHash = objSHA1.ComputeHash(abBytesToHash) 行...

Note that all I did was remove the abBytesToHash = objSHA1.ComputeHash(abBytesToHash) line...

或者,您可以更改 PHP 来执行此操作:

Alternatively, you could change the PHP to do this:

$cInput = "the quick brown fox jumps over the lazy dog";
$cBase64 = base64_encode($cInput);

echo "BASE64: " . $cBase64 . "<br />";
echo "SHA1: " . strtoupper(sha1(sha1($cBase64, true)));

这篇关于从 VB.net 和 PHP 获得的相同字符串,不同的 SHA1 哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆