Codage base64

Contenu du snippet

Un petit code qui permets de Coder ou Décoder chez le client ( avant l'envoi de données) des parametres qui pourraient etre classés secret défense ou moins si on veux ...
Enfin, les deux fonctions sont simples d'utilisation :
MaPhraseCode = enCode(leTexte);
MaPhraseDecode = deCode(leTexte);
bon usage a tous ...

Source / Exemple :


var dtable = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','.','_');

function inchar(d) {
  r = -1;
  for (var i = 0; i < dtable.length; i++) {
    if (d == dtable[i]) {
      r = i;
      break;
    }
  }
  return r;
}

function Trouve(n) {
while (n > 256) { n -= 256; }
return n
}

function enCode(n) {
  var o1 = o2 = o3 =o4 = 0;
  var text = "";
  j = 0;
  for (var i = 0; i < n.length; i += 3) {
    t = Math.min(3, n.length - i); 
    if (t == 1) {
       x = n.charCodeAt(i); 
       text += dtable[(x >> 2)]; 
       text += dtable[((x & 0X00000003) << 4)]; 
       text += '-'; 
       text += '-'; 
    } else if (t == 2) {
       x = n.charCodeAt(i); 
       y = n.charCodeAt(i+1); 
       text += dtable[(x >> 2)]; 
       text += dtable[((x & 0X00000003) << 4) | (y >> 4)]; 
       text += dtable[((y & 0X0000000f) << 2)]; 
       text += '-'; 
    } else {
       x = n.charCodeAt(i); 
       y = n.charCodeAt(i+1); 
       z = n.charCodeAt(i+2); 
       text += dtable[x >> 2]; 
       text += dtable[((x & 0x00000003) << 4) | (y >> 4)]; 
       text += dtable[((y & 0X0000000f) << 2) | (z >> 6)]; 
       text += dtable[z & 0X0000003f]; 
    }
  }
  return text; 
}

function deCode(n) {
  var p;
  var o1 = o2 = o3 = 0;
  var text = "";
  if ((n.length % 4) != 0) { return null; }
  j = 0;
  for (var i = 0; i < n.length; i += 4) {
    x1 = inchar(n.charAt(i));
    x2 = inchar(n.charAt(i+1));
    x3 = inchar(n.charAt(i+2));
    x4 = inchar(n.charAt(i+3));
    ol = 4;
    if (x4 == -1) { ol--; x4 = 0;}
    if (x3 == -1) { ol--; x3 = 0;}
    if (ol == 4) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
      o2 = ((x2 << 4) | (x3 >> 2)); ((o2 > 256) ? p=Trouve(o2) : p=o2) ; text += String.fromCharCode(p);
      o3 = ((x3 << 6) | x4); ((o3 > 256) ? p=Trouve(o3) : p=o3) ; text += String.fromCharCode(p);
    } else if (ol == 3) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
      o2 = ((x2 << 4) | (x3 >> 2)); ((o2 > 256) ? p=Trouve(o2) : p=o2); text += String.fromCharCode(p);
          } else if (ol == 2) {
      o1 = ((x1 << 2) | (x2 >> 4)); ((o1 > 256) ? p=Trouve(o1) : p=o1) ; text += String.fromCharCode(p);
    }
  }
  return text;
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.