我很想用md5散列这个.我试图移植到PHP的VB代码使用ComputeHash,它接收一个byte[]并对整个数组执行哈希操作。
Public Shared Function HashBytesMD5(ByVal strInput As String) As Guid
Dim oHasher As Cryptography.MD5 = Cryptography.MD5.Create()
Dim oEncoder As New System.Text.UTF8Encoding()
Dim csData() As Byte
csData = oEncoder.GetBytes(strInput)
csData = oHasher.ComputeHash(oEncoder.GetBytes(strInput))
Return New Guid(csData)
End Function现在,我有以下内容,它创建了一个ascii值数组。现在,我需要像md5一样使用VB.Net。它看上去并不像看起来那么简单。
$passHash = $this->ConvertToASCII('123456');
$passHash = md5(serialize($passHash));
/*
* Converts a string to ascii (byte) array
*/
function ConvertToASCII($password)
{
$byteArray = array();
for ($i=0; $i < strlen($password); $i++) {
array_push($byteArray,ord(substr($password,$i)));
}
return $byteArray;
}注意:第一个值是字符123456的acii值。
computeHash md5之前的字节数组
**index** **Value**
[0] 49
[1] 50
[2] 51
[3] 52
[4] 53
[5] 54从VB computeHash函数索引值返回的字节数组
[0] 225
[1] 10
[2] 220
[3] 57
[4] 73
[5] 186
[6] 89
[7] 171
[8] 190
[9] 86
[10] 224
[11] 87
[12] 242
[13] 15
[14] 136
[15] 62发布于 2011-04-04 22:55:34
我的VB.NET非常生疏,但MD5.ComputeHash()的输出似乎可以通过md5()运行输入,然后取每一对十六进制字符(字节)并转换为十进制来重新创建。
$passHash = md5('123456');
$strlen = strlen($passHash) ;
$hashedBytes = array() ;
$i = 0 ;
while ($i < $strlen) {
$pair = substr($passHash, $i, 2) ;
$hashedBytes[] = hexdec($pair) ;
$i = $i + 2 ;
}发布于 2011-04-04 23:17:23
通过魔法的力量,以下几点将发挥作用:
function get_VB_hash($text)
{
$hash = md5($text);
$hex = pack('H*', $hash); // Pack as a hex string
$int_arr = unpack('C*', $hex); // Unpack as unsigned chars
return $int_arr;
}或者作为一行:
unpack('C*', pack('H*', md5($text)) );
证明:
C:\>php -r "print_r( unpack('C*', pack('H*', md5('123456') )) );"
Array
(
[1] => 225
[2] => 10
[3] => 220
[4] => 57
[5] => 73
[6] => 186
[7] => 89
[8] => 171
[9] => 190
[10] => 86
[11] => 224
[12] => 87
[13] => 242
[14] => 15
[15] => 136
[16] => 62
)https://stackoverflow.com/questions/5544884
复制相似问题