Decoupage/Recollage

This commit is contained in:
Lucàs
2022-10-27 22:18:30 +02:00
parent 8b594bfe84
commit 51a98e486f
4 changed files with 127 additions and 62 deletions
+22 -24
View File
@@ -17,7 +17,7 @@ import java.util.Random;
*/ */
public class Des { public class Des {
private static final int TAILLE_BLOC = 64; public static final int TAILLE_BLOC = 64;
private static final int TAILLE_SOUS_BLOC = 32; private static final int TAILLE_SOUS_BLOC = 32;
private static final int NB_ROUND = 1; 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[] TAB_DECALAGE = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
@@ -51,9 +51,6 @@ public class Des {
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13} {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
}; };
*/ */
private ArrayList<int[][]> table_S;
private static final int[] E = { private static final int[] E = {
32, 1, 2, 3, 4, 5, 32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9,
@@ -65,6 +62,7 @@ public class Des {
28, 29, 30, 31, 32, 1, 28, 29, 30, 31, 32, 1,
}; };
public ArrayList<int[]> table_cles; public ArrayList<int[]> table_cles;
private final ArrayList<int[][]> table_S;
private int[] masterKey = new int[64]; private int[] masterKey = new int[64];
public Des() { public Des() {
@@ -264,26 +262,6 @@ public class Des {
return listePermut; return listePermut;
} }
/* Fonctions */
public int[] fonction_S(int[] tab, int index_S) {
String l = "" + tab[0] + tab[5];
int ligne = Integer.parseInt(l, 2);
String c = "" + tab[1] + tab[2] + tab[3] + tab[4];
int colonne = Integer.parseInt(c, 2);
// chaque blocs de 6 bits on fait le truc avec bit 1 et bit 6 et l'autre truc pour avoir ligne et colonne de S
String coordonneeStr = Integer.toString(this.table_S.get(index_S)[ligne][colonne], 2);
int coordonneeInt = Integer.parseInt(coordonneeStr);
int[] resultat = new int[4];
for (int i = 0; i < resultat.length; i++, coordonneeInt /= 10) {
resultat[resultat.length - i - 1] = coordonneeInt % 10;
}
return resultat;
}
/** /**
* Remove the last character if it contains only zeros * Remove the last character if it contains only zeros
* *
@@ -310,6 +288,26 @@ public class Des {
return msg_decrypt.stream().mapToInt(i -> i).toArray(); return msg_decrypt.stream().mapToInt(i -> i).toArray();
} }
/* Fonctions */
public int[] fonction_S(int[] tab, int index_S) {
String l = "" + tab[0] + tab[5];
int ligne = Integer.parseInt(l, 2);
String c = "" + tab[1] + tab[2] + tab[3] + tab[4];
int colonne = Integer.parseInt(c, 2);
// chaque blocs de 6 bits on fait le truc avec bit 1 et bit 6 et l'autre truc pour avoir ligne et colonne de S
String coordonneeStr = Integer.toString(this.table_S.get(index_S)[ligne][colonne], 2);
int coordonneeInt = Integer.parseInt(coordonneeStr);
int[] resultat = new int[4];
for (int i = 0; i < resultat.length; i++, coordonneeInt /= 10) {
resultat[resultat.length - i - 1] = coordonneeInt % 10;
}
return resultat;
}
/* Genere */ /* Genere */
public void genereCle(int n) { public void genereCle(int n) {
int[] newCle = new int[56]; int[] newCle = new int[56];
+18 -2
View File
@@ -2,10 +2,26 @@ package cryptography;
public class TripleDes { public class TripleDes {
private final Des[] listeDes; private static final int NB_DES = 3;
private final Des[] listeDes = new Des[NB_DES];
public TripleDes() { public TripleDes() {
this.listeDes = new Des[]{new Des(), new Des(), new Des(),}; for (int i = 0; i < NB_DES; i++) {
listeDes[i] = new Des();
}
}
public TripleDes(int[][] masterKeys) {
if (masterKeys.length != NB_DES)
throw new IllegalArgumentException("masterKeys parameter should have 3 elements");
for (int[] masterKey : masterKeys) {
if (masterKey.length != Des.TAILLE_BLOC)
throw new IllegalArgumentException("Each elements in masterKeys should have 64 bits");
}
for (int i = 0; i < NB_DES; i++) {
listeDes[i] = new Des(masterKeys[i]);
}
} }
public int[] crypte(String messageClair) { public int[] crypte(String messageClair) {
+60 -23
View File
@@ -322,66 +322,103 @@ class DesTest {
@Test @Test
@DisplayName("decoupage") @DisplayName("decoupage")
void decoupage() { void decoupage() {
fail(); // TODO HashMap<int[][], int[][]> TESTS = new HashMap<>() {{
// TODO add more tests
put(
new int[][]{
new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // bloc
new int[]{7} // nbBloc
},
new int[][]{ // expected result
new int[]{0, 1, 2},
new int[]{3, 4, 5},
new int[]{6, 7, 8},
new int[]{9, 10, 11},
new int[]{12, 13, 14},
new int[]{15, 16, 17},
new int[]{18, 19, 0}
} }
);
}};
@Test for (Map.Entry<int[][], int[][]> test : TESTS.entrySet()) {
@DisplayName("generePermutation") assertArrayEquals(test.getValue(), Des.decoupage(test.getKey()[0], test.getKey()[1][0]));
void generePermutation() { System.out.printf("(%s, %s) -> OK\n", Arrays.toString(test.getKey()[0]), test.getKey()[1][0]);
fail(); // TODO
} }
@Test
@DisplayName("permutation")
void permutation() {
fail(); // TODO
}
@Test
@DisplayName("invPermutation")
void invPermutation() {
fail(); // TODO
} }
@Test @Test
@DisplayName("recollageBloc") @DisplayName("recollageBloc")
void recollageBloc() { void recollageBloc() {
fail(); // TODO HashMap<int[][], int[]> TESTS = new HashMap<>() {{
// TODO add more tests
put(
new int[][]{
new int[]{1, 2, 3, 4, 5},
new int[]{6, 7, 8, 9, 10},
},
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
);
}};
for (Map.Entry<int[][], int[]> test : TESTS.entrySet()) {
assertArrayEquals(test.getValue(), Des.recollageBloc(test.getKey()));
System.out.printf("%s -> OK\n", Arrays.deepToString(test.getKey()));
}
} }
@Test @Test
@DisplayName("decaleGauche") @DisplayName("decaleGauche")
void decaleGauche() { void decaleGauche() {
fail(); // TODO fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("generePermutation")
void generePermutation() {
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("permutation")
void permutation() {
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("invPermutation")
void invPermutation() {
fail("Not yet implemented"); // TODO
} }
@Test @Test
@DisplayName("genereCle") @DisplayName("genereCle")
void genereCle() { void genereCle() {
fail(); // TODO fail("Not yet implemented"); // TODO
} }
@Test @Test
@DisplayName("fonction_S") @DisplayName("fonction_S")
void fonction_S() { void fonction_S() {
fail(); // TODO fail("Not yet implemented"); // TODO
} }
@Test @Test
@DisplayName("fonction_F") @DisplayName("fonction_F")
void fonction_F() { void fonction_F() {
fail(); // TODO fail("Not yet implemented"); // TODO
} }
@Test @Test
@DisplayName("crypte") @DisplayName("crypte")
void crypte() { void crypte() {
fail(); // TODO fail("Not yet implemented"); // TODO
} }
@Test @Test
@DisplayName("decrypte") @DisplayName("decrypte")
void decrypte() { void decrypte() {
fail(); // TODO fail("Not yet implemented"); // TODO
} }
} }
+14
View File
@@ -0,0 +1,14 @@
package cryptography;
import org.junit.jupiter.api.Test;
class TripleDesTest {
@Test
void crypte() {
}
@Test
void decrypte() {
}
}