PHP/JS 互通加密-解密

  • 场景:服务端Php与前端Js 实现互通加密/加密
  • php7.1以后mcrypt_encrypt将被废弃,所以使用openssl_decrypt和openssl_encrypt的组合方式实现AES加密
  • 环境 PHP7.*

php加密-解密

$data = 'ABCDEFGH';
$key = "1234567898882222";
$iv = '8NONwyJtHesysWpM';
// 加密
$encode = base64_encode(openssl_encrypt($data,"AES-128-CBC",$key,true,$iv));
// 解密
$decode = openssl_decrypt(base64_decode($encode),"AES-128-CBC",$key,true,$iv);

$lists = array("encode"=>$encode,"decode"=>$decode,"iv"=>$iv,"key"=>$key,"data"=>$data);

var_dump($lists);

// 数据结果
array(5) {
  ["encode"]=>
  string(24) "uJeZ6dyWTKDMRXNOVhxRNA=="
  ["decode"]=>
  string(8) "ABCDEFGH"
  ["iv"]=>
  string(16) "8NONwyJtHesysWpM"
  ["key"]=>
  string(16) "1234567898882222"
  ["data"]=>
  string(8) "ABCDEFGH"
}

JS加密-解密

  • js端使用 CryptoJS 封装的库。



    AES



    


你可能感兴趣的:(PHP/JS 互通加密-解密)