使用jQuery的HTML表单没有提供正确的输出

时间:2017-03-19 15:54:47

标签: php jquery html

我已经为AES加密创建了一个简单的脚本,它在openssl加密的帮助下加密输入文本,并且加密值显示为结果。 但是在给出不同的输入时我得到相同的输出值。

的index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Data Security</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
</head>
<body>
    <div class="container">
        <div class="well">
            <h1>
                <i>Encryption</i>
            </h1>
            <br>
            <form action="#" class="js-ajax-php-json" method="post" accept-charset="utf-8">
                Enter your Text here: <br>
                <textarea class="input-block-level" rows="5" name="inputText" id="inputText"></textarea>
               <br>                         
                <!-- aes_main_top_resp -->

                <input class="btn pull-right btn-inverse btn-small" value="Submit" name="direction" type="submit">
                    <br><br>
                <label>Results: </label><br>
                <textarea class="the-return" rows="5" name="the-return" id="the-return" readonly="readonly"></textarea>         
            </form>
        </div>
      </div>    

    <script type="text/javascript">
        $("document").ready(function(){
          $(".js-ajax-php-json").submit(function(){
              //alert("i am in");
            var data = {

            };
            data = $(this).serialize() + "&" + $.param(data);
            $.ajax({
              type: "POST",
              url: "aes.php", //Relative or absolute path to response.php file
              data: data,
              success: function(data) {
                $(".the-return").html(
                   data
                );
               // alert("Form submitted successfully.\nReturned json: " + data["json"]);
              }
            });
            return false;
          });
        });
    </script>
</body>
</html>

aes.php

<?php
$string = '';
if (is_ajax()) {
    if (isset($_POST["inputText"]) && !empty($_POST["inputText"])) { //Checks if action value exists
        $inputText = $_POST["inputText"];
        $encrypt_method = "aes-256-cbc";
        $secret_key = "something";
        $secret_iv = 'something';
        $key = hash('sha256', $secret_key);
        $iv = substr(hash('sha256', $secret_iv), 0, 16);
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
        echo $output;
    }
}

function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
?>

1 个答案:

答案 0 :(得分:1)

您总是在加密空字符串。将 openssl_encrypt 的行更改为:

$output = openssl_encrypt($inputText, $encrypt_method, $key, 0, $iv);