Cryptography

suggest change

Remarks

/* Base64-encoded encryption */
$enc_data = base64_encode(openssl_encrypt($data, $method, $password, OPENSSL_RAW_DATA, $iv));
/* Decode and decrypt */
$dec_data = base64_decode(openssl_decrypt($enc_data, $method, $password, OPENSSL_RAW_DATA, $iv));

This order does not work. openssl_encrypt() with OPENSSL_RAW_DATA returns raw bytes, and the value stored in $enc_data is the Base64 form of those bytes. Decrypting before Base64-decoding passes the encoded text to openssl_decrypt().

Decode first, then decrypt:

$enc_data = base64_encode(openssl_encrypt($data, $method, $pass, OPENSSL_RAW_DATA, $iv));
$dec_data = openssl_decrypt(base64_decode($enc_data), $method, $pass, OPENSSL_RAW_DATA, $iv);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents