diff --git a/src/cryptography/Des.java b/src/cryptography/Des.java index bc95fe9..d8ae98a 100644 --- a/src/cryptography/Des.java +++ b/src/cryptography/Des.java @@ -30,6 +30,19 @@ public class Des { 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; + + /* private static final int[] INV_PERM_INITIALE = { + 40,8,48,16,56,24,64,32, + 39,7,47,15,55,23,63,31, + 38,6,46,14,54,22,62,30, + 37,5,45,13,53,21,61,29, + 36,4,44,12,52,20,60,28, + 35,3,43,11,51,19,59,27, + 34,2,42,10,50,18,58,26, + 33,1,41,9,49,17,57,25 + }; +*/ + private static final int[][] S = { {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7}, {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8}, @@ -80,7 +93,7 @@ public class Des { Random r = new Random(); for (int i = taille; i > 0; i--) { - listePermut[taille - i] = listeIndice.remove(r.nextInt(0, i)); + listePermut[taille - i] = listeIndice.remove(r.nextInt(i)); } return listePermut; @@ -97,13 +110,14 @@ public class Des { public static void permutation(int[] tab_permutation, int[] bloc) { int[] newTab = new int[bloc.length]; for (int i = 0; i < bloc.length; i++) { + newTab[i] = bloc[tab_permutation[i] % tab_permutation.length]; } System.arraycopy(newTab, 0, bloc, 0, newTab.length); } - public static void invPermuation(int[] tab_permutation, int[] bloc) { + public static void invPermutation(int[] tab_permutation, int[] bloc) { int[] newTab = new int[bloc.length]; //// System.out.println(bloc.length); @@ -139,6 +153,7 @@ public class Des { } public static int[] recollageBloc(int[][] blocs) { + int[] bloc = new int[blocs.length * blocs[0].length]; int y = 0; for (int[] ints : blocs) { @@ -207,7 +222,7 @@ public class Des { int[] resultat = new int[4]; for (int i = 0; i < resultat.length; i++, coordonneeInt /= 10) { - resultat[i] = coordonneeInt % 10; + resultat[resultat.length-i-1] = coordonneeInt % 10; } return resultat; @@ -216,29 +231,26 @@ public class Des { public int[] fonction_F(int indice_cle, int[] Dn) { //xor entre this.E et la cle trouvé à cette ronde int[] cle = this.table_cles.get(indice_cle); - int[] newDn = new int[48]; - for (int i = 0; i < this.E.length; i++) { - newDn[i] = Dn[this.E[i] % Dn.length]; + int[] d_prime_n = new int[48]; + for (int i = 0; i < E.length; i++) { + d_prime_n[i] = Dn[E[i] % Dn.length]; } - int[] Dn_prime = xor(cle, newDn); + int[] D_etoile_n = xor(cle, d_prime_n); //découpage en 8 blocs de 6 bits - int[][] decoupe = decoupage(Dn_prime, 8); + int[][] decoupe = decoupage(D_etoile_n, 8); int[][] bloc = new int[8][4]; for (int i = 0; i < decoupe.length; i++) { int[] tab = fonction_S(decoupe[i]); System.arraycopy(tab, 0, bloc[i], 0, tab.length); } - return recollageBloc(bloc); } public int[] crypte(String message_clair) { int[] msg_bit = stringToBits(message_clair); - // // System.out.println(Arrays.toString(msg_bit)); - // // System.out.println(msg_bit.length); int taille = (int) Math.ceil(msg_bit.length / (float) TAILLE_BLOC) * 64; int[] msgBit = new int[taille]; @@ -251,45 +263,57 @@ public class Des { int[][] decoupe = decoupage(msgBit, (int) Math.ceil(msg_bit.length / (float) TAILLE_BLOC)); - int[][] msg_crypte_bit = new int[msg_bit.length / TAILLE_BLOC][TAILLE_BLOC]; + + int[][] msg_crypte_bit = new int[(int) Math.ceil(msg_bit.length / (float) TAILLE_BLOC)][TAILLE_BLOC]; + //Boucle de génération 16 des clés for (int n = 0; n < (TAILLE_BLOC / 4); n++) { this.genereCle(n); } - for (int i = 0; i < decoupe.length - 1; i++) { + for (int i = 0; i < decoupe.length ; i++) { permutation(PERM_INITIALE, decoupe[i]); int[][] decoupe2 = decoupage(decoupe[i], 2); + for (int n = 0; n < 16; n++) { - int[] Gn1 = decoupe2[1]; - int[] F = fonction_F(i, decoupe2[1]); + int[] Gn1 = new int[decoupe2[1].length]; + System.arraycopy(decoupe2[1], 0, Gn1, 0, Gn1.length); + int[] F = fonction_F(n, decoupe2[1]); decoupe2[1] = xor(decoupe2[0], F); decoupe2[0] = Gn1; } - msg_crypte_bit[i] = recollageBloc(decoupe2); - invPermuation(PERM_INITIALE, msg_crypte_bit[i]); - } + msg_crypte_bit[i] = recollageBloc(decoupe2); + invPermutation(PERM_INITIALE, msg_crypte_bit[i]); + } return recollageBloc(msg_crypte_bit); } public String decrypte(int[] messageCode) { + int[][] decoupe = decoupage(messageCode, (int) (messageCode.length / TAILLE_BLOC)); for (int i = 0; i < decoupe.length; i++) { permutation(PERM_INITIALE, decoupe[i]); int[][] bloc32 = decoupage(decoupe[i], 2); + for (int n = 15; n >= 0; n--) { - int[] dn1 = bloc32[0]; - bloc32[0] = xor(bloc32[1], fonction_F(i, dn1)); + int[] dn1 = new int[bloc32[0].length]; + System.out.println("clé de chiffrement utilisé :" + Arrays.toString(this.table_cles.get(n))); + System.arraycopy(bloc32[0], 0, dn1, 0, dn1.length); + bloc32[0] = xor(bloc32[1], fonction_F(n, dn1)); bloc32[1] = dn1; + System.out.println("bloc32[0] :"+ Arrays.toString(bloc32[0])); + System.out.println("bloc32[1] :"+ Arrays.toString(bloc32[1])); + } decoupe[i] = recollageBloc(bloc32); - invPermuation(PERM_INITIALE, decoupe[i]); + invPermutation(PERM_INITIALE, decoupe[i]); + } int[] message_decrypte = recollageBloc(decoupe); return bitsToString(message_decrypte); diff --git a/src/cryptography/test/testDes.java b/src/cryptography/test/testDes.java index e279a4d..bb81018 100644 --- a/src/cryptography/test/testDes.java +++ b/src/cryptography/test/testDes.java @@ -7,7 +7,7 @@ import java.util.Arrays; public class testDes { public static void testStringToBits() { - System.out.println(Arrays.toString(Des.stringToBits("13"))); + System.out.println(Arrays.toString(Des.stringToBits("Bonjour"))); } public static void testBitsToString() { @@ -46,7 +46,7 @@ public class testDes { System.out.println("Bloc apres permuation: "); System.out.println(Arrays.toString(bloc)); - Des.invPermuation(permutation, bloc); + Des.invPermutation(permutation, bloc); System.out.println("Bloc après Inv-permuation: "); System.out.println(Arrays.toString(bloc)); @@ -54,11 +54,11 @@ public class testDes { } public static void testDecoupage() { - System.out.println(Arrays.deepToString(Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4))); + System.out.println(Arrays.deepToString(Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 7))); } public static void testRecollage() { - int[][] decoupage = Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4); + int[][] decoupage = Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 7); System.out.println(Arrays.deepToString(decoupage)); assert decoupage != null; @@ -85,37 +85,39 @@ public class testDes { public static void testFonctionF() { Des des = new Des(); - // System.out.println(Arrays.toString(des.fonction_F())); +// System.out.println(Arrays.toString(des.fonction_F())); } private static void testCrypte() { Des des = new Des(); int [] msg = des.crypte("Bonjour"); - System.out.println(Arrays.toString(msg)); - System.out.println(Des.bitsToString(msg)); + System.out.println("en bit : " +Arrays.toString(msg)); + System.out.println("en string :"+Des.bitsToString(msg)); } private static void testDecrypte() { Des des = new Des(); int[] msg = des.crypte("Bonjour"); + System.out.println("Message crypté : " + Des.bitsToString(msg)); System.out.println(des.decrypte(msg));; } public static void main(String[] args) { - // testStringToBits(); - // testBitsToString(); - // testGenerePermutation(); - // testPermuation(); - // testInvPermuation(); - // testDecoupage(); - // testRecollage(); - // testDecaleGauche(); - // testXor(); - // testGenereCle(); - // testFonctionS(); - // testFonctionF(); - testCrypte(); - // testDecrypte(); +// testStringToBits(); //Il marche +// testBitsToString();//Il marche +// testGenerePermutation();//Il marche +// testPermuation();//Il marche +// testInvPermuation();//Il marche +// testDecoupage();//Il marche (même avec un nombre de bloc qui divise pas la taille du bloc de base) +// testRecollage();//Il marche +// testDecaleGauche();//Il marche +// testXor();//Il marche +// testGenereCle();// Aucune idée, ça créé une clé random mais je sais pas si c'est comme ça +// testFonctionS();// corrigé +// testFonctionF(); // il a l'air bon +// testCrypte(); //Il a l'air bon (si on regarde decoupe[0]et [1] à chaque itérations on voit que c'est cohérent) + testDecrypte(); // Un truc est normal c'est que jusqu'à la deniere itération sur les 16 , + // les clés sont randoms jusqu'à la dernière itération ou elle est tout le temps la meme } }