Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Encryption used in C# to PHP
    primarykey
    data
    text
    <p>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#</p> <pre><code>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 &gt; 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); } </code></pre> <p>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 <a href="https://stackoverflow.com/questions/7249998/why-isnt-my-php-sha256-hash-equivalent-to-c-sharp-sha256managed-hash">this link</a></p> <p>.I have found that there is no need to produce byte arrays in PHP as strings are byte arrays in php.</p> <pre><code>$originalSalt = 'pradeep14520'; $originalSalt .= '9A111B734E4F10469668'; hash("sha256",iconv(mb_detect_encoding($originalSalt, mb_detect_order(), false), "UTF-8", $originalSalt),true); </code></pre> <p>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#</p> <pre><code>for PHP :afc80d1a6cae81e8f868b054dafbb74bdd86399d771fceef574e4b373506b704 for C# :683CA8E00E8CE41A079B7C86CE4960AC2376CD85F2AEAB2E4DF99FFA7F7FA4F3 </code></pre> <p>Plz help. Thanks in advance.</p> <p>Based on the suggestion provided by Daniel Hilgarth i have modified the code and made it work.</p> <pre><code> $password = 'pradeep14520'; $originalSalt = hex2bin('9A111B734E4F10469668'); echo strtoupper(hash("sha256",mb_convert_encoding($password,"UTF-16LE").$originalSalt,false)); </code></pre> <p>I hope it will help someone..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload