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..