1function encrypt_decrypt($string, $action = 'encrypt')
2{
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
5 $secret_iv = '5fgf5HJ5g27'; // user define secret key
6 $key = hash('sha256', $secret_key);
7 $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
8 if ($action == 'encrypt') {
9 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
10 $output = base64_encode($output);
11 } else if ($action == 'decrypt') {
12 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
13 }
14 return $output;
15}
16
17echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
18echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');
19
1$textToEncrypt = "My super secret information.";
2$encryptionMethod = "AES-256-CBC"; // AES is used by the U.S. gov't to encrypt top secret documents.
3$secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
4
5//To encrypt
6$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);
7
8//To Decrypt
9$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);
10
11//Result
12echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";
1function rw_hash($string, $encrypt = true)
2 {
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = "AA74CDCC2BBRT935136HH7B63C27"; // user define private key
5 $secret_iv = "RwS3cr3t"; // user define secret key
6 $key = hash("sha256", $secret_key);
7 $iv = substr(hash("sha256", $secret_iv), 0, 16); // sha256 is hash_hmac_algo
8 if ($encrypt) {
9 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
10 $output = base64_encode($output);
11 } else {
12 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
13 }
14 return $output;
15 }