Old Documentation

Javascript

Requirements:
jQuery 1.3.2

Options:

Name Default Description
formElements “input,select,textarea” string / jQuery selector
The jQuery seletor to select which form element should be encrypted. Leave this option to get all form elements.
submitEvent “click” string / jQuery event
Event that triggers the submit of the form. “Click” also handles the “Enter button”.
submitTrigger “:input:submit” string / jQuery selector
The element where the submitEvent should get bound to.
getKeysURL “main.php?generateKeypair=true” string
The url where the script should receive the keys.
postVariable “jCryption” string
The name of the POST/GET variable which the PHP-Script receives.
disableAllFields true boolean
Disables all form fields when you submit a form. You have to do that if you submit the form without ajaxSubmit because enabled form elements will still be sent unencrypted to the server.
addClassDisabled false boolean
Set to true if jCryption should add a class disabeld to every disabled element.
collectionSpeed 1 int / in ms
The speed of the element collection.
encryptionSpeed 1 int / in ms
The speed of each encryption step.
receivedKeys function(keys) {} function(keys)
Called when keys are received from the server.
beforeEncryption function() { return true; } function()
This function is called before the keys will be retrieved from the server (if you w”ant to javascript validate something do it here), return true to continue.
inputCollectorCallback function(i,length) {} function(i,length)
“this”, refers to the current collected input element. “i” is the current number of the element and “length” is the number of all elements.
inputCollectorFinished function(length) { return true; } function(length)
Called when all elements are collected, you can abort the encryption by returning false.
encryptionCallback function(i,length) {} function(i,length)
This function is called on every encryption step. “i” is the current number of the step and “length” is the total number of all steps.
encryptionFinished function(encryptedString,length) {return true;} function(encryptedString,length)
Called when the encryption is finished. You can still cancel the submit of the form by returning false.
ajaxSubmit false boolean
When you set this to true the form will be sent by $.ajax with following options.
ajaxDataType “html” string
jQuery $.ajax dataType.
ajaxSubmitSuccess function(data, textStatus) {} function(data, textStatus)
jQuery $.ajax success callback function.
ajaxSubmitError function(XMLHttpRequest, textStatus, errorThrown) {} function(XMLHttpRequest, textStatus, errorThrown)
jQuery $.ajax error callback function.

PHP

Requirements:
PHP 4.0.4 with libbcmath

Here is a small example how you may use the jCryption PHP class.
Basically you’ll find everything you need in this example.

<?php
session_start();
require_once("jCryption-1.0.1.php");
 
$keyLength = 256;
$jCryption = new jCryption();
 
if(isset($_GET["generateKeypair"])) {
	$keys = $jCryption->generateKeypair($keyLength);
 
	//sets the keys in the session to have them ready when you submit the form
	//please keep this structure. You'll need the hex value of the key for javascript
	//and the int value of the key for PHP. e = public key, d = private key, n = modulo
	$_SESSION["e"] = array("int" => $keys["e"], "hex" => $jCryption->dec2string($keys["e"],16));
	$_SESSION["d"] = array("int" => $keys["d"], "hex" => $jCryption->dec2string($keys["d"],16));
	$_SESSION["n"] = array("int" => $keys["n"], "hex" => $jCryption->dec2string($keys["n"],16));
 
	//returns the needed keys for the javascript part in a JSON string
	//maxdigits is need for the javascript and caluclated like ($keyLength * 2 / 16 + 3)
	echo '{"e":"'.$_SESSION["e"]["hex"].'","n":"'.$_SESSION["n"]["hex"].'","maxdigits":"'.intval($keyLength*2/16+3)).'"}';
} else {
	//print the orginial POST
	echo "POST:";
	print_r($_POST);
	echo "<br/>";
 
	//here the decrypt function is called. The first parameter is the encrypted POST.
	//Second parameter is the private key d in it's int form and third the modulo n.
	//This function will return your orginal decrypted POST.
	$var = $jCryption->decrypt($_POST['jCryption'], $_SESSION["d"]["int"], $_SESSION["n"]["int"]);
	//parses the decrypted string and returns it in an array.
	//now it's important to unset the keys from the session
	unset($_SESSION["e"]);
	unset($_SESSION["d"]);
	unset($_SESSION["n"]);
	parse_str($var,$result);
	echo "decrypted POST";
	print_r($result);
}
?>
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


No trackbacks yet.