rsa, a suite of routines for performing RSA Public-key computations in
Javascript.
//
Requires Bigint.js and Barrett.js.
//
Copyright 1998-2005 David Shapiro.
//
re-use, abuse, copy, and modify this code to your liking
Please keep the This header.
//
thanks!
//
Dave Shapiro
[email protected]
function Rsakeypair (encryptionexponent, decryptionexponent, Modulus)
{
THIS.E = Bifromhex (encryptionexponent);
THIS.D = Bifromhex (decryptionexponent);
THIS.M = Bifromhex (modulus);
We can do the bytes per digit, so
ChunkSize = 2 * (number of digits in modulus-1).
Since Bihighindex returns the high index, not the number of digits, 1 have
Already been Subtracted.
This.chunksize = 2 * Bihighindex (this.m);
This.radix = 16;
This.barrett = new BARRETTMU (this.m);
}
function Twodigit (n)
{
Return (n < 10?) "0": "") + String (n);
}
function Encryptedstring (key, S)
Altered by Rob Saunders ([email protected]). New Routine Pads The
String after it had been converted to an array. This fixes an
Incompatibility with Flash MX ' s ActionScript.
{
var a = new Array ();
var sl = s.length;
var i = 0;
While (i < Sl) {
a[i] = S.charcodeat (i);
i++;
}
While (a.length% key.chunksize! = 0) {
a[i++] = 0;
}
var al = a.length;
var result = "";
var j, k, block;
For (i = 0; I < al; i + = Key.chunksize) {
block = new BigInt ();
j = 0;
For (k = i; K < i + key.chunksize; ++j) {
block.digits[j] = a[k++];
block.digits[j] + = a[k++] << 8;
}
var crypt = Key.barrett.powMod (block, key.e);
var Text = Key.radix = = 16? Bitohex (crypt): bitostring (crypt, key.radix);
Result + = text + "";
}
return result.substring (0, result.length-1); Remove last Space.
}
function Decryptedstring (key, S)
{
var blocks = S.split ("");
var result = "";
var i, j, block;
For (i = 0; I < blocks.length; ++i) {
var bi;
if (key.radix = = 16) {
Bi = Bifromhex (blocks[i]);
}
else {
Bi = bifromstring (blocks[i], key.radix);
}
block = Key.barrett.powMod (bi, key.d);
For (j = 0; J <= Bihighindex (block); ++j) {
Result + = String.fromCharCode (block.digits[j] & 255,
block.digits[j] >> 8);
}
}
Remove trailing null, if any.
If (result.charcodeat (result.length-1) = = 0) {
result = result.substring (0, result.length-1);
}
Return result;
}
JS version of RSA algorithm