John Davidson

encryption - AES encrypt PHP 5.6 to PHP 7.3

0 comments
Message:


I would like to transform the code below code PHP 5.6 to PHP 7.3, We are more grateful to that person who provides us some suggestions on learning encryption in PHP on a more detailed level so we can try to convert that code itself.


function generateRandomString($length = 10)
{

$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for($i = 0; $i < $length; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}

return $randomString;
}

function strToHex($string)
{

$hex = '';
for($i = 0; $i < strlen($string); $i++)
{
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0' . $hexCode, -2);
}

return strToUpper($hex);
}

function AESencrypt($decrypted, $password = 'password', $salt = 'salt_key')
{

$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$padding = $block - (strlen($decrypted) % $block);
$decrypted .= str_repeat(chr($padding), $padding);
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $password, $decrypted, MCRYPT_MODE_CBC, $password));
$encrypted = hash('SHA256', $encrypted . '|' . $salt, true);

return strToHex($encrypted);
}

$agentid = "EU01EU22INB000";
$mercode = "BPP";
$username = "BPP_01";
$userpass = "BPP_01";
$storecode = "BPP_01";
$ipaddr = "103.235.104.168";
$billerid = '00000NATTQ';
$bbpstel = 'QH01WD3737';
$bbpstel1 = $bbpstel;
$stringParts = str_split($bbpstel1);
sort($stringParts);
$sortval = implode('', $stringParts);
$sort = $sortval;
$Billfetch = "BillPay" . generateRandomString();

$hashkey = AESencrypt($Billfetch . "|" . $mercode . "|" . $agentid . "|" . $billerid . "|" . $sort, "password", "salt_key");
echo $Billfetch."----".$hashkey."<br/>";
exit;


My Answer:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

// Example usage
echo generateRandomString(10); // Output: "3aBcDeFgHi"

Rate this post

3 of 5 based on 9736 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us