/////////////////////////////////////////////////
// WebAgain - Content-Management-System        //
// Copyright 2002-2009 NOTEVO (www.notevo.com) //
/////////////////////////////////////////////////

var hex_chr_rsa = "0123456789abcdef";
function rhex_rsa(num)
{
  str = "";
  for(j = 0; j < 1; j++)
    str += hex_chr_rsa.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr_rsa.charAt((num >> (j * 8)) & 0x0F);
  return str;
}

function mo (g, l)
{
	return g - (l * Math.floor (g/l));
}

function powmod (tbase, texp, tmodulus)
{
	accum = 1;
	i = 0;
	basepow2 = tbase;
	while ((texp >> i)>0) {
		if (((texp >> i) & 1) == 1) {
			accum = mo((accum * basepow2) , tmodulus);
		}
		basepow2 = mo((basepow2 * basepow2) , tmodulus);
		i++;
	}
	return accum;
}

function rsa_decrypt (c, d, n)
{
	decryptarray = c.split(" ");
	deencrypt = "";
	resultd = "";
	
	for (u=0; u< decryptarray.length; u++)
	{
		resultmod = String(powmod(decryptarray[u], d, n));
		deencrypt = deencrypt + resultmod.substr(1,resultmod.length-2);
	}
	for (u=0; u<deencrypt.length; u+=2)
	{	
		resultd += "%"+rhex_rsa(Number(deencrypt.substr(u, 2)) + 30);
	}
	return unescape(resultd);
}

function rsa_encrypt (m, e, n)
{
	asciidx = 0;
	coded = "";
	asci = new Array();
			
	for (i=0; i<m.length; i+=3)
	{
		tmpasci="1";
		for (h=0; h<3; h++) 
		{
			if (i+h < m.length) 
			{
				tmpstr = String((m.substr(i+h, 1)).charCodeAt(0)-30);
				
				if (tmpstr.length < 2)
				{
					tmpstr = "0" + tmpstr;
				}			

			}
			else
			{
				break;
			}
			tmpasci += tmpstr;
		}
		
		asci[asciidx++] = tmpasci + "1";

	}

	for (k=0; k< asci.length; k++)
	{
		resultmod = powmod(asci[k], e, n);
		coded += resultmod + " ";
	}
	return coded;
}