klenwell information services : JavascriptKwyption

Kwyption

Kwyption is a javascript encryption class developed from Paul A. Johnson's md5 library and John M. Hanna's rc4 library to provide a convenient interface for encrypting and decrypting strings in javascript.

A note of efficiency : the kwypt_string method basically double-encrypts a string twice using the rc4 method. This is highly inefficient (effectively tripling string length). But it does add a little more security for public data against brute-force trolling attacks. For basic rc4 encryption, there are encrypt and decrypt methods available. (See kwyption.core.js and the Rick 4 class.)

license : GPL2
source : google code
demo : klenwell.googlepages.com

Sample Usage

simple example
var debug = 0;
var key = 'a simple key';
var plaintext = 'This is top secret.  Reveal to no one.  Unless they have the precious key.'
var TheKwyptor = new Kwyption(debug);
TheKwyptor.ini(key);
var ciphertext = TheKwyptor.enkwypt_string(plaintext);
alert(ciphertext);
// will display something like: OLRHqz+$XDS3Mb+R3LOAlF55SiiU1v2ekAnh7$dw9IKMK612$FWKthCTlVg$ykpiwfW61oQ174ea6cc8187d9bbdd96358f1a279db43XuuKI$RrrEPKXGUNGievyp$gNOb@VgQ5Gop


working example
the code below is the actual code from the demo listed above
<!-- from head -->
<script type="text/javascript" src="http://klenwell.googlecode.com/svn/trunk/JS/kwyption/kwyption.ini.js"></script>

<!-- from body-->
<!-- SCRIPT -->
<script type="text/javascript">

/* Create Kwyption Object */
var debug = 0;
var TheKwyptor = new Kwyption(debug);
TheKwyptor.ini();

function run_encrypt()
{
var TheForm = document.forms['kwyptor'];
var TxtKey = TheForm.elements['keytext'];
var TaPlain = TheForm.elements['plaintext'];
var TaCipher = TheForm.elements['ciphertext'];

    if ( TaPlain.value == '' )
    {
        alert('please enter the text you wish to encrypt in the plain text box');
        return;
    }
   
    TheKwyptor.set_key(TxtKey.value);
    TaCipher.value = TheKwyptor.enkwypt_string(TaPlain.value);
    TaPlain.value = '';
    return;
}

function run_decrypt()
{
var TheForm = document.forms['kwyptor'];
var TxtKey = TheForm.elements['keytext'];
var TaPlain = TheForm.elements['plaintext'];
var TaCipher = TheForm.elements['ciphertext'];

    if ( TaCipher.value == '' )
    {
        alert('please enter the text you wish to decrypt in the cipher text box');
        return;
    }
    else if ( TxtKey.value == '' )
    {
        alert('please enter your key in the key field');
        return;
    }
   
    TheKwyptor.set_key(TxtKey.value);
    TaPlain.value = TheKwyptor.dekwypt_string(TaCipher.value);
    TaCipher.value = '';
    return;
}
</script>
<!-- end SCRIPT -->

<!-- THE FORM -->
<div id="kwyption_form" class="js_form">
<form name="kwyptor" id="kwyptor">
<h4>plain text</h4>
<textarea id="plaintext" cols="40" rows="4" onfocus="javascript:this.value='';">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>

<h4>key</h4>
<input id="keytext" name="keytext" type="text" value="your key here" onfocus="javascript:this.value='';" size="24" />

<div class="bx_group">
<input id="bx_encrypt" name="bx_encrypt" type="button" value="encrypt" onclick="javascript:run_encrypt();" />
<input id="bx_decrypt" name="bx_decrypt" type="button" value="decrypt" onclick="javascript:run_decrypt();" />
</div>

<h4>cipher text</h4>
<textarea id="ciphertext" cols="40" rows="4"></textarea>
</form>
</div>
<!-- end FORM -->



CategoryJavascript