Encrypt & Decrypt data using AES algorithm

ØEncryption:
function encrypt($action, $string) {
    $output = false;
 $encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';
 // hash
    $key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
    $output = base64_encode($output);
   return $output;
}
$plain_txt = "This is my plain text";
echo "Plain Text =" .$plain_txt;

$encrypted_txt = encrypt('encrypt', $plain_txt);
echo "Encrypted Text =". $encrypted_txt;

ØDecryption:
function decrypt($action, $string) {
    $output = false;
$encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';
 // hash
    $key = hash('sha256', $secret_key);
   // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    return $output;
}
$plain_txt = "This is my plain text";
echo "Plain Text =" .$plain_txt;

$decrypted_txt = decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = $decrypted_txt\n";

Comments

Popular posts from this blog

Application ld json data in shopify

on_sent_ok not working in Contact Form 7 version 5.0.2

Make carousel slider in shopify