codes = new Array();

codes[0] = new Array();
codes[0].push("1829 1819 1707 349 251 1083 833 1684 1356 564 1684 520 833 349 564 833 357 393 1216 1027 1083 1083 1027 1780 959 1198 1780");
codes[1] = new Array();
codes[1].push("2257 1957 1570 1570 47 697 36 1876 101 1385 1570 1335 1832 1756");
codes[2] = new Array();
codes[2].push("2323 1891 458 428 1240 202 115 1726 458 428 1240 202 2216 1096 1333 202 115 202 1665 760 428 760 683 293 1665 276 94 760 1146");
codes[3] = new Array();
codes[3].push("2201 1769 1645 39 1532 1734 1949 2003 1139 1734 716 1579 1276 1532 1734 1358 872 716 1949 16 2003 716 39 1809 2167 872 1579 2115 716");
codes[4] = new Array();
codes[4].push("1843 1919 1803 1552 1342 461 609 1422 1803 1552 1342 461 1406 340 461 1786 461 865 783 306 1637 461 601 111 1210 865");
codes[5] = new Array();
codes[5].push("1769 1513 109 1025 1555 108 1044 111 1595 1185 1680 101 1507 1555 1347 101 690 1044 296 109 111 690 1480 109 101 690 1044 1555 690 1347 111 708 974 1555 690 101 771 1552 1680");
codes[6] = new Array();
codes[6].push("2263 2047 872 560 110 523 139 1229 556 1668 110 68 1690 865 1812 766 1690 1036 110 484 1690 1026 139 721 872 1229 1026 477 872 1690 1026 139 110 1026 484 1229 68 424 110 1026 1690 27 545 766");
codes[7] = new Array();
codes[7].push("2449 2231 1101 283 2067 2433 364 2105 120 761 225 26 384 225 364 283 2067 384 225 684 1101 2105 2316 538 1101 225 2316 364 2067 2316 1539 2105 26 830 2067 2316 225 1286 1161 384");
codes[8] = new Array();
codes[8].push("1919 1711 755 876 1245 906 1807 1707 1236 1807 266 101 115 1707 266 1245 101 266 1698 755 1707 148 79 755 101 148 1807 1245 148 100 1707 1486 446 1245 148 101 1186 102 266");

var tableau_clef = new Array();

function decodage(crypto,indice,n,clef,courriel) {
	var index_clef = "'"+crypto+","+courriel+"'";
	if(tableau_clef[index_clef])
		return tableau_clef[index_clef];				

	if(codes[indice][crypto])					
		var crypto = codes[indice][crypto];			

	if(!crypto.length)						
		return "Erreur, code invalide.";

	if(n == 0 || clef == 0) {					
		var rangs = crypto.split(' ');		
		n = rangs[0];	clef = rangs[1];			
		rangs[0] = ""; rangs[1] = "";				
		crypto = rangs.join(" ").substr(2);
		
	}

	var texte_clair = '';
	var cryptogrammes = crypto.split(' ');

	for(var i in cryptogrammes) {
		var caractere = cryptogrammes[i];
		var cryptogramme = algorithme(caractere,n,clef);

		if(courriel && i < 7)				
			continue;
		if(courriel && cryptogramme == 63)	
			break;
		texte_clair += String.fromCharCode(cryptogramme);
	}
	
	tableau_clef[index_clef] = texte_clair;			

	return texte_clair;
}

function moulinette(indice,crypto,n,clef) {
	if(!n || !clef) { n = 0; clef = 0; }
	if(!crypto) crypto = 0;

	var texte_clair = decodage(crypto,indice,n,clef,false);
	parent.location = texte_clair;
}

function moulinet(indice,fenetre,crypto,n,clef) {
	if(!n || !clef) { n = 0; clef = 0; }
	if(!crypto) crypto = 0;

	var texte_clair = decodage(crypto,indice,n,clef,false);
	frames[fenetre].location = texte_clair;
}
function moulinet(indice,fenetre,crypto,n,clef) {
	if(!n || !clef) { n = 0; clef = 0; }
	if(!crypto) crypto = 0;

	var texte_clair = decodage(crypto,indice,n,clef,false);
	frames[fenetre].location = texte_clair;
}

function algorithme(base,exponent,y) {

		base = base;
		temp = (base*base) % exponent;
			
		for(var i = 1; i <= y/2; i++) {
			base = (temp*base) % exponent;
		}
					
	return base;
}


