-5

I have this function in .net but i need to use it in a php code. How can I do that? Thank you.

originalPassword=user+password;
public static string Encrypt(string originalPassword)
  {
string key = "7f9facc418f74439c5e9709832;0ab8a5:OCOdN5Wl,q8SLIQz8i|8agmu¬s13Q7ZXyno/yv.XSN1DsgKq9zi]XrE^gx8vPC^Av8=e/bF4pX1Oe hfqGb#JK~RONkS1wx5w=RE0$DxZSu7evPfshBw7p5Gb&suEkw=RE0DxZSu7e´vPfshBw7p+5GbsuEkw=H1fTWFXfsXo}z0fOd{KTt[IdDG2y6E=";

SHA512 sha512 = new SHA512CryptoServiceProvider();
byte[] inputBytes = (new UnicodeEncoding()).GetBytes(originalPassword + key);
        byte[] hash = sha512.ComputeHash(inputBytes);
        return Convert.ToBase64String(hash);
}
Blidge
  • 118
  • 3
  • 2
    _"How can I do that?"_ - you start by figuring out what the individual pieces of the given code do, and then you go _research_ how to achieve the same in PHP, step by step. – CBroe Oct 12 '17 at 17:23
  • I did a lot of this 18 years ago when I came from Tango into PHP... had a lot of tango functions that I wanted equivs in PHP, so I had to research each one, figure out the specifics, make up the PHP functions. So, its a lengthy process... but you learn a whole lot while doing it! – IncredibleHat Oct 12 '17 at 17:29
  • Use PHP hash? http://php.net/manual/en/function.hash.php – Dave S Oct 12 '17 at 17:36
  • Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Oct 12 '17 at 17:53
  • Already try with password_hash and other functions. The problem is that I am migrating a web page in .net to php. And there are already passwords stored in the database that do not match when I encrypt them with php. – Blidge Oct 12 '17 at 17:59
  • Then when the user logs in you should recheck them and save the new hash. – Jay Blanchard Oct 12 '17 at 18:01

1 Answers1

0

This should do the trick:

<?php

function hash_data($data)
{
    $data = mb_convert_encoding($data, 'UTF-16LE', 'UTF-8');
    $hash = hash('sha512', $data, true);
    return base64_encode($hash);
}

$user = 'admin';
$password = 'secret';
$key = "7f9facc418f74439c5e9709832;0ab8a5:OCOdN5Wl,q8SLIQz8i|8agmu¬s13Q7ZXyno/yv.XSN1DsgKq9zi]XrE^gx8vPC^Av8=e/bF4pX1Oe hfqGb#JK~RONkS1wx5w=RE0$" . "DxZSu7evPfshBw7p5Gb&suEkw=RE0DxZSu7e´vPfshBw7p+5GbsuEkw=H1fTWFXfsXo}z0fOd{KTt[IdDG2y6E=";
$data = $user . $password . $key;

// 6xecArT38JVtGKH2yQs/T6btOUF41vW5ptaPjgrd8hTaZZKnbJed5551LuYV7vR/Dr3Jb873JMvX0je+8XUpxw==
echo hash_data($data);
odan
  • 4,757
  • 5
  • 20
  • 49