相同的字符串,从VB.net和PHP获得不同的SHA1哈希值 [英] Same string, different SHA1 hash values obtained from VB.net and PHP
问题描述
我有一些问题字符串的SHA1哈希值。我试图从写在VB.net到PHP编写的服务器的客户端发送一个文件。我的问题是,当我通过相同的字符串来VB.net和PHP,由VB.net计算的SHA1值是由PHP计算出的值完全不同。
例如,我要带code为Base64字符串,然后计算Base64编码字符串的SHA1哈希。当计算机从vb.net版和PHP版本执行这个任务,我得到两个不同的SHA1值,即使显然为Base64 EN codeD字符串是相同的:
VB.net: 2E97A53B09C482A831540B532845BCAC79BFACCF
PHP: 350A2080264E2724D4BCBC521C35264D264A1DAF
我肯定失去了一些东西,你可以点我在正确的方向,告诉我我在做什么错在这里?
非常感谢你。
下面是VB.net code:
昏暗cInput作为字符串
昏暗cBase64作为字符串
昏暗objSHA1作为新SHA1CryptoServiceProvider()
昏暗abBytesToHash()为字节
昏暗cHash作为字符串
cInput =快速的棕色狐狸跳过懒狗
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 =替换(cHash, - ,)MSGBOX(BASE64:+ cBase64 + vbNewLine +SHA1:+ cHash)结果是:
BASE64:dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw ==
SHA1:2E97A53B09C482A831540B532845BCAC79BFACCF
和这里的PHP code:
$ cInput =快速的棕色狐狸跳过懒狗
$ cBase64 = base64_en code($ cInput);回波(BASE64:$ cBase64&所述峰; br />中SHA1:。strtoupper(SHA1($ cBase64)));//结果是:
// BASE64:dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw ==
// SHA1:350A2080264E2724D4BCBC521C35264D264A1DAF
那么,问题是,你在PHP .NET,也是唯一一个散列双散列。下面是你在.NET做翻译成PHP:
$ cInput =快速的棕色狐狸跳过懒狗
$ cBase64 = base64_en code($ cInput);$ SHA = SHA1($ cBase64,真正的); //真正的参数返回原始字节而不是十六进制
$ chash = SHA1($ SHA);
所以你是双散列它。要修复它,你只需要你的算法更改为:
cInput =快速的棕色狐狸跳过懒狗
cBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(cInput))abBytesToHash = System.Text.Encoding.ASCII.GetBytes(cBase64)cHash = BitConverter.ToString(objSHA1.ComputeHash(abBytesToHash))
cHash =替换(cHash, - ,)
请注意,我所做的只是删除 abBytesToHash = objSHA1.ComputeHash(abBytesToHash)
行...
另外,你可以改变PHP来做到这一点:
$ cInput =快速的棕色狐狸跳过懒狗
$ cBase64 = base64_en code($ cInput);回声BASE64。 $ cBase64。 < BR />中;
回声SHA1。 strtoupper(SHA1(SHA1($ cBase64,真)));
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.
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: 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?
Thank you very much
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
And here's the PHP Code:
$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
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, "-", "")
Note that all I did was remove the abBytesToHash = objSHA1.ComputeHash(abBytesToHash)
line...
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屋!