/*********************************************************************************

	NWW Client Side RSA Encrypter/Decrypter v1.1 by Aaron MacQuarrie

	This script is not to be used without the permission
	of both NWW and the Script Author.

	© Copyright 2001, NWW.
	Web		: http://www.nwwebs.co.uk/scripts/
	Email	: aaron@nwwebs.co.uk

	Latest Release : 30/9/2001

	Known Bugs : None

*********************************************************************************/

// Outgoing RSA Codes
var PublicKey=43709569;
var Lock1=12234779;

// Incoming RSA Codes
var PrivateKey=10018969;
var Lock2=12234779;


// RSA Encoder

function toAscii_rsa(ch)
{
	var start=32;
	var ASCII = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
	for (i=0;i<ASCII.length;i++)
	{
		if (ch==ASCII.charAt(i))
		{
			return(i+start);
		}
	}
}

function csEncrypt_rsa(text)
{
	var outString = "";

	for(k=0;k<text.length;k++)
	{
		outString += encode_rsa(toAscii_rsa(text.charAt(k)));
		if(k+1<text.length)
		{
			outString += "+"
		}
	}
	return outString;
} 

function encode_rsa(x)
{
	var p = PublicKey;
	var m = Lock1;
	var y = 1;
	
	while(p > 0)
	{
		while(Math.round(p/2)==(p/2))
		{
			x = (x * x) % m;
			p /= 2;
		}
		y = (x * y) % m;
		p --;
	}
	return y;
}


// RSA Decoder

function decode_rsa(x)
{
	var p = PrivateKey;
	var m = Lock2;
	var y = 1;
	while(p > 0)
	{
		while(Math.round(p/2)==(p/2))
		{
			x = (x * x) % m;
			p /= 2;
		}
		y = (x * y) % m;
		p --;
	}
	return fromAsciiCharAt_rsa(y);
}

function fromAsciiCharAt_rsa(x)
{
	var start=32;
	var ASCII = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
	return ASCII.charAt(x-start);
}

function csDecrypt_rsa(inString)
{
	var outString = "";

	var EncryptedChars = String(inString).split("+");

	for (i=0;i<EncryptedChars.length;i++)
	{
		outString += decode_rsa(EncryptedChars[i]);
	}
	return outString;	
}