JS version of RSA algorithm

Source: Internet
Author: User
Tags crypt modulus

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.