begin process at 2010 02 10 17:38:19
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Trucs & Astuces

 > CODAGE BASE64

CODAGE BASE64


 Information sur la source

Note :
8,5 / 10 - par 2 personnes
8,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Trucs & Astuces Niveau :Expert Date de création :28/06/2002 Date de mise à jour :28/06/2002 09:46:04 Vu :10 651

Auteur : LleWellA

Ecrire un message privé
Commentaire sur cette source (7)
Ajouter un commentaire et/ou une note

 Description

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

  • 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;
  • }
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;
} 



 Sources du même auteur

Source avec Zip CALENDRIER AVEC LES FERIÉS

 Sources de la même categorie

VERIFIER UNE ADRESSE E-MAIL par elmersaid
DÉTECTER LES VISITEURS EN PROVENANCE DE GOOGLE par elmersaid
EMPÊCHER QUE VOTRE PAGE NE S'AFFICHE PAS DANS UN CADRE (IFRA... par elmersaid
ROLLOVER SUR LES BARRES DE SCROLL par elmersaid
OUTIL DE SÉLECTION par jdmcreator

Commentaires et avis

Commentaire de Dean le 19/07/2002 16:54:45

Ton script est bien pensé mais on ne peut pas dire qu'il s'agisse d'une méthode de cryptage efficace (pas de protection par clé/password).

Commentaire de LleWellA le 19/07/2002 17:01:47

C'est vrai, mais au départ je l'ai fait pour éviter d'avoir des page web en HTPPS et donc j'ai fait ca pour ne pas envoyer des variables en 'clair' dans les POST des formulaires.
Si tu envoi une variable en post, tu peux la coder avant l'envoi.

Commentaire de Francois DERAMAUX le 08/07/2004 11:20:17

Le script fonctionne (thks) mais j'ai du modifier les deux derniers éléments de dtable - Remplacement de '.','_' par '+','/' - pour être compatible avec une librairie propriétaire.
Pour une protection par clé, voir le script de cryptage AES suivant
http://www.cs.ucsd.edu/~fritz/rijndael.js
Cela permet d'enchainer cryptage AES puis codage Base 64, ce que j'ai du réaliser pour échanger avec le partenaire d'un client.

Commentaire de logiciels_thiery le 24/06/2005 17:53:47

Heu... c'est toi qui à écrit ce script ?
C'est du déjà vu par mon Beau-Frère !

Commentaire de LleWellA le 29/06/2005 08:36:21

Alors la mon'ptit gars va faloir apprendre a lire ...
Vu la date a laquelle j'ai déposé ce script, je pari que ton beau-frère a copier depuis cette source. Mais comme je connais pas ton beau-frère je ne vais rien dire !

Vérifie tes sources, ou plutot dit à ton beau-frère de le faire !

Commentaire de logiciels_thiery le 29/06/2005 10:40:58

ok,j'vais lui dire ça, mais il ne connais pas ce site web !

Commentaire de kankrelune le 21/05/2006 21:58:06

Cette source n'encrypte rien puisqu'il suffit de passer la fonction inverse pour avoir la version en clair... l'interet de base64 c'est qu'elle permet de passer des caractères sensible via l'url.. .

Cependant c'est la première source que je trouve qui est compatible avec php permettant ainsi d'encoder des infos et de les décoder coté client avec base64_decode()... et pour ça je met 9/10... .. .

faut juste remplacer le caractère de complément qui est "-" par "="... .. .

@ tchaOo°

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,686 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales