protected static function encrypt(string $string): string
{
return openssl_encrypt($string, self::ENCRYPTION_METHOD, self::KEY);
}
protected static function decrypt(string $string): string
{
return openssl_decrypt($string, self::ENCRYPTION_METHOD, self::KEY);
}
Warning: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash, 0, $iv);
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash, 0, $iv);
<?php
$phrase = 'secret message';
$key = 'KEY';
$encrypt = encrypt($key, $phrase);
$phrase = decrypt($key, $encrypt);
echo $encrypt, ' = ', $phrase;
function encrypt($key, $text) {
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $key, '12345678');
$encrypted = mcrypt_generic($cipher,$text);
mcrypt_generic_deinit($cipher);
$encrypted = bin2hex($encrypted);
return $encrypted;
}
function decrypt($key, $encrypted) {
$encrypted = hex2bin($encrypted);
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $key, '12345678');
$decrypted = mdecrypt_generic($cipher,$encrypted);
mcrypt_generic_deinit($cipher);
return $decrypted;
}