You have a lot going on with your example, since mcrypt_*
is deprecated, this example uses openssl_*
functions instead. To skip IV creation (less secure but easier) use the following:
$plainText = 'Hello world';
$key = 'my-secret-encrpytion-key';
$algo = 'BF-CFB'; // blowfish cfb
$cypherText = openssl_encrypt($plainText, $algo, $key);
echo base64_encode($cypherText); // ZTVtdWNMRUp5N1dwZ2NFPQ==
echo openssl_decrypt($cypherText, $algo, $key);
For a more complete implementation, including generation of the $iv
as you were attempting to do in your original question, the following code will work:
$plainText = 'Hello world';
$key = 'my-secret-encrpytion-key';
$algo = 'BF-CFB'; // blowfish cfb
$ivLen = openssl_cipher_iv_length($algo); // $ivLen = 8
$iv = openssl_random_pseudo_bytes($ivLen);
$cypherText = openssl_encrypt($plainText, $algo, $key, 0, $iv);
echo base64_encode($cypherText); // V3JtL1crWHZKL1lEeGJBPQ==
echo openssl_decrypt($cypherText, $algo, $key, 0, $iv);
This was edited to correct variable names that were incorrect.