Example 1: encrypt decrypt php
function encrypt_decrypt($string, $action = 'encrypt')
{
$encrypt_method = "AES-256-CBC";
$secret_key = 'AA74CDCC2BBRT935136HH7B63C27';
$secret_iv = '5fgf5HJ5g27';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} else if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');
Example 2: openssl_encrypt
$textToEncrypt = "My super secret information.";
$encryptionMethod = "AES-256-CBC";
$secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);
echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";
Example 3: password encryption php
<?php
echo password_hash('rasmuslerdorf', PASSWORD_DEFAULT);
?>
Example 4: php two way encrypt decrypt
function encryptor($action, $string) {
$output = FALSE;
$encrypt_method = "AES-256-CBC";
$secret_key = 'SecretKeyWord';
$secret_iv = 'SecretIV@123GKrQp';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} elseif ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
function encrypt($data) {
return urlencode(self::encryptor('encrypt', self::sanitize($data)));
}
function decrypt($data) {
return self::encryptor('decrypt', urldecode(self::sanitize($data)));
}
?>