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; package cryptography;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; 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 * @author Gaël BURGUÈS
@@ -22,14 +20,12 @@ import static org.junit.jupiter.api.Assertions.*;
class DesTest { class DesTest {
@Test @Test
@DisplayName("stringToBits")
void stringToBits() { void stringToBits() {
HashMap<String, int[]> TESTS = new HashMap<>() {{ // Chaine vide
// Empty string assertArrayEquals(new int[]{}, Des.stringToBits(""));
put("", new int[]{});
// abc, maj // Minuscules
put("abcdefghijklmnopqrstuvwxyz", new int[]{ assertArrayEquals(new int[]{
0, 1, 1, 0, 0, 0, 0, 1, // a 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, 0, // b
0, 1, 1, 0, 0, 0, 1, 1, // c 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, 0, // x
0, 1, 1, 1, 1, 0, 0, 1, // y 0, 1, 1, 1, 1, 0, 0, 1, // y
0, 1, 1, 1, 1, 0, 1, 0, // z 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, 0, 1, // A
0, 1, 0, 0, 0, 0, 1, 0, // B 0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 0, 0, 0, 0, 1, 1, // C 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, 0, // X
0, 1, 0, 1, 1, 0, 0, 1, // Y 0, 1, 0, 1, 1, 0, 0, 1, // Y
0, 1, 0, 1, 1, 0, 1, 0, // Z 0, 1, 0, 1, 1, 0, 1, 0, // Z
}); }, Des.stringToBits("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
// Numbers // Chiffres
put("0123456789", new int[]{ assertArrayEquals(new int[]{
0, 0, 1, 1, 0, 0, 0, 0, // 0 0, 0, 1, 1, 0, 0, 0, 0, // 0
0, 0, 1, 1, 0, 0, 0, 1, // 1 0, 0, 1, 1, 0, 0, 0, 1, // 1
0, 0, 1, 1, 0, 0, 1, 0, // 2 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, 0, 1, 1, 1, // 7
0, 0, 1, 1, 1, 0, 0, 0, // 8 0, 0, 1, 1, 1, 0, 0, 0, // 8
0, 0, 1, 1, 1, 0, 0, 1, // 9 0, 0, 1, 1, 1, 0, 0, 1, // 9
}); }, Des.stringToBits("0123456789"));
// Special chars // Symboles mathématiques
put("+-/*%=", new int[]{ assertArrayEquals(new int[]{
0, 0, 1, 0, 1, 0, 1, 1, // + 0, 0, 1, 0, 1, 0, 1, 1, // +
0, 0, 1, 0, 1, 1, 0, 1, // - 0, 0, 1, 0, 1, 1, 0, 1, // -
0, 0, 1, 0, 1, 1, 1, 1, // / 0, 0, 1, 0, 1, 1, 1, 1, // /
0, 0, 1, 0, 1, 0, 1, 0, // * 0, 0, 1, 0, 1, 0, 1, 0, // *
0, 0, 1, 0, 0, 1, 0, 1, // % 0, 0, 1, 0, 0, 1, 0, 1, // %
0, 0, 1, 1, 1, 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, 1, 1, 1, 1, 1, // ?
0, 0, 1, 0, 0, 0, 0, 1, // ! 0, 0, 1, 0, 0, 0, 0, 1, // !
0, 0, 1, 0, 1, 1, 1, 0, // . 0, 0, 1, 0, 1, 1, 1, 0, // .
@@ -120,18 +118,20 @@ class DesTest {
0, 0, 1, 1, 1, 0, 1, 0, // : 0, 0, 1, 1, 1, 0, 1, 0, // :
0, 1, 0, 1, 1, 1, 1, 1, // _ 0, 1, 0, 1, 1, 1, 1, 1, // _
0, 0, 1, 0, 0, 0, 0, 0, // space 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, 0, // (
0, 0, 1, 0, 1, 0, 0, 1, // ) 0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 1, 1, 1, 0, 1, 1, // { 0, 1, 1, 1, 1, 0, 1, 1, // {
0, 1, 1, 1, 1, 1, 0, 1, // } 0, 1, 1, 1, 1, 1, 0, 1, // }
0, 1, 0, 1, 1, 0, 1, 1, // [ 0, 1, 0, 1, 1, 0, 1, 1, // [
0, 1, 0, 1, 1, 1, 0, 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, 0, 1, 0, 0, 1, 1, 0, // &
0, 1, 0, 0, 0, 0, 0, 0, // @ 0, 1, 0, 0, 0, 0, 0, 0, // @
0, 0, 1, 0, 0, 0, 1, 1, // # 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, 0, 1, 0, // "
0, 0, 1, 0, 0, 1, 1, 1, // ' 0, 0, 1, 0, 0, 1, 1, 1, // '
0, 1, 1, 0, 0, 0, 0, 0, // ` 0, 1, 1, 0, 0, 0, 0, 0, // `
}); }, Des.stringToBits("&@#^\"'`"));
// 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());
}
} }
@Test @Test
@DisplayName("stringToBits")
void bitsToString() { void bitsToString() {
HashMap<int[], String> TESTS = new HashMap<>() {{
// Empty string
put(new int[]{}, "");
// abc, maj // Chaine vide
put(new int[]{ 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, 0, 1, // a
0, 1, 1, 0, 0, 0, 1, 0, // b 0, 1, 1, 0, 0, 0, 1, 0, // b
0, 1, 1, 0, 0, 0, 1, 1, // c 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, 0, // x
0, 1, 1, 1, 1, 0, 0, 1, // y 0, 1, 1, 1, 1, 0, 0, 1, // y
0, 1, 1, 1, 1, 0, 1, 0, // z 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, 0, 1, // A
0, 1, 0, 0, 0, 0, 1, 0, // B 0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 0, 0, 0, 0, 1, 1, // C 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, 0, // X
0, 1, 0, 1, 1, 0, 0, 1, // Y 0, 1, 0, 1, 1, 0, 0, 1, // Y
0, 1, 0, 1, 1, 0, 1, 0, // Z 0, 1, 0, 1, 1, 0, 1, 0, // Z
}, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); }));
// Numbers // Chiffres
put(new int[]{ assertEquals("0123456789", Des.bitsToString(new int[]{
0, 0, 1, 1, 0, 0, 0, 0, // 0 0, 0, 1, 1, 0, 0, 0, 0, // 0
0, 0, 1, 1, 0, 0, 0, 1, // 1 0, 0, 1, 1, 0, 0, 0, 1, // 1
0, 0, 1, 1, 0, 0, 1, 0, // 2 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, 0, 1, 1, 1, // 7
0, 0, 1, 1, 1, 0, 0, 0, // 8 0, 0, 1, 1, 1, 0, 0, 0, // 8
0, 0, 1, 1, 1, 0, 0, 1, // 9 0, 0, 1, 1, 1, 0, 0, 1, // 9
}, "0123456789"); }));
// Special chars // Symboles mathématiques
put(new int[]{ assertEquals("+-/*%=", Des.bitsToString(new int[]{
0, 0, 1, 0, 1, 0, 1, 1, // + 0, 0, 1, 0, 1, 0, 1, 1, // +
0, 0, 1, 0, 1, 1, 0, 1, // - 0, 0, 1, 0, 1, 1, 0, 1, // -
0, 0, 1, 0, 1, 1, 1, 1, // / 0, 0, 1, 0, 1, 1, 1, 1, // /
0, 0, 1, 0, 1, 0, 1, 0, // * 0, 0, 1, 0, 1, 0, 1, 0, // *
0, 0, 1, 0, 0, 1, 0, 1, // % 0, 0, 1, 0, 0, 1, 0, 1, // %
0, 0, 1, 1, 1, 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, 1, 1, 1, 1, 1, // ?
0, 0, 1, 0, 0, 0, 0, 1, // ! 0, 0, 1, 0, 0, 0, 0, 1, // !
0, 0, 1, 0, 1, 1, 1, 0, // . 0, 0, 1, 0, 1, 1, 1, 0, // .
@@ -258,18 +242,20 @@ class DesTest {
0, 0, 1, 1, 1, 0, 1, 0, // : 0, 0, 1, 1, 1, 0, 1, 0, // :
0, 1, 0, 1, 1, 1, 1, 1, // _ 0, 1, 0, 1, 1, 1, 1, 1, // _
0, 0, 1, 0, 0, 0, 0, 0, // space 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, 0, // (
0, 0, 1, 0, 1, 0, 0, 1, // ) 0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 1, 1, 1, 0, 1, 1, // { 0, 1, 1, 1, 1, 0, 1, 1, // {
0, 1, 1, 1, 1, 1, 0, 1, // } 0, 1, 1, 1, 1, 1, 0, 1, // }
0, 1, 0, 1, 1, 0, 1, 1, // [ 0, 1, 0, 1, 1, 0, 1, 1, // [
0, 1, 0, 1, 1, 1, 0, 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, 0, 1, 0, 0, 1, 1, 0, // &
0, 1, 0, 0, 0, 0, 0, 0, // @ 0, 1, 0, 0, 0, 0, 0, 0, // @
0, 0, 1, 0, 0, 0, 1, 1, // # 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, 0, 1, 0, // "
0, 0, 1, 0, 0, 1, 1, 1, // ' 0, 0, 1, 0, 0, 1, 1, 1, // '
0, 1, 1, 0, 0, 0, 0, 0, // ` 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 @Test
@DisplayName("xor")
void xor() { void xor() {
assertArrayEquals(
final int[][][] TESTS = { new int[]{0, 1, 1, 0},
{ Des.xor(new int[]{0, 1, 0, 1}, new int[]{0, 0, 1, 1})
{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])
); );
} }
}
@Test @Test
@DisplayName("decoupage")
void decoupage() { void decoupage() {
HashMap<int[][], int[][]> TESTS = new HashMap<>() {{ assertArrayEquals(new int[][]{
// TODO add more tests {0, 1, 2},
put( {3, 4, 5},
new int[][]{ {6, 7, 8},
new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // bloc {9, 10, 11},
new int[]{7} // nbBloc {12, 13, 14},
}, {15, 16, 17},
new int[][]{ // expected result {18, 19, 0}
new int[]{0, 1, 2}, }, 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));
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]);
}
} }
@Test @Test
@DisplayName("recollageBloc")
void recollageBloc() { void recollageBloc() {
HashMap<int[][], int[]> TESTS = new HashMap<>() {{ assertArrayEquals(
// TODO add more tests new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
put( Des.recollageBloc(new int[][]{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10},})
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")
void 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 @Test
@DisplayName("generePermutation")
void generePermutation() { void generePermutation() {
fail("Not yet implemented"); // TODO // TODO
System.out.println(Arrays.toString(Des.generePermutation(64)));
} }
@Test @Test
@DisplayName("permutation")
void 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 @Test
@DisplayName("invPermutation")
void 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 @Test
@DisplayName("genereCle")
void 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 @Test
@DisplayName("fonction_S")
void 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 @Test
@DisplayName("fonction_F")
void 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 @Test
@DisplayName("crypte")
void 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 @Test
@DisplayName("decrypte")
void 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));
} }
} }