使用Ctypto-js加密 然后用PHP解密
最近项目需要用到Ctypto-js加密 然后用PHP解密的需求,在这里记录下。
Html部分
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
- </head>
- <body>
- <script type="text/javascript">
- var CryptoJSAesJson = {
- stringify: function (cipherParams) {
- var j = {
- ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
- };
- if (cipherParams.iv) j.iv = cipherParams.iv.toString();
- if (cipherParams.salt) j.s = cipherParams.salt.toString();
- return JSON.stringify(j);
- },
- parse: function (jsonStr) {
- var j = JSON.parse(jsonStr);
- var cipherParams = CryptoJS.lib.CipherParams.create({
- ciphertext: CryptoJS.enc.Base64.parse(j.ct)
- });
- if (j.iv) cipherParams.iv = CryptoJS.enc.Hex.parse(j.iv)
- if (j.s) cipherParams.salt = CryptoJS.enc.Hex.parse(j.s)
- return cipherParams;
- }
- }
- var encrypted = CryptoJS.AES.encrypt(JSON.stringify("value to encrypt"), "my passphrase", {
- format: CryptoJSAesJson
- }).toString();
- var decrypted = JSON.parse(CryptoJS.AES.decrypt(encrypted, "my passphrase", {
- format: CryptoJSAesJson
- }).toString(CryptoJS.enc.Utf8));
- console.log(encrypted,decrypted);
- </script>
- </body>
- </html>
php部分
- <?php
- /**
- * Decrypt data from a CryptoJS json encoding string
- *
- * @param mixed $passphrase
- * @param mixed $jsonString
- * @return mixed
- */
- function cryptoJsAesDecrypt($passphrase, $jsonString) {
- $jsondata = json_decode($jsonString, true);
- $salt = hex2bin($jsondata["s"]);
- $ct = base64_decode($jsondata["ct"]);
- $iv = hex2bin($jsondata["iv"]);
- $concatedPassphrase = $passphrase.$salt;
- $md5 = array();
- $md5[0] = md5($concatedPassphrase, true);
- $result = $md5[0];
- for ($i = 1; $i < 3; $i++) {
- $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true);
- $result .= $md5[$i];
- }
- $key = substr($result, 0, 32);
- $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
- return json_decode($data, true);
- }
- /**
- * Encrypt value to a cryptojs compatiable json encoding string
- *
- * @param mixed $passphrase
- * @param mixed $value
- * @return string
- */
- function cryptoJsAesEncrypt($passphrase, $value) {
- $salt = openssl_random_pseudo_bytes(8);
- $salted = '';
- $dx = '';
- while (strlen($salted) < 48) {
- $dx = md5($dx.$passphrase.$salt, true);
- $salted .= $dx;
- }
- $key = substr($salted, 0, 32);
- $iv = substr($salted, 32, 16);
- $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv);
- $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt));
- return json_encode($data);
- }
- $encrypted = cryptoJsAesEncrypt("my passphrase", "value to encrypt");
- $decrypted = cryptoJsAesDecrypt("my passphrase", $encrypted);
- var_dump($decrypted);
来源:https://pretagteam.com/question/aes-encryption-in-php-and-then-decryption-with-javascript-cryptojs
发布于记忆碎片-网络技术分享 https://huilang.me
文章地址:https://huilang.me/shi-yong-ctypto-js-jia-mi-ran-hou-yong-php-jie-mi/