douao3636 2014-08-21 15:04
浏览 73
已采纳

PHP MD5与VB.NET MD5不匹配

I am trying to get the same MD5 hash in PHP and VB.NET but no matter what text encoding I use I cannot get the hashes to match. What am I doing wrong? Below is my code in each language:

PHP Code:

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 Code:

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条回答 默认 最新

  • dongshuzhuo5659 2014-08-21 21:59
    关注

    String concatenation is . in PHP not + as in VB.Net. Thus, '1C9BRPS3TN85I2ULE5FP' + '20.00' won't result in '1C9BRPS3TN85I2ULE5FP20.00'in PHP.

    Edit: VB.net's Real string concatenation is & but since the term visual basic is used, + sign can do the same as long as mathematical interpretation is meaningless. So to be on the safe side, use &.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型