/* Cryptage et decryptage */ public class Cryptage { ///////////////////////////////////////////////// /* Type agrege de stockage */ /* d'une clef de cryptage */ static class ClefDeCryptage { char [] cn = new char[256]; }; /* Fonction de cryptage d'un tableau */ /* de caracteres en un nouveau tableau */ /* de caracteres retourné */ /* t : Le tableau de caracteres à crypter */ /* cdc : La clef de cryptage à utiliser */ static char [] cryptage(char [] t,ClefDeCryptage cdc) { int i; char [] tc = new char[t.length]; for ( i = 0 ; i < tc.length ; i++ ) { tc[i] = cdc.cn[t[i]]; } return(tc); } /* Fonction de calcul et retour de la clef */ /* de cryptage inverse d'une clef de cryptage: */ /* la clef utilisable pour décrypter */ /* en effectuant une opération de cryptage */ /* Retour de la clef obtenue */ /* cdc : La clef de cryptage à inverser */ static ClefDeCryptage clefDeDecryptage(ClefDeCryptage cdc) { ClefDeCryptage cddc = new ClefDeCryptage(); int i; for ( i = 0 ; i < 256 ; i++ ) cddc.cn[cdc.cn[i]] =(char) i; return(cddc); } /* Fonction de decryptage d'un tableau */ /* de caracteres en un nouveau tableau */ /* de caracteres retourné */ /* t : Le tableau de caracteres à decrypter */ /* cdc : La clef de cryptage utilisée */ /* pour le cryptage initial */ static char [] decryptage(char [] t,ClefDeCryptage cdc) { ClefDeCryptage cddc = clefDeDecryptage(cdc); char [] tc = new char[t.length]; int i; for ( i = 0 ; i < tc.length ; i++ ) { tc[i] = cddc.cn[t[i]]; } return(tc); } ///////////////////////////////////////////////// static void afficher(char [] t) { int i; for ( i = 0 ; i < t.length ; i = i+1 ) { Ecran.afficher(t[i]); } } /* Programme principal */ public static void main(String [] args) { int i; char aux; int i1; int i2; char [] t; char [] tc; char [] tcdc; ClefDeCryptage cdc = new ClefDeCryptage(); String s = "Un texte est stocké dans un tableau de caractères."; for ( i = 0 ; i < 256 ; i = i+1 ) { cdc.cn[i] =(char) i; } for ( i = 0 ; i < 1000 ; i++ ) { i1 =(int) (Math.random()*256); i2 =(int) (Math.random()*256); aux = cdc.cn[i1]; cdc.cn[i1] = cdc.cn[i2]; cdc.cn[i2] = aux; } t = s.toCharArray(); tc = cryptage(t,cdc); tcdc = decryptage(tc,cdc); Ecran.afficherln("Chaine avant cryptage"); afficher(t); Ecran.sautDeLigne(); Ecran.afficherln("Chaine après cryptage"); afficher(tc); Ecran.sautDeLigne(); Ecran.afficherln("Chaine cryptée après décryptage"); afficher(tcdc); Ecran.sautDeLigne(); } }