Update DesTest.java

This commit is contained in:
Lucàs
2022-11-04 15:37:12 +01:00
parent 51a98e486f
commit 46b6849c93
+119 -155
View File
@@ -5,14 +5,12 @@
*/
package cryptography;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Gaël BURGUÈS
@@ -22,14 +20,12 @@ import static org.junit.jupiter.api.Assertions.*;
class DesTest {
@Test
@DisplayName("stringToBits")
void stringToBits() {
HashMap<String, int[]> TESTS = new HashMap<>() {{
// Empty string
put("", new int[]{});
// Chaine vide
assertArrayEquals(new int[]{}, Des.stringToBits(""));
// abc, maj
put("abcdefghijklmnopqrstuvwxyz", new int[]{
// Minuscules
assertArrayEquals(new int[]{
0, 1, 1, 0, 0, 0, 0, 1, // a
0, 1, 1, 0, 0, 0, 1, 0, // b
0, 1, 1, 0, 0, 0, 1, 1, // c
@@ -56,9 +52,10 @@ class DesTest {
0, 1, 1, 1, 1, 0, 0, 0, // x
0, 1, 1, 1, 1, 0, 0, 1, // y
0, 1, 1, 1, 1, 0, 1, 0, // z
});
}, Des.stringToBits("abcdefghijklmnopqrstuvwxyz"));
put("ABCDEFGHIJKLMNOPQRSTUVWXYZ", new int[]{
// Majuscules
assertArrayEquals(new int[]{
0, 1, 0, 0, 0, 0, 0, 1, // A
0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 0, 0, 0, 0, 1, 1, // C
@@ -85,10 +82,10 @@ class DesTest {
0, 1, 0, 1, 1, 0, 0, 0, // X
0, 1, 0, 1, 1, 0, 0, 1, // Y
0, 1, 0, 1, 1, 0, 1, 0, // Z
});
}, Des.stringToBits("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
// Numbers
put("0123456789", new int[]{
// Chiffres
assertArrayEquals(new int[]{
0, 0, 1, 1, 0, 0, 0, 0, // 0
0, 0, 1, 1, 0, 0, 0, 1, // 1
0, 0, 1, 1, 0, 0, 1, 0, // 2
@@ -99,19 +96,20 @@ class DesTest {
0, 0, 1, 1, 0, 1, 1, 1, // 7
0, 0, 1, 1, 1, 0, 0, 0, // 8
0, 0, 1, 1, 1, 0, 0, 1, // 9
});
}, Des.stringToBits("0123456789"));
// Special chars
put("+-/*%=", new int[]{
// Symboles mathématiques
assertArrayEquals(new int[]{
0, 0, 1, 0, 1, 0, 1, 1, // +
0, 0, 1, 0, 1, 1, 0, 1, // -
0, 0, 1, 0, 1, 1, 1, 1, // /
0, 0, 1, 0, 1, 0, 1, 0, // *
0, 0, 1, 0, 0, 1, 0, 1, // %
0, 0, 1, 1, 1, 1, 0, 1, // =
});
}, Des.stringToBits("+-/*%="));
put("?!.,;:_ ", new int[]{
// Ponctuations
assertArrayEquals(new int[]{
0, 0, 1, 1, 1, 1, 1, 1, // ?
0, 0, 1, 0, 0, 0, 0, 1, // !
0, 0, 1, 0, 1, 1, 1, 0, // .
@@ -120,18 +118,20 @@ class DesTest {
0, 0, 1, 1, 1, 0, 1, 0, // :
0, 1, 0, 1, 1, 1, 1, 1, // _
0, 0, 1, 0, 0, 0, 0, 0, // space
});
}, Des.stringToBits("?!.,;:_ "));
put("(){}[]", new int[]{
// Parenthèses
assertArrayEquals(new int[]{
0, 0, 1, 0, 1, 0, 0, 0, // (
0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 1, 1, 1, 0, 1, 1, // {
0, 1, 1, 1, 1, 1, 0, 1, // }
0, 1, 0, 1, 1, 0, 1, 1, // [
0, 1, 0, 1, 1, 1, 0, 1, // ]
});
}, Des.stringToBits("(){}[]"));
put("&@#^\"'`", new int[]{
// Caractères spéciaux
assertArrayEquals(new int[]{
0, 0, 1, 0, 0, 1, 1, 0, // &
0, 1, 0, 0, 0, 0, 0, 0, // @
0, 0, 1, 0, 0, 0, 1, 1, // #
@@ -139,35 +139,17 @@ class DesTest {
0, 0, 1, 0, 0, 0, 1, 0, // "
0, 0, 1, 0, 0, 1, 1, 1, // '
0, 1, 1, 0, 0, 0, 0, 0, // `
});
// Custom test
put("Bonjour", new int[]{
0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 0, 1, 1, 1, 0, // n
0, 1, 1, 0, 1, 0, 1, 0, // j
0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 1, 0, 1, 0, 1, // u
0, 1, 1, 1, 0, 0, 1, 0, // r
});
}};
for (Map.Entry<String, int[]> test : TESTS.entrySet()) {
assertArrayEquals(test.getValue(), Des.stringToBits(test.getKey()));
System.out.printf("\"%s\" -> OK\n", test.getKey());
}
}, Des.stringToBits("&@#^\"'`"));
}
@Test
@DisplayName("stringToBits")
void bitsToString() {
HashMap<int[], String> TESTS = new HashMap<>() {{
// Empty string
put(new int[]{}, "");
// abc, maj
put(new int[]{
// Chaine vide
assertEquals("", Des.bitsToString(new int[]{}));
// Minuscules
assertEquals("abcdefghijklmnopqrstuvwxyz", Des.bitsToString(new int[]{
0, 1, 1, 0, 0, 0, 0, 1, // a
0, 1, 1, 0, 0, 0, 1, 0, // b
0, 1, 1, 0, 0, 0, 1, 1, // c
@@ -194,9 +176,10 @@ class DesTest {
0, 1, 1, 1, 1, 0, 0, 0, // x
0, 1, 1, 1, 1, 0, 0, 1, // y
0, 1, 1, 1, 1, 0, 1, 0, // z
}, "abcdefghijklmnopqrstuvwxyz");
}));
put(new int[]{
// Majuscules
assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Des.bitsToString(new int[]{
0, 1, 0, 0, 0, 0, 0, 1, // A
0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 0, 0, 0, 0, 1, 1, // C
@@ -223,10 +206,10 @@ class DesTest {
0, 1, 0, 1, 1, 0, 0, 0, // X
0, 1, 0, 1, 1, 0, 0, 1, // Y
0, 1, 0, 1, 1, 0, 1, 0, // Z
}, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}));
// Numbers
put(new int[]{
// Chiffres
assertEquals("0123456789", Des.bitsToString(new int[]{
0, 0, 1, 1, 0, 0, 0, 0, // 0
0, 0, 1, 1, 0, 0, 0, 1, // 1
0, 0, 1, 1, 0, 0, 1, 0, // 2
@@ -237,19 +220,20 @@ class DesTest {
0, 0, 1, 1, 0, 1, 1, 1, // 7
0, 0, 1, 1, 1, 0, 0, 0, // 8
0, 0, 1, 1, 1, 0, 0, 1, // 9
}, "0123456789");
}));
// Special chars
put(new int[]{
// Symboles mathématiques
assertEquals("+-/*%=", Des.bitsToString(new int[]{
0, 0, 1, 0, 1, 0, 1, 1, // +
0, 0, 1, 0, 1, 1, 0, 1, // -
0, 0, 1, 0, 1, 1, 1, 1, // /
0, 0, 1, 0, 1, 0, 1, 0, // *
0, 0, 1, 0, 0, 1, 0, 1, // %
0, 0, 1, 1, 1, 1, 0, 1, // =
}, "+-/*%=");
}));
put(new int[]{
// Ponctuations
assertEquals("?!.,;:_ ", Des.bitsToString(new int[]{
0, 0, 1, 1, 1, 1, 1, 1, // ?
0, 0, 1, 0, 0, 0, 0, 1, // !
0, 0, 1, 0, 1, 1, 1, 0, // .
@@ -258,18 +242,20 @@ class DesTest {
0, 0, 1, 1, 1, 0, 1, 0, // :
0, 1, 0, 1, 1, 1, 1, 1, // _
0, 0, 1, 0, 0, 0, 0, 0, // space
}, "?!.,;:_ ");
}));
put(new int[]{
// Parenthèses
assertEquals("(){}[]", Des.bitsToString(new int[]{
0, 0, 1, 0, 1, 0, 0, 0, // (
0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 1, 1, 1, 0, 1, 1, // {
0, 1, 1, 1, 1, 1, 0, 1, // }
0, 1, 0, 1, 1, 0, 1, 1, // [
0, 1, 0, 1, 1, 1, 0, 1, // ]
}, "(){}[]");
}));
put(new int[]{
// Caractères spéciaux
assertEquals("&@#^\"'`", Des.bitsToString(new int[]{
0, 0, 1, 0, 0, 1, 1, 0, // &
0, 1, 0, 0, 0, 0, 0, 0, // @
0, 0, 1, 0, 0, 0, 1, 1, // #
@@ -277,148 +263,126 @@ class DesTest {
0, 0, 1, 0, 0, 0, 1, 0, // "
0, 0, 1, 0, 0, 1, 1, 1, // '
0, 1, 1, 0, 0, 0, 0, 0, // `
}, "&@#^\"'`");
// Custom test
put(new int[]{
0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 0, 1, 1, 1, 0, // n
0, 1, 1, 0, 1, 0, 1, 0, // j
0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 1, 0, 1, 0, 1, // u
0, 1, 1, 1, 0, 0, 1, 0, // r
}, "Bonjour");
}};
for (Map.Entry<int[], String> test : TESTS.entrySet()) {
assertEquals(test.getValue(), Des.bitsToString(test.getKey()));
System.out.printf("\"%s\" -> OK\n", test.getValue());
}
}));
}
@Test
@DisplayName("xor")
void xor() {
final int[][][] TESTS = {
{
{0, 1, 0, 1}, // Table 1
{0, 0, 1, 1}, // Table 2
{0, 1, 1, 0}, // Result expected
},
};
for (int[][] test : TESTS) {
assertArrayEquals(Des.xor(test[0], test[1]), test[2]);
System.out.printf(
"%s XOR %s -> OK\n",
Arrays.toString(test[0]),
Arrays.toString(test[1])
assertArrayEquals(
new int[]{0, 1, 1, 0},
Des.xor(new int[]{0, 1, 0, 1}, new int[]{0, 0, 1, 1})
);
}
}
@Test
@DisplayName("decoupage")
void decoupage() {
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}
}
);
}};
for (Map.Entry<int[][], int[][]> test : TESTS.entrySet()) {
assertArrayEquals(test.getValue(), Des.decoupage(test.getKey()[0], test.getKey()[1][0]));
System.out.printf("(%s, %s) -> OK\n", Arrays.toString(test.getKey()[0]), test.getKey()[1][0]);
}
assertArrayEquals(new int[][]{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{9, 10, 11},
{12, 13, 14},
{15, 16, 17},
{18, 19, 0}
}, 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));
}
@Test
@DisplayName("recollageBloc")
void recollageBloc() {
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}
assertArrayEquals(
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
Des.recollageBloc(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
@DisplayName("decaleGauche")
void decaleGauche() {
fail("Not yet implemented"); // TODO
// 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)));
}
@Test
@DisplayName("generePermutation")
void generePermutation() {
fail("Not yet implemented"); // TODO
// TODO
System.out.println(Arrays.toString(Des.generePermutation(64)));
}
@Test
@DisplayName("permutation")
void permutation() {
fail("Not yet implemented"); // TODO
// TODO
int[] permutation = Des.generePermutation(64);
int[] bloc = new int[permutation.length];
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
@DisplayName("invPermutation")
void invPermutation() {
fail("Not yet implemented"); // TODO
// 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;
}
System.out.println("Bloc avant permuation: ");
System.out.println(Arrays.toString(bloc));
Des.permutation(permutation, bloc);
System.out.println("Bloc apres permuation: ");
System.out.println(Arrays.toString(bloc));
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
@DisplayName("genereCle")
void genereCle() {
fail("Not yet implemented"); // TODO
// TODO
Des a = new Des();
a.genereCle(1);
System.out.println(Arrays.toString(a.table_cles.get(0)));
}
@Test
@DisplayName("fonction_S")
void fonction_S() {
fail("Not yet implemented"); // TODO
// TODO
Des des = new Des();
//System.out.println(Arrays.toString(des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}, 1)));
}
@Test
@DisplayName("fonction_F")
void fonction_F() {
fail("Not yet implemented"); // TODO
// TODO
Des des = new Des();
//System.out.println(Arrays.toString(des.fonction_F(1, )));
}
@Test
@DisplayName("crypte")
void crypte() {
fail("Not yet implemented"); // TODO
// 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
@DisplayName("decrypte")
void decrypte() {
fail("Not yet implemented"); // TODO
// 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));
}
}