前端使用CryptoJS AES加密 ,后端php解密问题
发布网友
发布时间:2022-04-23 05:52
我来回答
共1个回答
热心网友
时间:2022-04-07 10:48
PHP7.1 已经不能用mcrypt了,所以我用的是openssl_encrypt和openssl_decrypt。
<?php
$data = "This is an AES crypt demo.";
$privateKey = "5d4bcd5912db00c28e9ce7fd5e9b7f78"; // KEY 16字节用aes-128-cbc,32字节用aes-256-cbc
$iv= "4490d2ded4f2d4ad"; //AES的IV是16个字节
//加密
// $encrypted = openssl_encrypt($data,'aes-128-cbc',$privateKey,0,$iv);
$encrypted = openssl_encrypt($data,'aes-256-cbc',$privateKey,0,$iv);
echo $encrypted,PHP_EOL;
// 解密
$encryptedData = $encrypted;
// $decrypted = openssl_decrypt($encryptedData,'aes-128-cbc',$privateKey,0,$iv);
$decrypted = openssl_decrypt($encryptedData,'aes-256-cbc',$privateKey,0,$iv);
echo($decrypted);
输出结果如下:
EPcMQRXA53/hRkPyILFI4fF/9sW2X53tLiDT26khNsA=
This is an AES crypt demo.