Encryption and decryption in PHP

Encryption and decryption in PHP

Encryption and decryption in PHP

This is function is tested for high-security level coding in a very long time. Our we share our encrydecry function for helping your developers to create a 100% secure coding environment.

function encrydecry($string, $action = 'e')
{
    // you may change these values to your own
    $secret_key = '0123456789';
    // you may change these values to your own
    $secret_iv  = 'abcdefghij';
    
    $output         = false;
    $encrypt_method = "AES-256-CBC";
    $key            = hash('sha256', $secret_key);
    $iv             = substr(hash('sha256', $secret_iv), 0, 16);
    
    if ($action == 'e') {
        $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
    } else if ($action == 'd') {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
}

For encryption some value

<?php
// for encryption 
encrydecry("value",'e')
?>

For decryption some encrypted value

<?php
// for decryption encrypted value
encrydecry("encryptedvalue",'d')
?>

Leave a Reply