JS加密(使用google crypto-js加密组件,官方地址 本地下载: CryptoJS-v3.1.2.rar)
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/aes.js";;></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/pad-zeropadding.js";;></script> <script> var data = "username"; var key = CryptoJS.enc.Latin1.parse('1234567812345678'); var iv = CryptoJS.enc.Latin1.parse('1234567812345678'); //加密 var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}); document.write(encrypted);//rwRxKql4WWspSBZcV4HP4A== </script>
PHP解密
$privateKey = "1234567812345678"; $iv = "1234567812345678"; $encryptedData = base64_decode("rwRxKql4WWspSBZcV4HP4A=="); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv); echo 'php---'.$decrypted;
==================================================
当出现无法验证密码的情况,通过Laravel dd一下解密的结果,发现后面多了很多“\x00\x00”
解决办法:清楚后面的字符
$cleaned = strval(str_replace("\0", "", $password));
参考文章:
https://blog.catscarlet.com/201701162689.html
本帖已被设为精华帖!