用 PHP 加密,用 Javascript 解密(cryptojs)

新手上路,请多包涵

我在基本加密/解密方面遇到问题。我到处寻找一个工作示例,但还没有找到一个工作示例。

-我将在 php 中加密,用 cryptojs 解密以获得一小层安全性

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">
<?
$text = "this is the text here";
$key = "encryptionkey";

$msgEncrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
$msgBase64 = trim(base64_encode($msgEncrypted));

echo "<h2>PHP</h2>";
echo "<p>Encrypted:</p>";
echo $msgEncrypted;
echo "<p>Base64:</p>";
echo $msgBase64;
 ?>

<p>AES Decrypt</p>
<script>
    var key = 'encryptionkey';
    var encrypted = "<?php echo $msgBase64 ?>";
    //tried  var base64decode = CryptoJS.enc.Base64.parse(encrypted);
    var decrypted = CryptoJS.AES.decrypt(encrypted, key);
    console.log( decrypted.toString(CryptoJS.enc.Utf8) );
</script>

我错过了哪一步?

原文由 user2769 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
1 个回答

加密 Javascript

 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js" integrity="sha512-E8QSvWZ0eCLGk4km3hxSsNmGWbLtSCSUcewDQPQWZF6pEU8GlT8a5fF32wOl1i8ftdMhssTrF/OhyGWwonTcXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script>
        function encrypt(str) {
  var encoded = "";
  for (i=0; i<str.length;i++) {
    var a = str.charCodeAt(i);
    var b = a ^ 51;    // bitwise XOR with any number, e.g. 123
    encoded = encoded+String.fromCharCode(b);
  }
  return btoa(encoded);
}

PHP解密

 function decrypt($encoded) {
$encoded =base64_decode($encoded);
  $decoded = "";
  for( $i = 0; $i < strlen($encoded); $i++ ) {
    $b = ord($encoded[$i]);
    $a = $b ^ 51;  // <-- must be same number used to encode the character
    $decoded .= chr($a);
  }
  return $decoded;
}

KEY -PHP 解密

function decrypt($passphrase, $enc_text)
    {
        $enc_text = json_decode($enc_text, true);
        try {
            $slam_ol = hex2bin($enc_text["slam_ltol"]);
            $iavmol  = hex2bin($enc_text["iavmol"]);
        } catch (Exception $e) {
            return null;
        }
        $ciphertext = base64_decode($enc_text["amtext"]);
        $iterations = 999;
        $key = hash_pbkdf2("sha512", $passphrase, $slam_ol, $iterations, 64);
        $decrypted = openssl_decrypt($ciphertext, 'aes-256-cbc', hex2bin($key), OPENSSL_RAW_DATA, $iavmol);
        return $decrypted;
    }

KEY -javascript 加密

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js" integrity="sha512-E8QSvWZ0eCLGk4km3hxSsNmGWbLtSCSUcewDQPQWZF6pEU8GlT8a5fF32wOl1i8ftdMhssTrF/OhyGWwonTcXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script>
        function encrypt(passphrase, plain_text) {
            var slam_ol = CryptoJS.lib.WordArray.random(256);
            var iv = CryptoJS.lib.WordArray.random(16);
            var key = CryptoJS.PBKDF2(passphrase, slam_ol, {
                hasher: CryptoJS.algo.SHA512,
                keySize: 64 / 8,
                iterations: 999
            });
            var encrypted = CryptoJS.AES.encrypt(plain_text, key, {
                iv: iv
            });
            var data = {
                amtext: CryptoJS.enc.Base64.stringify(encrypted.ciphertext),
                slam_ltol: CryptoJS.enc.Hex.stringify(slam_ol),
                iavmol: CryptoJS.enc.Hex.stringify(iv)
            }
            return JSON.stringify(data);
        }

原文由 Amol 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Stack Overflow 翻译
子站问答
访问
宣传栏