VB Code that works:
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 code that works:
byte[] bytes = stringToConvert.getBytes("UTF-8");
MessageDigest m = MessageDigest.getInstance("MD5");
hashedBytes = m.digest(bytes);
What I have tried in PHP that does not work and I think I know why.
I think it is because of this:
Characters in Java are stored as Unicode 16-bit sequences. In PHP they’re single byte sequences.
This is the code I have tried…
$UTFbString = UTF8_encode($bString);
$hashedBytes = md5($UTFbString, true);
Ok, I have found that if I use this method…
function ascii_to_dec($str)
{
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
$dec_array[] = ord($str{$i});
}
return $dec_array;
}
and this code…
$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?
The JAVA code that does that looks like this…
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();
Any suggestions on implementing something something like this in PHP?
Although I am not sure why one would want to use a byte array as md5 hash, here is my solution: