P粉6680193392023-09-01 16:26:36
Try this:
function encryptData($data, $key, $iv) { $cipher = "aes-256-cbc"; $options = OPENSSL_RAW_DATA; $encrypted = openssl_encrypt($data, $cipher, $key, $options, $iv); $encrypted = base64_encode($encrypted); return $encrypted; } $message = "消息"; $key = "我的秘密密钥"; $iv = "我的iv"; $encrypted = encryptData($message, $key, $iv); echo $encrypted;
P粉6396675042023-09-01 15:35:49
In the PHP code, the key must be Base64 decoded , not Base64 encoded:
$key = base64_decode("GSTEGSTEjdfheyhdHSHSHSHDHHDHmdjjdn12ndndn5r=");
With this change, the required ciphertext is created.
Please note that if 0 is passed in the fourth parameter of the
openssl_encrypt() call instead of
OPENSSL_RAW_DATA
, the encryption The file will be Base64 encoded by default. Therefore, explicitly Base64 encoding the ciphertext is unnecessary.
Please remember that static IVs are not safe. Typically during the encryption process, a random IV is generated and passed along with the ciphertext to the decryption party (usually spliced together).