PHP MD5与VB.NET MD5不匹配

时间:2014-08-21 15:04:15

标签: php vb.net hash md5

我试图在PHP和VB.NET中获得相同的MD5哈希,但无论我使用什么文本编码,我都无法获得匹配的哈希值。我究竟做错了什么?以下是我在每种语言中的代码:

PHP代码:

echo '<b>$20.00 (UTF-32)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32'));  
echo '<b>$20.00 (UTF-32LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32LE'));
echo '<b>$20.00 (UTF-32BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-32BE'));
echo '<b>$20.00 (UTF-8)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-8'));
echo '<b>$20.00 (UTF-7)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-7'));
echo '<b>$20.00 (UTF-16)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16'));
echo '<b>$20.00 (UTF-16LE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16LE'));
echo '<b>$20.00 (UTF-16BE)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'UTF-16BE'));
echo '<b>$20.00 (ASCII)</b> = ' . md5(mb_convert_encoding('1C9BRPS3TN85I2ULE5FP' + '20.00', 'ASCII'));

VB.NET代码:

Try
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()
' Convert the input string to a byte array and compute the hash.
Dim strInput As String = "1C9BRPS3TN85I2ULE5FP" & "20.00"
Dim data As Byte() = md5Hasher.ComputeHash(Encoding.UTF32.GetBytes(strInput))

' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()

' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
    'sBuilder.Append(data(i).ToString("x2"))
Next i

lblResponse.Text = "<b>$20.00 (UTF-32)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.ASCII.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (ASCII)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (Unicode)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF7.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF7)</b> = " & sBuilder.ToString() & "<br>"

' Try a different encoding
data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(strInput))
' Loop through each byte of the hashed data 
' and format each one as a hexadecimal string.
sBuilder.Length = 0
For i = 0 To data.Length - 1
    sBuilder.AppendFormat("{0:x2}", data(i))
Next i
lblResponse.Text &= "<b>$20.00 (UTF8)</b> = " & sBuilder.ToString() & "<br>"

Catch ex As Exception
    lblErrorMessage.Text = ex.ToString()
End Try

1 个答案:

答案 0 :(得分:1)

PHP中的字符串连接是.,而不是像VB.Net中那样+。因此,'1C9BRPS3TN85I2ULE5FP' + '20.00'在PHP中不会导致'1C9BRPS3TN85I2ULE5FP20.00'

修改:VB.net的真正字符串连接为&,但由于使用了术语 visual basic 只要数学解释毫无意义,+符号就可以这样做。 因此,为了安全起见,请使用&