VB代码有效:
Public Function Encrypt(ByVal Data As String) As Byte()
Dim md5Hasher As New MD5CryptoServiceProvider()
Dim hashedBytes As Byte()
Dim encoder As New UTF8Encoding()
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Data))
Return hashedBytes
End Function
可行的JAVA代码:
byte[] bytes = stringToConvert.getBytes("UTF-8");
MessageDigest m = MessageDigest.getInstance("MD5");
hashedBytes = m.digest(bytes);
我在PHP中尝试过哪些不起作用,我想我知道为什么。
我认为是因为:
Java中的字符存储为Unicode 16位序列。在PHP中,它们是单字节序列。 这是我试过的代码......
$UTFbString = UTF8_encode($bString);
$hashedBytes = md5($UTFbString, true);
好的,我发现如果我使用这种方法......
function ascii_to_dec($str)
{
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
$dec_array[] = ord($str{$i});
}
return $dec_array;
}
和这段代码......
$bStringArr = array( ascii_to_dec($bString));
I can get back an array that matches the byte array in JAVA.
So the next challenge is to convert that to bytes then md5 hash those bytes?
这样做的JAVA代码就像这样......
MessageDigest digester = MessageDigest.getInstance("MD5");
byte[] bytes = new byte[8192];
int byteCount;
while ((byteCount = in.read(bytes)) > 0) {
digester.update(bytes, 0, byteCount);
}
byte[] digest = digester.digest();
有关在PHP中实现类似内容的任何建议吗?
答案 0 :(得分:1)
尝试:
<?php
$hashedBytes = base64_encode(md5($bString, true))
答案 1 :(得分:1)
虽然我不确定为什么要将字节数组用作md5哈希,但这是我的解决方案:
<?php
$stringToConvert = "äöüß";
$md5 = md5(utf8_encode($stringToConvert), true);
for($i = 0; $i < strlen($md5); $i++) {
$c = ord($md5[$i]);
$b[] = $c > 127 ? $c-256 : $c;
}
print_r($b);
?>