From f56ffc4c48061a89a898a182e151a3a9fffc2026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= Date: Fri, 4 Nov 2022 17:48:06 +0100 Subject: [PATCH] test crypte decrypte, permutation, recollage, decale, ... --- src/cryptography/Des.java | 49 ++++---- src/cryptography/temp/testDes.java | 7 +- test/cryptography/DesTest.java | 194 ++++++++++++++++++++--------- 3 files changed, 167 insertions(+), 83 deletions(-) diff --git a/src/cryptography/Des.java b/src/cryptography/Des.java index 0c9934b..99c0746 100644 --- a/src/cryptography/Des.java +++ b/src/cryptography/Des.java @@ -44,13 +44,13 @@ public class Des { }; */ - /* 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}, - {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0}, - {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13} - }; -*/ + /* 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}, + {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0}, + {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13} + }; + */ private static final int[] E = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, @@ -61,15 +61,14 @@ public class Des { 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1, }; - public ArrayList table_cles; private final ArrayList table_S; + public ArrayList table_cles; private int[] masterKey = new int[64]; public Des() { Random random = new Random(); - for (int i = 0; i < masterKey.length; i++) { + for (int i = 0; i < masterKey.length; i++) this.masterKey[i] = random.nextInt(2); - } this.table_cles = new ArrayList<>(); this.table_S = new ArrayList<>(); } @@ -224,25 +223,29 @@ public class Des { } /* Permutations */ - public static void permutation(int[] tab_permutation, int[] bloc) { - int[] newTab = new int[bloc.length]; - for (int i = 0; i < bloc.length; i++) { + public static int[] permutation(int[] tableauPermutation, int[] bloc) { + if (tableauPermutation.length != bloc.length) + throw new IllegalArgumentException("tableauPermutation et bloc n'ont pas la même taille"); - newTab[i] = bloc[tab_permutation[i] % tab_permutation.length]; + int[] resultat = new int[bloc.length]; + for (int i = 0; i < bloc.length; i++) { + resultat[i] = bloc[tableauPermutation[i] % tableauPermutation.length]; } - System.arraycopy(newTab, 0, bloc, 0, newTab.length); + System.arraycopy(resultat, 0, bloc, 0, resultat.length); + return resultat; } - public static void invPermutation(int[] tab_permutation, int[] bloc) { - int[] newTab = new int[bloc.length]; + public static int[] invPermutation(int[] tableauPermutation, int[] bloc) { + if (tableauPermutation.length != bloc.length) + throw new IllegalArgumentException("tableauPermutation et bloc n'ont pas la même taille"); + int[] resultat = new int[bloc.length]; for (int i = 0; i < bloc.length; i++) { - - newTab[tab_permutation[i] % tab_permutation.length] = bloc[i]; + resultat[tableauPermutation[i] % tableauPermutation.length] = bloc[i]; } - - System.arraycopy(newTab, 0, bloc, 0, newTab.length); + System.arraycopy(resultat, 0, bloc, 0, resultat.length); + return resultat; } public static int[] generePermutation(int taille) { @@ -309,7 +312,7 @@ public class Des { } /* Genere */ - public void genereCle(int n) { + public int[][] genereCle(int n) { int[] newCle = new int[56]; int[] lastCle = new int[48]; int[] permInit = generePermutation(newCle.length); @@ -327,6 +330,8 @@ public class Des { System.arraycopy(newCle, 0, lastCle, 0, lastCle.length); permutation(lastPerm, lastCle); this.table_cles.add(lastCle); + + return table_cles.toArray(new int[][]{}); } public void genereS(int n) { diff --git a/src/cryptography/temp/testDes.java b/src/cryptography/temp/testDes.java index 1bb4042..c7f197c 100644 --- a/src/cryptography/temp/testDes.java +++ b/src/cryptography/temp/testDes.java @@ -89,13 +89,12 @@ public class testDes { } public static void testFonctionS() { -// System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}))); + //System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}))); } public static void testFonctionF() { - Des des = new Des(); - -// System.out.println(Arrays.toString(des.fonction_F())); + //Des des = new Des(); + //System.out.println(Arrays.toString(des.fonction_F())); } private static void testCrypte() { diff --git a/test/cryptography/DesTest.java b/test/cryptography/DesTest.java index 3bd2592..62db75d 100644 --- a/test/cryptography/DesTest.java +++ b/test/cryptography/DesTest.java @@ -5,12 +5,15 @@ */ package cryptography; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * @author Gaël BURGUÈS @@ -19,6 +22,51 @@ import static org.junit.jupiter.api.Assertions.assertEquals; */ class DesTest { + private final ArrayList FIXTURE = new ArrayList<>(); + + @BeforeEach + public void init() { + int[][] MASTER_KEYS = {{ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 1, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 1, + }, { + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + 1, 0, 1, 0, 1, 0, 1, 0, + 0, 1, 0, 1, 0, 1, 0, 1, + }, { + 1, 1, 0, 1, 1, 1, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 1, 0, 1, 1, + 1, 1, 1, 0, 1, 0, 1, 1, + 1, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 1, 0, 1, 1, + 0, 0, 1, 1, 1, 0, 1, 1, + }}; + + for (int[] masterKey : MASTER_KEYS) FIXTURE.add(new Des(masterKey)); + } + @Test void stringToBits() { // Chaine vide @@ -297,92 +345,124 @@ class DesTest { @Test void decaleGauche() { - // TODO - System.out.println(Arrays.toString(Des.decaleGauche(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4))); + assertArrayEquals(new int[]{0, 0, 1, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 1)); + assertArrayEquals(new int[]{0, 1, 0, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 2)); + assertArrayEquals(new int[]{0, 0, 0, 1}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 4)); + assertArrayEquals(new int[]{0, 1, 0, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 10)); + + assertArrayEquals( + new int[]{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3}, + Des.decaleGauche(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4) + ); } @Test void generePermutation() { - // TODO - System.out.println(Arrays.toString(Des.generePermutation(64))); + // Longueur de la permuation + assertEquals(64, Des.generePermutation(64).length); + + /* Utilisation d'ensemble pour détecter la présence d'éléments, de doublons, etc... */ + int[] unePermuation = Des.generePermutation(64); + + Set ensemblePermutation = new HashSet<>(); + for (int valeur : unePermuation) ensemblePermutation.add(valeur); + + // Pas de Doublons + assertEquals(ensemblePermutation.size(), unePermuation.length); + + // contiens les éléments de 0 a n-1 (n est la longueur de la liste) + for (int i = 0; i < unePermuation.length; i++) { + assertTrue(ensemblePermutation.contains(i)); + } + + // Ne contiens pas n (la longueur de la liste) + assertFalse(ensemblePermutation.contains(64)); } @Test void permutation() { - // TODO - int[] permutation = Des.generePermutation(64); - int[] bloc = new int[permutation.length]; + // Erreur si les tailles des paramètres ne sont pas les mêmes + assertThrows(IllegalArgumentException.class, () -> Des.permutation(new int[]{1, 2}, new int[]{0, 1, 2, 3, 4, 5})); + assertThrows(IllegalArgumentException.class, () -> Des.permutation(new int[]{0, 1, 2, 3, 4, 5}, new int[]{1, 2})); - for (int i = 0; i < bloc.length; i++) { - bloc[i] = i; - } - System.out.println(Arrays.toString(bloc)); - Des.permutation(permutation, bloc); - System.out.println(Arrays.toString(bloc)); + // Test de la longueur du résultat + assertEquals(5, Des.permutation(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 3, 4}).length); + + // Test de permuation simple + assertArrayEquals(new int[]{1, 5, 3, 4, 2, 0}, Des.permutation( + new int[]{1, 5, 3, 4, 2, 0}, + new int[]{0, 1, 2, 3, 4, 5} + )); + + // Test de permutation complexe + assertArrayEquals(new int[]{2, 0, 1, 5, 4, 3}, Des.permutation( + new int[]{1, 5, 3, 4, 2, 0}, + new int[]{3, 2, 4, 1, 5, 0} + )); } @Test void invPermutation() { - // TODO - int[] permutation = Des.generePermutation(64); - int[] bloc = new int[permutation.length]; - int[] bloc2 = new int[permutation.length]; - for (int i = 0; i < bloc.length; i++) { - bloc[i] = i; - bloc2[i] = i; - } + // Erreur si les tailles des paramètres ne sont pas les mêmes + assertThrows(IllegalArgumentException.class, () -> Des.invPermutation(new int[]{1, 2}, new int[]{0, 1, 2, 3, 4, 5})); + assertThrows(IllegalArgumentException.class, () -> Des.invPermutation(new int[]{0, 1, 2, 3, 4, 5}, new int[]{1, 2})); - System.out.println("Bloc avant permuation: "); - System.out.println(Arrays.toString(bloc)); + // Test de la longueur du résultat + assertEquals(5, Des.invPermutation(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 3, 4}).length); - Des.permutation(permutation, bloc); - System.out.println("Bloc apres permuation: "); - System.out.println(Arrays.toString(bloc)); + // Test de permuation simple + assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, Des.invPermutation( + new int[]{1, 5, 3, 4, 2, 0}, + new int[]{1, 5, 3, 4, 2, 0} + )); - Des.invPermutation(permutation, bloc); - System.out.println("Bloc après Inv-permuation: "); - System.out.println(Arrays.toString(bloc)); - - System.out.println(Arrays.equals(bloc, bloc2)); + // Test de permutation complexe + assertArrayEquals(new int[]{3, 2, 4, 1, 5, 0}, Des.invPermutation( + new int[]{1, 5, 3, 4, 2, 0}, + new int[]{2, 0, 1, 5, 4, 3} + )); } @Test void genereCle() { - // TODO - Des a = new Des(); - a.genereCle(1); - System.out.println(Arrays.toString(a.table_cles.get(0))); + for (Des des : FIXTURE) { + System.out.println(des); + // 1 élément généré + assertEquals(1, des.genereCle(1).length); + + // Premier élément est composé de 48 éléments + assertEquals(48, des.genereCle(1)[0].length); + } } @Test void fonction_S() { // TODO Des des = new Des(); - //System.out.println(Arrays.toString(des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}, 1))); + 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}, + {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0}, + {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13} + }; + + + // assertArrayEquals(, des.fonction_S(...)) + } @Test void fonction_F() { - // TODO - Des des = new Des(); - //System.out.println(Arrays.toString(des.fonction_F(1, ))); + // System.out.println(Arrays.toString(des.fonction_F(1, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}))); } @Test - void crypte() { - // TODO - Des des = new Des(); - int[] msg = des.crypte("Bonjour"); - System.out.println("en bit : " + Arrays.toString(msg)); - System.out.println("en string :" + Des.bitsToString(msg)); - } - - @Test - void decrypte() { - // TODO - Des des = new Des(); - int[] msg = des.crypte("Bonjour, je suis super heureux de vous voir"); - System.out.println("Message crypté : " + Des.bitsToString(msg)); - System.out.println(des.decrypte(msg)); + @DisplayName("crypte() & decrypte()") + void crypteDecrypte() { + for (Des des : FIXTURE) { + String message = "Hello World!"; + int[] messageCrypte = des.crypte(message); + assertEquals(message, des.decrypte(messageCrypte)); + } } } \ No newline at end of file