// JavaScript Document
// <!-- Quick! Hide the java!

// Speaking of Java, this particular script is (C) Copyright 2002 Jim Tucek
// This script is NOT for public use.  I'm working on a general RSA encryption
// script, so be patient!  This is not the script that decrypts an email so that
// spam bots can't find it.  This is the script that makes that script!
// (Wow, code that writes code.  Not that big of a deal really)

// Visit www.jracademy.com/~jtucek/ for script information or 
// www.jracademy.com/~jtucek/email.html for contact information.

// A brief history of this script can be found (and it's rather entertaining)
// at www.jracademy.com/~jtucek/eencrypt.html

function MakeArray(n) {
     this.length = n
     for (var i = 1; i <= n; i++) {
          this[i] = 0
     }
     return this
}

function makeKey(p,q) {
	var c = 0;
	var z = (p-1)*(q-1);
	var e = 3
	var n = p*q;
	if(getKey(e,z) == 1)
		e = 5;
	if(getKey(e,z) == 1)
		e = 7;
	var d = getKey(e,z);
	//Turn the string into an array of numbers < 255
	var m = document.form.insertEmail.value;
	var length = m.length;
	theString = new MakeArray(length);
	for(var i = 0; i < length; i++) {
		theString[i+1] = m.charCodeAt(i);
	}
	//Encrypt each of the numbers
	theCode = new MakeArray(length);
	c = "";
	var temp = 0;
	for(i = 0; i < length; i++) {
		if(i != 0)
			c+= " ";
		temp = Math.pow(theString[i+1],e) % n;
		theCode[i+1] = temp;
		c += temp;
	}
	//var c = Math.pow(m,e) % n;
	var tempString = "";
	tempString += "'javascript:goForth(\"";
	tempString += c;
	tempString += "\",";
	tempString += n;
	tempString += ",";
	tempString += d;
	tempString += ")'";
	document.form.email.value = tempString
	//document.form.insertEmail.value = "Your e-mail has been encrypted";
	//}
}

function getKey(e,z) {
	A = 1;B = 0;C = z;F = 0;G = 1;bar = e;    
	// Euclid's Algorithm:
	while (bar != 0) {
		foo = Math.floor(C/bar);
		K = A - foo * F;
		L = B - foo * G;
		M = C - foo * bar;
		A = F;B = G;C = bar;
		F = K;G = L;bar = M;
	}
	if (B < 0)
		return (B + z);
	else
		return (B);
}

function goForth(c,n,d) {
c += ' ';
var length = c.length;
var number = 0;
var bar = 0;
var answer = '';
for(var i = 0; i < length; i++) {
number = 0;
bar = 0;
while(c.charCodeAt(i) != 32) {
number = number * 10;
number = number + c.charCodeAt(i)-48;
i++;
}
answer += String.fromCharCode(decrypt(number,n,d));
}
// Updated security feature

parent.location = 'm'+'a'+'i'+'l'+'t'+'o'+':'+answer;
}

function decrypt(c,n,d) {
// Split exponents up
	if (d % 2== 0) {
		 bar = 1;
		 for(var i = 1; i <= d/2; i++) {
			 foo = (c*c) % n;
			 bar = (foo*bar) % n;
		 }
	} else {
		 bar = c;
		 for(var i = 1; i <= d/2; i++) {
		   foo = (c*c) % n;
		   bar = (foo*bar) % n;
		 }
	}
	return bar;
}
// Stop hiding script -->
