dongyanggan3025
2014-09-28 04:59
浏览 94

AES-256-CBC Mcrypt - Crypto-JS加密和PHP解密[关闭]

Reference: AES-256-CBC Mcrypt-PHP decrypt and Crypto-JS Encrypt

I tried using the code provided by Shurik to get a working example, but could not get this to work.

Thanks for any help.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Encrypt with javascript and decrypt using php</title>
    <meta name="generator" content="BBEdit 10.5" />

    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/pbkdf2.js"></script>
    <script>
        function encrypt_me(obj)
        {
            // alert(obj.input_value.value);

            var input_value = document.getElementById("input_value").value;
            // alert(input_value);

            var salt = CryptoJS.lib.WordArray.random(128/8);
            var key256Bits500Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 256/32, iterations: 500 });
            var iv  = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f'); // just chosen for an example, usually random as well

            var encrypted = CryptoJS.AES.encrypt(input_value+"_Messsage", key256Bits500Iterations, { iv: iv });
            var data_base64 = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
            var iv_base64   = encrypted.iv.toString(CryptoJS.enc.Base64);
            var key_base64  = encrypted.key.toString(CryptoJS.enc.Base64);

            document.getElementById("data_base64").value = data_base64;
            document.getElementById("iv_base64").value = iv_base64;
            document.getElementById("key_base64").value = key_base64;
            document.getElementById("encrypted_value").value = encrypted;
        }
    </script>
</head>
<body>
    <?php

        $mode = 'text';     // choices {"text", "hidden"}
        if(isset($_POST['submit']))
        {
            echo('<pre>'.print_r($_POST, true).'</pre>');

            $input_value = $_POST['input_value'];
            $encrypted_value = $_POST['encrypted_value'];
            $decrypted_value = $_POST['decrypted_value'];

            $encrypted = base64_decode("data_base64"); // data_base64 from JS
            $iv        = base64_decode("iv_base64");   // iv_base64 from JS
            $key       = base64_decode("key_base64");  // key_base64 from JS

            $decrypted_value = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted_value, MCRYPT_MODE_CBC, $iv ), "\t\0 " );
            echo ("\$decrypted_value : ".$decrypted_value."<br />");
        }
        else
        {
            $input_value = "";
            $encrypted_value = "";
            $decrypted_value = "";

            $encrypted = "";
            $iv = "";
            $key = "";
            $plaintext = "";
        }
    ?>
    <form action="sample_encrypt.php" method="post" name="main_form" id="main_form">

        input_value : <input type="text" id="input_value" name="input_value" value="<?=$input_value?>" size="100"><br />
        encrypted_value : <input type="text" id="encrypted_value" name="encrypted_value" value="<?=$encrypted_value?>" size="100"><br />
        decrypted_value : <input type="text" id="decrypted_value" name="decrypted_value" value="<?=$decrypted_value?>" size="100"><br />
        <br />
        data_base64 : <input type="<?=$mode?>" id="data_base64" name="data_base64" value="<?=$encrypted?>" size="100"><br />
        iv_base64   : <input type="<?=$mode?>" id="iv_base64" name="iv_base64" value="<?=$iv?>" size="100"><br />
        key_base64  : <input type="<?=$mode?>" id="key_base64" name="key_base64" value="<?=$key?>" size="100"><br />

        <input type="button" id="no_submit" name="no_submit" value="Encrypt" onclick="encrypt_me(this.form);">
        <br />
        <input type="submit" id="submit" name="submit" value="Decrypt">
    </form>
</body>
</html>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanhuai7532 2014-09-28 09:40
    已采纳

    You need to change three things to make it work.


    First, since you are using 128-bit AES, your key must be 128 bit:

    var key256Bits500Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 128/32, iterations: 500 });
    

    Note, I changed keySize from 256/32 to 128/32.


    Next, you need to get encrypted, iv and key from the input:

    $encrypted = base64_decode($_POST["data_base64"]); // data_base64 from JS
    $iv        = base64_decode($_POST["iv_base64"]);   // iv_base64 from JS
    $key       = base64_decode($_POST["key_base64"]);  // key_base64 from JS
    

    Note, I added $_POST[...] around the strings.


    Finally, you should base64 decode the encrypted value.

    $encrypted_value = base64_decode($_POST['encrypted_value']);
    

    See the code in its entirety here.

    打赏 评论

相关推荐 更多相似问题