-1

I have been trying to convert a C# encryption technique to PHP. The C# code is as given below. I have no experience in C#. I had to take over project in C#

private void button1_Click(object sender, EventArgs e)
{
    //byte[] response = ComputeHash("pradeep14520", BitConverter.GetBytes("0x9A111B734E4F10469668"));
    //byte[] response = ComputeHash("pradeep14520", dictionary.ElementAt(0).Value.Salt);
    byte[] response =ComputeHash("pradeep14520", StringToByteArray("9A111B734E4F10469668"));
    string hex = BitConverter.ToString(response).Replace("-", string.Empty);
    string base64 = Convert.ToBase64String(response); // 683CA8E00E8CE41A079B7C86CE4960AC2376CD85F2AEAB2E4DF99FFA7F7FA4F3
}

 private byte[] ComputeHash(string password, byte[] storedSalt)
    {
        byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
        if ((int)storedSalt.Length > 0)
        {
            byte[] passwordAndSalt = new byte[(int)passwordBytes.Length + (int)storedSalt.Length];
            passwordBytes.CopyTo(passwordAndSalt, 0);
            storedSalt.CopyTo(passwordAndSalt, (int)passwordBytes.Length);
            passwordBytes = passwordAndSalt;
        }

        return new SHA256Managed().ComputeHash(passwordBytes);
    }

I have commented the sample response in the button click function. I tried to implement the same functionality using PHP. with the below code after referring this link

.I have found that there is no need to produce byte arrays in PHP as strings are byte arrays in php.

$originalSalt = 'pradeep14520';
    $originalSalt .= '9A111B734E4F10469668';
    hash("sha256",iconv(mb_detect_encoding($originalSalt, mb_detect_order(), false), "UTF-8", $originalSalt),true);

I have passed the last argument to the hash function as false inorder to get raw output. But i am getting different Output for PHP and C#

for PHP :afc80d1a6cae81e8f868b054dafbb74bdd86399d771fceef574e4b373506b704
for C#  :683CA8E00E8CE41A079B7C86CE4960AC2376CD85F2AEAB2E4DF99FFA7F7FA4F3

Plz help. Thanks in advance.

Based on the suggestion provided by Daniel Hilgarth i have modified the code and made it work.

   $password = 'pradeep14520';
$originalSalt = hex2bin('9A111B734E4F10469668');
echo    strtoupper(hash("sha256",mb_convert_encoding($password,"UTF-16LE").$originalSalt,false));

I hope it will help someone..

Community
  • 1
  • 1
shanavascet
  • 589
  • 1
  • 4
  • 18

1 Answers1

1

The problem here is that the C# code uses Encoding.Unicode. This is UTF-16, i.e. it returns two bytes per character, even if the character is representable in ASCII.

You need to use an equivalent of this in PHP or change your C# code to use either Encoding.UTF8 or Encoding.ASCII, depending on what PHP strings really are.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • In the link that i have mentioned they made it as correct answer. And there is nothing like UTF encoding and all. I have tried without encoding and still it shows the same output. – shanavascet Apr 16 '13 at 12:22
  • @shanavascet: No. The linked question uses `Encoding.UTF8` just as I said. You are using `Encoding.Unicode`. – Daniel Hilgarth Apr 16 '13 at 12:24