get same response in php as Rfc2898DeriveBytes gives in c#
get same response in php as Rfc2898DeriveBytes gives in c#
This is my C# code but I want same encrypted string in PHP. Can you please help me in any way.
var token ="MqsXexqpYRUNAHR_lHkPRic1g1BYhH6bFNVPagEkuaL8Mf80l_tOirhThQYIbfWYErgu4bDwl-7brVhXTWnJNQ2";
var id = "bob@company.com";
var ssokey = "7MpszrQpO95p7H";
string idAndKey = id + ssokey;
var salt = HttpServerUtility.UrlTokenDecode(token);
var pbkdf2 = new Rfc2898DeriveBytes(idAndKey, salt) IterationCount = 1000;
var key = HttpServerUtility.UrlTokenEncode(pbkdf2.GetBytes(24));
//key = aE1k9-djZ66WbUATqdHbWyJzskMI5ABS0;
My PHP code is:
function base64url_encode($data)
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
function base64url_decode($data)
return base64_decode(str_pad(strtr($data, '-_', '+/'),strlen($data) % 4, '=', STR_PAD_RIGHT));
$token = "MqsXexqpYRUNAHR_lHkPRic1g1BYhH6bFNVPagEkuaL8Mf80l_tOirhThQYIbfWYErgu4bDwl-7brVhXTWnJNQ2";
$id = "bob@company.com";
$ssokey = "7MpszrQpO95p7H";
$idAndKey = $id.$ssokey;
$salt = base64_decode(base64url_decode($token));
$pbkdf2 = openssl_pbkdf2($idAndKey,$salt,20,1000);
$key = base64url_encode(base64_encode($pbkdf2));
//should produce key = aE1k9-djZ66WbUATqdHbWyJzskMI5ABS0
echo "key = ".$key; exit;
It should give aE1k9-djZ66WbUATqdHbWyJzskMI5ABS0
but is produced differently.
aE1k9-djZ66WbUATqdHbWyJzskMI5ABS0
Any help is appreciated.
If you have made any attempt, please edit your original post and add it there, not in the comments.
– AmmoPT
Sep 13 '18 at 10:31
1 Answer
1
$idandKey = "bob@company.com" . "7MpszrQpO95p7H";
$salt = convertFromUrlTokenFormat("MqsXexqpYRUNAHR_lHkPRic1g1BYhH6bFNVPagEkuaL8Mf80l_tOirhThQYIbfWYErgu4bDwl-7brVhXTWnJNQ2");
$hash = hash_pbkdf2("sha1", $idandKey, base64_decode($salt), 1000, 24, true);
$key = convertToUrlTokenFormat(base64_encode($hash));
// key = “aE1k9-djZ66WbUATqdHbWyJzskMI5ABS0”;
function convertToUrlTokenFormat($val)
$padding = substr_count($val, '=');
$val = str_replace('=', '', $val);
$val .= $padding;
$val = str_replace('+', '-', str_replace('/', '_', $val));
return $val;
function convertFromUrlTokenFormat($val)
$val = str_replace('-', '+', str_replace('_', '/', $val));
$lastCharacter = substr($val, -1);
$val = substr($val, 0, -1);
switch($lastCharacter)
case 1:
$val = $val . "=";
break;
case 2:
$val = $val . "==";
break;
return $val;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you made any attempt?
– AmmoPT
Sep 13 '18 at 10:26