Encrypt decrypt in php with salt

encrypt decrypt in php with salt

OpenSSL functions to use encrypt decrypt in php with salt below code

Encrypt Method

$privateKey 	= 'AA74CDCC2BBRT935136HH7B63C27'; // user define key
$secretKey      = '5fgf5HJ5g27'; // user define secret key
$encryptMethod  = "AES-256-CBC";
$string 	= 'IB12345'; // user define value

$key     = hash('sha256', $privateKey);
$ivalue  = substr(hash('sha256', $secretKey), 0, 16); // sha256 is hash_hmac_algo
$result      = openssl_encrypt($string, $encryptMethod, $key, 0, $ivalue);
echo $output = base64_encode($result);  // output is a encripted value

Decrypt Method

$privateKey 	= 'AA74CDCC2BBRT935136HH7B63C27'; // user define key
$secretKey 	= '5fgf5HJ5g27'; // user define secret key
$encryptMethod  = "AES-256-CBC";
$stringEncrypt  = '3423432423'; // user encrypt value
$key    = hash('sha256', $privateKey);
$ivalue = substr(hash('sha256', $secretKey), 0, 16); // sha256 is hash_hmac_algo
echo $output = openssl_decrypt(base64_decode($stringEncrypt), $encryptMethod, $key, 0, $ivalue);
// output is a decripted value

Database connection in PHP

Reference : openssl_encrypt

Back To Top