I want to encrypt the content in a textarea using the key provided on clicking on a button and decrypt again on clicking on another button
<input type="button" name="encrypt" value="Encrypt" onclick="encryption();">
.
.
<script type="text/javascript">
var xmlHttp;
function encryption()
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
// Display a quick message while the script is being processed
document.getElementById('displaycontent').innerHTML = "...Encryption in progress...";
// Getting user input
var key = document.getElementById("txtKey").value;
var content = document.getElementById("txtContent").value;
// alert("Key is " + key + " content is " + content)
// Send the key and the content through the url
var url = "encdec.php";
url = url + "?keysent=" + key;
url = url + "&contentsent=" + content;
xmlHttp.onreadystatechenge = stateChanged;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChanged()
{
if(xmlHttp.readyState == 4)
{
document.getElementById("displaycontent").innerHTML = xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp = null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
// For Internet Explorer 6.0+
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
// For Internet Explorer 5.5+
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<?php
header("Cache-Control: no-cache, must-revalidate");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// Get the parameters from the url
$key = $_GET["keysent"];
$text = $_GET["contentsent"];
// For encryption/decryption to work enable php_mstring and php_mcrypt in PHP_settings -> PHP_extensions -> ..
// Encrypt the content using the key
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
// Set output
if(empty($key) && empty($text))
{
$response = "Please enter key and content to encrypt!";
}
else
{
$response = $crypttext;
}
// Display response
echo $response;
?>