使用Ctypto-js加密 然后用PHP解密

2022.01.08 分享 1231 人浏览 留言

最近项目需要用到Ctypto-js加密 然后用PHP解密的需求,在这里记录下。

Html部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
  9. </head>
  10. <body>
  11. <script type="text/javascript">
  12. var CryptoJSAesJson = {
  13.   stringify: function (cipherParams) {
  14.     var j = {
  15.       ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
  16.     };
  17.     if (cipherParams.iv) j.iv = cipherParams.iv.toString();
  18.     if (cipherParams.salt) j.s = cipherParams.salt.toString();
  19.     return JSON.stringify(j);
  20.   },
  21.   parse: function (jsonStr) {
  22.     var j = JSON.parse(jsonStr);
  23.     var cipherParams = CryptoJS.lib.CipherParams.create({
  24.       ciphertext: CryptoJS.enc.Base64.parse(j.ct)
  25.     });
  26.     if (j.iv) cipherParams.iv = CryptoJS.enc.Hex.parse(j.iv)
  27.     if (j.s) cipherParams.salt = CryptoJS.enc.Hex.parse(j.s)
  28.     return cipherParams;
  29.   }
  30. }
  31. var encrypted = CryptoJS.AES.encrypt(JSON.stringify("value to encrypt"), "my passphrase", {
  32.   format: CryptoJSAesJson
  33. }).toString();
  34. var decrypted = JSON.parse(CryptoJS.AES.decrypt(encrypted, "my passphrase", {
  35.   format: CryptoJSAesJson
  36. }).toString(CryptoJS.enc.Utf8));
  37. console.log(encrypted,decrypted);
  38. </script>
  39. </body>
  40. </html>

php部分

  1. <?php
  2. /**
  3.  * Decrypt data from a CryptoJS json encoding string
  4.  *
  5.  * @param mixed $passphrase
  6.  * @param mixed $jsonString
  7.  * @return mixed
  8.  */
  9. function cryptoJsAesDecrypt($passphrase$jsonString) {
  10.   $jsondata = json_decode($jsonString, true);
  11.   $salt = hex2bin($jsondata["s"]);
  12.   $ct = base64_decode($jsondata["ct"]);
  13.   $iv = hex2bin($jsondata["iv"]);
  14.   $concatedPassphrase = $passphrase.$salt;
  15.   $md5 = array();
  16.   $md5[0] = md5($concatedPassphrase, true);
  17.   $result = $md5[0];
  18.   for ($i = 1; $i < 3; $i++) {
  19.      $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true);
  20.      $result .= $md5[$i];
  21.   }
  22.   $key = substr($result, 0, 32);
  23.   $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
  24.   return json_decode($data, true);
  25. }
  26. /**
  27. * Encrypt value to a cryptojs compatiable json encoding string
  28. *
  29. * @param mixed $passphrase
  30. * @param mixed $value
  31. * @return string
  32. */
  33. function cryptoJsAesEncrypt($passphrase$value) {
  34.   $salt = openssl_random_pseudo_bytes(8);
  35.   $salted = '';
  36.   $dx = '';
  37.   while (strlen($salted) < 48) {
  38.      $dx = md5($dx.$passphrase.$salt, true);
  39.      $salted .= $dx;
  40.   }
  41.   $key = substr($salted, 0, 32);
  42.   $iv = substr($salted, 32, 16);
  43.   $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv);
  44.   $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt));
  45.   return json_encode($data);
  46. }
  47. $encrypted = cryptoJsAesEncrypt("my passphrase""value to encrypt");
  48. $decrypted = cryptoJsAesDecrypt("my passphrase"$encrypted);
  49. var_dump($decrypted);

来源:https://pretagteam.com/question/aes-encryption-in-php-and-then-decryption-with-javascript-cryptojs

文章地址:https://huilang.me/shi-yong-ctypto-js-jia-mi-ran-hou-yong-php-jie-mi/

回复本文

电子邮件地址不会被公开。 必填项已用*标注

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif