function createtab() /* generates Vigénère array */
{
  //init
  array = new Array(25);
  for (i=0;i<26;i++)
  {
    array[i] = new Array(25);
  }
  
  //attribution des valeurs
  var increment = 65; //A = 65
  for (i=0;i<26;i++)
  {
    increment = 65+i;
    for (j=0;j<26;j++)
    {
      if (increment == 91 ) {increment = 65;} //on a dépassé 'Z'
      array[i][j] = asciiTOchar(increment);
      increment++;
    }
  }
}

//DEBUG
function showtab(tableau) //pour tableau 26x26
{
  for (i=0;i<26;i++)
  {
    for (j=0;j<26;j++)
    {
      window.document.write(tableau[i][j]);
    }
    window.document.write("<BR>");
  }
}

function keygen(size) /* generates a key */
{
  var key="";
  while (key.length < size)
  {
    key += asciiTOchar(Math.floor(26*Math.random())+65);
  }
  document.formulaire.key.value=key;
}

function convert(ascii) /* converts an ascii to int */
{
  return ascii - 65;
  /*A=0,B=1...*/
}

function crypt(texte,key,array,tabchar)
{
  var textcrypt ="";
  var j=0;
  var ascisse=0;
  var ordonnee=0;
  for (i=0;i<texte.value.length;i++)
  {
    if (j==key.value.length){j=0;} //parcourt toute la clé
    if (charTOascii(texte.value.charAt(i)) == 32) {textcrypt += " ";} //SPACE char
    else if(charTOascii(texte.value.charAt(i)) >= 97 && charTOascii(texte.value.charAt(i)) <= 122) //small caps
    {
      var temp = charTOascii(texte.value.charAt(i))-32; //convert in capital letters
      abscisse=convert(charTOascii(key.value.charAt(j)));
      ordonnee=convert(temp);
      textcrypt += asciiTOchar(charTOascii(array[abscisse][ordonnee])+32); //convert back in small letters
      j++;
    }
    else if(charTOascii(texte.value.charAt(i)) >= 65 && charTOascii(texte.value.charAt(i)) <= 90) //capital letters, easier !
    {
      abscisse=convert(charTOascii(key.value.charAt(j)));
      ordonnee=convert(charTOascii(texte.value.charAt(i)));
      textcrypt += array[abscisse][ordonnee];
      j++;
    }
    else //other cases : we let the original caracters, but we could have crypted it
    {
      textcrypt += texte.value.charAt(i);
      j++;
    }
  }
  return textcrypt;
}

function uncrypt(texte,key,array)
{
  var textdecrypt ="";
  var j=0;
  var ascisse=0;
  var ordonnee=0;
  for (i=0;i<texte.value.length;i++)
  {
    if (j==key.value.length){j=0;}

    if (charTOascii(texte.value.charAt(i))==32){textdecrypt+=" "} //space char

    else if(charTOascii(texte.value.charAt(i))>=97 && charTOascii(texte.value.charAt(i))<=122) //small caps
    {
      var temp = charTOascii(texte.value.charAt(i))-32; //convert in capital letters
      ordonnee = convert(charTOascii(key.value.charAt(j)));
      for(k=0;k<26;k++)
      {
        if(array[ordonnee][k] == asciiTOchar(temp)) {abscisse=k;}
      }
      temp = array[0][abscisse];
      textdecrypt += asciiTOchar(charTOascii(temp)+32);
      j++;
    }

    else if(charTOascii(texte.value.charAt(i))>=65 && charTOascii(texte.value.charAt(i))<=90) //capital letters, easier !
    {
      ordonnee = convert(charTOascii(key.value.charAt(j)));
      for(k=0;k<26;k++)
      {
        if(array[ordonnee][k] == texte.value.charAt(i)) {abscisse=k;}
      }
      textdecrypt += array[0][abscisse];
      j++;
    }

    else //other cases : we let the original caracters, but we could have crypted it
    {
      textdecrypt+=texte.value.charAt(i);
      j++
    }
  }
  return textdecrypt;
}

function charTOascii(char)
{
  return char.charCodeAt(0);
}

function asciiTOchar(ascii)
{
  return String.fromCharCode(ascii);
}

function encrypt(texte,key) /*main function*/
{

  document.formulaire.textedecrypte.value=crypt(texte,key,array);

  //create histogram :
  tabchar = inittabchar();
  tabchar2 = inittabchar();
  //display histogram:
  displayhisto(tabchar,document.formulaire.texte);
  displayhisto2(tabchar2,document.formulaire.textedecrypte);
}

function decrypt(texte,key) /*main function*/
{

  document.formulaire.texte.value=uncrypt(texte,key,array);
  
  //create histogram :
  tabchar = inittabchar();
  tabchar2 = inittabchar();
  //display histogram:
  displayhisto(tabchar,document.formulaire.texte);
  displayhisto2(tabchar2,document.formulaire.textedecrypte);
}

