From 55e349957969f2ebd6dce52ab53723505a8ef32e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= Date: Tue, 25 Oct 2022 15:14:01 +0200 Subject: [PATCH] 25/10/2022 Decrypte/Crypte ne marche pas --- src/cryptography/Des.java | 119 ++++++++++++++++++++--------- src/cryptography/test/testDes.java | 11 ++- 2 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/cryptography/Des.java b/src/cryptography/Des.java index 3f0ad68..bc95fe9 100644 --- a/src/cryptography/Des.java +++ b/src/cryptography/Des.java @@ -7,11 +7,12 @@ package cryptography; import java.math.BigInteger; import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; /** - * @author Lucàs VABRE + * @author Lucàs VABRE et les 2 autres */ public class Des { @@ -19,7 +20,16 @@ public class Des { private static final int TAILLE_SOUS_BLOC = 32; private static final int NB_ROUND = 1; private static final int[] TAB_DECALAGE = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}; - private static final int[] PERM_INITIALE = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7}; + private static final int[] PERM_INITIALE = { + 58, 50, 42, 34, 26, 18, 10, 2, + 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, + 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, + 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, + 63, 55, 47, 39, 31, 23, 15, 7 + }; 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}, @@ -86,9 +96,8 @@ 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]]; + newTab[i] = bloc[tab_permutation[i] % tab_permutation.length]; } System.arraycopy(newTab, 0, bloc, 0, newTab.length); @@ -97,29 +106,36 @@ public class Des { public static void invPermuation(int[] tab_permutation, int[] bloc) { int[] newTab = new int[bloc.length]; + //// System.out.println(bloc.length); for (int i = 0; i < bloc.length; i++) { - newTab[tab_permutation[i]] = bloc[i]; + //// System.out.println(tab_permutation[i] % tab_permutation.length); + + newTab[tab_permutation[i] % tab_permutation.length] = bloc[i]; } System.arraycopy(newTab, 0, bloc, 0, newTab.length); } public static int[][] decoupage(int[] bloc, int nbBlocs) { - - if (bloc.length % nbBlocs == 0) { - int z = bloc.length / nbBlocs; - int[][] newTab = new int[nbBlocs][z]; - int y = 0; - - for (int i = 0; i < bloc.length; i++) { - if (i % z == 0 && i > 0) { - y++; - } - newTab[y][i - y * z] = bloc[i]; - } - return newTab; + int surplu = bloc.length % nbBlocs; + int z = (bloc.length) / nbBlocs; + if (surplu != 0) { + z = (bloc.length + (nbBlocs - surplu)) / nbBlocs; } - return null; + + int[][] newTab = new int[nbBlocs][z]; + + // Remplis le tableau de 0 + for (int[] b : newTab) Arrays.fill(b, 0); + + int y = 0; + for (int i = 0; i < bloc.length; i++) { + if (i % z == 0 && i > 0) { + y++; + } + newTab[y][i - y * z] = bloc[i]; + } + return newTab; } public static int[] recollageBloc(int[][] blocs) { @@ -148,6 +164,7 @@ public class Des { public static int[] xor(int[] tab1, int[] tab2) { int[] resultat = new int[tab1.length]; + // System.out.println(resultat.length); for (int i = 0; i < resultat.length; i++) { resultat[i] = (tab1[i] + tab2[i]) % 2; @@ -196,13 +213,17 @@ public class Des { return resultat; } - public int[] fonction_F(int[] unCle) { + public int[] fonction_F(int indice_cle, int[] Dn) { //xor entre this.E et la cle trouvé à cette ronde - // this.genereCle(this.table_cles.size()); - int[] Dn = xor(E, unCle); + 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[] Dn_prime = xor(cle, newDn); //découpage en 8 blocs de 6 bits - int[][] decoupe = decoupage(Dn, 8); + int[][] decoupe = decoupage(Dn_prime, 8); int[][] bloc = new int[8][4]; for (int i = 0; i < decoupe.length; i++) { @@ -216,32 +237,62 @@ public class Des { public int[] crypte(String message_clair) { int[] msg_bit = stringToBits(message_clair); - int[][] decoupe = decoupage(msg_bit, (int) msg_bit.length / 64); - int[][] msg_crypte_bit = new int[(int)msg_bit.length / 64][64]; + // // 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; - for (int n = 0; n < 16; n++) { + int[] msgBit = new int[taille]; + Arrays.fill(msgBit, 0); + + // boucle du XOR + for (int y = 0; y < msg_bit.length; y++) { + msgBit[y] = (msgBit[y] + msg_bit[y]) % 2; + } + + 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]; + + //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++) { - permutation(PERM_INITIALE, decoupe[i]); + int[][] decoupe2 = decoupage(decoupe[i], 2); for (int n = 0; n < 16; n++) { - // this.genereCle(n); - decoupe2[1] = fonction_F(this.table_cles.get(n)); - decoupe2[0] = fonction_F(this.table_cles.get(n)); + int[] Gn1 = decoupe2[1]; + int[] F = fonction_F(i, decoupe2[1]); + decoupe2[1] = xor(decoupe2[0], F); + decoupe2[0] = Gn1; } msg_crypte_bit[i] = recollageBloc(decoupe2); + invPermuation(PERM_INITIALE, msg_crypte_bit[i]); } + return recollageBloc(msg_crypte_bit); } - public static String decrypte(int[] messageCode) { - return null; + 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)); + bloc32[1] = dn1; + } + decoupe[i] = recollageBloc(bloc32); + invPermuation(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 4a5984e..e279a4d 100644 --- a/src/cryptography/test/testDes.java +++ b/src/cryptography/test/testDes.java @@ -90,8 +90,15 @@ public class testDes { 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(Arrays.toString(des.crypte("Hello World!"))); + private static void testDecrypte() { + Des des = new Des(); + int[] msg = des.crypte("Bonjour"); + System.out.println(des.decrypte(msg));; } public static void main(String[] args) { @@ -107,7 +114,7 @@ public class testDes { // testGenereCle(); // testFonctionS(); // testFonctionF(); - testCrypte(); + testCrypte(); // testDecrypte(); }