Android Question Mismatch in MD5 result when comparing with PHP

Meigionel TS

Member
Licensed User
I was experimenting with encryption and hashing when I came across this weird problem.

So I was trying to generate a 128bit key for an AES function, so I did the following to generate the hash and check the hash in base64 format:

B4X:
Dim Secret as String
Dim j() as byte
Dim md as MessageDigest
Dim su as StringUtils

Secret = "Hello B4J"

j = md.GetMessageDigest(Secret.GetBytes("UTF8"),"MD5")

Log(su.EncodeBase64(j))

It returned this: MaC/vRE/Qq0cCGy1p3MkqA==

However, the same thing I did in PHP is giving me a different hash result with this code:

PHP:
<?php

    $p = "Hello B4J";
    $p = md5($p);
    echo(base64_encode($p));

?>

And it is giving me problem with encryption and decryption, something encrypted in B4X is not being decrypted in PHP because the keys are different.

Why is this happening, and how to make both the outputs same?
 

Meigionel TS

Member
Licensed User
Ok, I found the solution.

The hash key generated by B4X and PHP is different because, B4X message digest is always in raw binary data, however the md5() function in php gives out data in lowercase hexits.

That is why it is generating different hash functions.

The solution is to use hash() which also gives out raw data:

PHP:
<?php

 hash("md5","String",True);

?>
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…