codes = new Array();

codes[0] = new Array();
codes[0].push("2147 1763 1335 1815 620 1555 2091 1176 1335 1815 620 1555 722 1815 1466 1466 1555 1497 1815 620 1135 1555 1874 1016 1670 1016");
codes[1] = new Array();
codes[1].push("2831 1853 1468 2518 1361 1289 495 1461 1468 2518 1361 1289 684 2518 2435 2435 1446 2518 2636 1430 1289 278 2270 1632 2270");
codes[2] = new Array();
codes[2].push("1765 1757 1185 1272 228 1126 1355 667 1185 1272 228 1126 1005 1369 1051 105 1369 1126 129 1296 1756 982 1004 982");
codes[3] = new Array();
codes[3].push("1829 1337 658 994 1161 47 1367 221 658 994 1161 47 1620 1483 994 903 994 916 994 1746 47 213 970 105 970");
codes[4] = new Array();
codes[4].push("1853 1591 1792 330 205 662 667 72 1792 330 205 662 379 762 147 762 147 1027 634 519 634");
codes[5] = new Array();
codes[5].push("1591 871 1585 532 1229 1174 77 121 1585 532 1229 1174 361 396 1338 1222 340 703 311 1174 234 1222 1447 1222");
codes[6] = new Array();
codes[6].push("2831 2827 180 2586 2586 1537 1046 2270 2270 708 708 708 1319 2073 710 710 2073 1319 710 2413 2270");
codes[7] = new Array();
codes[7].push("2119 1211 1866 2013 1559 628 461 1318 1100 1991 147 684 1261 556 2013 222 147 628 1510 221 2105 684");
codes[8] = new Array();
codes[8].push("2071 1781 233 1644 1399 586 1217 378 233 1644 1399 586 475 1429 586 1054 272 732 2026 1054 1613 1054"); 
codes[9] = new Array();
codes[9].push("2413 1717 1071 859 1919 1567 1837 484 859 1559 595 294 1276 179 859 190 144 851 1072 2334 1203 2334");
codes[10] = new Array();
codes[10].push("2581 1207 158 1558 2082 764 637 2131 158 1558 2082 764 31 764 1129 805 637 764 764 1172 774 824 774");
codes[11] = new Array();
codes[11].push("907 821 695 695 431 396 180 782 650 419 695 397 624 397");
codes[12] = new Array();
codes[12].push("2419 1819 956 1482 1482 753 176 157 157 1240 1240 1240 74 1588 1766 2198 1482 2198 1381 1015 490 1485 1588 1700 1980 2156 1015 1588 257 257 1381 1766 1588 1700 795 1381 74 490 1485 157 1479 2156 1980 294 1766 1381 1700 1482 415 157 984 1381 1766 2198 415 1381 1015 2086 74 275 753 1068");
codes[13] = new Array();
codes[13].push("2911 1909 1195 2801 2801 832 1778 637 637 2388 2388 2388 869 181 2257 2257 181 181 869 2257 1426 637");
codes[14] = new Array();
codes[14].push("1751 2231 1061 194 103 560 1211 1602 1061 194 103 560 73 1606 560 560 1315 825 648 1444 560 338 262 1535 825 1535");
codes[15] = new Array();
codes[15].push("1909 1513 960 159 1834 564 1840 852 960 159 1834 564 170 1825 564 508 508 564 1840 345 612 673 612");
codes[16] = new Array();
codes[16].push("1691 1391 1175 1586 1665 1610 853 133 853 1027 632 1610 231 25 853 986 658 537 243 1267");
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 algorithme(base,exponent,y) {

		base = base;
		temp = (base*base) % exponent;
			
		for(var i = 1; i <= y/2; i++) {
			base = (temp*base) % exponent;
		}
					
	return base;
}


