M Meigionel TS Member Licensed User Jan 8, 2019 #1 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: Code: 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?
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: Code: 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?
DonManfred Expert Licensed User Longtime User Jan 8, 2019 #2 Check http://php.net/manual/de/function.openssl-encrypt.php though i don´t know if it return the same as Messagedigest. You need to check. Upvote 0
Check http://php.net/manual/de/function.openssl-encrypt.php though i don´t know if it return the same as Messagedigest. You need to check.
M Meigionel TS Member Licensed User Jan 8, 2019 #3 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
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); ?>