Reputation: 5
I have created a simple script for AES encryption which encrypts input text with the help of openssl encryption and encrypted value is shown as result. But I am getting same output value while giving different inputs.
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';
}
?>
Upvotes: 0
Views: 42
Reputation: 20236
You're always encrypting an empty string. Change the line with openssl_encrypt to this:
$output = openssl_encrypt($inputText, $encrypt_method, $key, 0, $iv);
Upvotes: 1