0

I try to replicate this C# code in php to get the same output(I cannot change c# code only php).And here I'm stuck

 public static string HashData(string textToBeEncripted)
         {
        //Convert the string to a byte array
        Byte[] byteDataToHash =              System.Text.Encoding.Unicode.GetBytes(textToBeEncripted);

        //Compute the MD5 hash algorithm
        Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash);

        return System.Text.Encoding.Unicode.GetString(byteHashValue);
    }

The php code that I have made till now looks like this

$a = "test";
$a = mb_convert_encoding($a, "UTF-16LE");

$a = md5($a,true);

$a = unpack('C*', $a);
var_dump($a);
//with the output
array(16) { [1]=> int(200) [2]=> int(5) [3]=> int(158) [4]=> int(46) [5]=> int(199) [6]=> int(65) [7]=> int(159) [8]=> int(89) [9]=> int(14) [10]=> int(121) [11]=> int(215) [12]=> int(241) [13]=> int(183) [14]=> int(116) [15]=> int(191) [16]=> int(230) }

As you can see the output is the same as in the C# code But I'm stuck at the function System.Text.Encoding.Unicode.GetString().How to replicate this in php?Or there is a easier way to get the same output?(I cannot change the C# code sorry)
Edit: Based on Vasiliy Zverev answers since the php hash is different a little bit.I end up making aproximating the hash value of php with the C# hash

function validare_parola($parola,$dbHash){

$parola = mb_convert_encoding($parola, "UTF-16LE");
$parola = md5($parola, true);
$parola = mb_convert_encoding($parola, "UCS-2BE", "UCS-2LE");
$parola = bin2hex($parola);

$procent;
similar_text($dbHash,$parola,$procent);

if($procent>=90){
    return true;
}else{
    return false;
}

}

$parola = "testa";

$dbHash = "10095018710be2bcbbf9bba3f9d91ce8";
if(validare_parola($parola,$dbHash)){
echo 'PASSWORD CORRECT.You can log in.';
}else{
echo 'INCORRECT PASSWORD.Try again.';
}

As a side note don't use md5 for passwords use php password hashing api

Edit2: I ended up using Vasiliy Zverev solution.
Edit3: For the value "111111" there is different output in php...
Edit4: Vasily Zverev updated his solution and now is working as expected

1

2 Answers 2

2

The solution, updated:

$a = "SF0D9G9SGGF0gdsfg976590";
$a = mb_convert_encoding($a, "UTF-16LE");
$a = md5($a, true);
$res = '';
for($i=0; $i<16; $i+=2) {
    // System.Text.Encoding.Unicode.GetString(byteHashValue) replaces invalid characters to 0xfffd ('я')
    // while PHP to 0x003d ('?') or empty string. So replace them like C# does
    $a2 = mb_convert_encoding($a[$i].$a[$i+1], "UTF-16LE","UTF-16LE"); // check if this is invalid UTF-16 character
    if(strlen($a2)==0 || $a[$i]!=$a2[0]) {
        // replace invalid UTF-16 character with C# like
        $v = 0xfffd;
    }
    else {
        // prepare a word (UTF-16 character)
        $v = ord($a[$i+1])<<8 | ord($a[$i]);
    }
    // print each word without leading zeros as C# code does
    $res .= sprintf("%x",$v);
}
echo($res);
Sign up to request clarification or add additional context in comments.

3 Comments

What is input for that hash?
Try new version :) I search for invalid UTF-16 character one by one now.
Great job.Is finaly working.Thanks a lot for your help,like realy great job.
2

Removed this variant because it was wrong. See correct code above.

7 Comments

Hi it works for "test".But if I type $a="parolaeste" I get different output on the php side.
I think I know what's wrong: return System.Text.Encoding.Unicode.GetString(byteHashValue); Not all binary strings produce correct UTF-16 string. Some combinations of bits are invalid. While php bin2hex(md5()) doesn't do additional UTF conversion. I will prepare a fix today later.
Please try out my new version in another answer.
If I type $a="111111" I get different output in php there is a 0 in plus.
LOL, this is because hex += String.Format("{0:x2}", (uint) System.Convert.ToUInt32(tmp.ToString())) eats leading zeros.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.