您现在的位置是: 网站首页>详情 文章详情

前端js 加解密 后端php加解密,采用AES

erhas2019-12-19 10:34:18go

<!DOCTYPE html>
<html>
<head>
	<meta charset="Utf8">
    <title>AES</title>
</head>
<script type="text/javascript" src="http://react.file.alimmdn.com/aes.js"></script>
<body>
    <script> 
        var IV = 'THISISAGOODDAY!!'; // 设置16位
        var KEY = '1234567891012345';// 设置16位
        var data=JSON.stringify({'name':'zsq','password':'123456'});

        // 加密
        function encrypt(str) {
            var key = CryptoJS.enc.Utf8.parse(KEY);
            var iv = CryptoJS.enc.Utf8.parse(IV);
            var encrypted = CryptoJS.AES.encrypt(str,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});
            return encrypted.toString();
        }

        // 解密
        function decrypt(str) {
            var key = CryptoJS.enc.Utf8.parse(KEY);
            var iv = CryptoJS.enc.Utf8.parse(IV);
            var decrypted = CryptoJS.AES.decrypt(str,key,{iv:iv,padding:CryptoJS.pad.Pkcs7});
            return decrypted.toString(CryptoJS.enc.Utf8);
        }

        var encode = encrypt(data);
        var decode = decrypt(encode);
         console.log(encode);
        console.log(decode);
    </script>
</body>
</html>
<?php
  $data = 'KN6VUKu9BnDqDEPjl5dGYlrYWAWwnUBA5z1KGBz2uBX6xc4Og0DvuxWX5WL4emgi';
  $key = "1234567891012345";
  $iv = 'THISISAGOODDAY!!';
  // 加密
  $encode = base64_encode(openssl_encrypt($data,"AES-128-CBC",$key,true,$iv));
  // 解密
  $decode = openssl_decrypt(base64_decode($data),"AES-128-CBC",$key,true,$iv);
  var_dump(json_decode($decode,true));