Encrypt String Using PHP

Posted Under: PHP, Snippets, Tutorials on Aug 17, 2018
Encrypt String Using PHP
PHP snippet to Encrypt string using AES-256 encryption algorithm with 32 Bytes key. In order to use encryption php_mcrypt extension need to be installed and activated.
$encryptionKey="EncryptionKeyMustBExactly32Bytes"; // 32 bytes Key
$strinToEncrypt="My Very special secret message";
$encryptedBytes=mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $encryptionKey, $strinToEncrypt, MCRYPT_MODE_CBC, $initializationVector);

// This step is optional base64 encoding convert 
// binary in to human readable characters.


// If you want to convert binary Output to hex

$hexOutput= bin2hex($encryptedBytes);

Sample Output

base64: krzY/3Gh7xd1U+8zfOGyTweF+QPNTYtOiCz50kcL4SM=
Hex: 92bcd8ff71a1ef177553ef337ce1b24f0785f903cd4d8b4e882cf9d2470be123
no image
© Tutorial Jinni