【发布时间】:2011-04-04 22:05:30
【问题描述】:
我在尝试用 PHP 对它进行 md5 哈希处理时遇到了麻烦……我试图移植到 PHP 的 VB 代码使用 ComputeHash,它接收一个字节 [] 并对整个数组执行哈希处理。
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 值数组。现在我需要像 VB.Net 那样对它进行 md5 处理。它似乎并不像看起来那么简单。
$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
【问题讨论】:
-
乍一看,我不确定这是否可以通过 md5'ing 字节序列化数组来完成。旁注:您使用
substr()的方式采用从$i开始的所有字符串,而我认为您只需要那个单个字符:substr($password, $i, 1) -
我从 php 手册中的注释中得到了 php 函数。我做了一个 print_r,它似乎工作。
-
nvm 你是对的 :) 不错。
-
是的,因为
ord()只关心第一个给定的字符 :) 我想我明白了,发布答案是因为里面有代码
标签: php vb.net encryption md5