Update DesTest.java

This commit is contained in:
Lucàs
2022-11-04 15:37:12 +01:00
parent 51a98e486f
commit 46b6849c93
+311 -347
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,403 +20,369 @@ 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
0, 1, 1, 0, 0, 1, 0, 0, // d 0, 1, 1, 0, 0, 1, 0, 0, // d
0, 1, 1, 0, 0, 1, 0, 1, // e 0, 1, 1, 0, 0, 1, 0, 1, // e
0, 1, 1, 0, 0, 1, 1, 0, // f 0, 1, 1, 0, 0, 1, 1, 0, // f
0, 1, 1, 0, 0, 1, 1, 1, // g 0, 1, 1, 0, 0, 1, 1, 1, // g
0, 1, 1, 0, 1, 0, 0, 0, // h 0, 1, 1, 0, 1, 0, 0, 0, // h
0, 1, 1, 0, 1, 0, 0, 1, // i 0, 1, 1, 0, 1, 0, 0, 1, // i
0, 1, 1, 0, 1, 0, 1, 0, // j 0, 1, 1, 0, 1, 0, 1, 0, // j
0, 1, 1, 0, 1, 0, 1, 1, // k 0, 1, 1, 0, 1, 0, 1, 1, // k
0, 1, 1, 0, 1, 1, 0, 0, // l 0, 1, 1, 0, 1, 1, 0, 0, // l
0, 1, 1, 0, 1, 1, 0, 1, // m 0, 1, 1, 0, 1, 1, 0, 1, // m
0, 1, 1, 0, 1, 1, 1, 0, // n 0, 1, 1, 0, 1, 1, 1, 0, // n
0, 1, 1, 0, 1, 1, 1, 1, // o 0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 1, 0, 0, 0, 0, // p 0, 1, 1, 1, 0, 0, 0, 0, // p
0, 1, 1, 1, 0, 0, 0, 1, // q 0, 1, 1, 1, 0, 0, 0, 1, // q
0, 1, 1, 1, 0, 0, 1, 0, // r 0, 1, 1, 1, 0, 0, 1, 0, // r
0, 1, 1, 1, 0, 0, 1, 1, // s 0, 1, 1, 1, 0, 0, 1, 1, // s
0, 1, 1, 1, 0, 1, 0, 0, // t 0, 1, 1, 1, 0, 1, 0, 0, // t
0, 1, 1, 1, 0, 1, 0, 1, // u 0, 1, 1, 1, 0, 1, 0, 1, // u
0, 1, 1, 1, 0, 1, 1, 0, // v 0, 1, 1, 1, 0, 1, 1, 0, // v
0, 1, 1, 1, 0, 1, 1, 1, // w 0, 1, 1, 1, 0, 1, 1, 1, // w
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
0, 1, 0, 0, 0, 0, 0, 1, // A assertArrayEquals(new int[]{
0, 1, 0, 0, 0, 0, 1, 0, // B 0, 1, 0, 0, 0, 0, 0, 1, // A
0, 1, 0, 0, 0, 0, 1, 1, // C 0, 1, 0, 0, 0, 0, 1, 0, // B
0, 1, 0, 0, 0, 1, 0, 0, // D 0, 1, 0, 0, 0, 0, 1, 1, // C
0, 1, 0, 0, 0, 1, 0, 1, // E 0, 1, 0, 0, 0, 1, 0, 0, // D
0, 1, 0, 0, 0, 1, 1, 0, // F 0, 1, 0, 0, 0, 1, 0, 1, // E
0, 1, 0, 0, 0, 1, 1, 1, // G 0, 1, 0, 0, 0, 1, 1, 0, // F
0, 1, 0, 0, 1, 0, 0, 0, // H 0, 1, 0, 0, 0, 1, 1, 1, // G
0, 1, 0, 0, 1, 0, 0, 1, // I 0, 1, 0, 0, 1, 0, 0, 0, // H
0, 1, 0, 0, 1, 0, 1, 0, // J 0, 1, 0, 0, 1, 0, 0, 1, // I
0, 1, 0, 0, 1, 0, 1, 1, // K 0, 1, 0, 0, 1, 0, 1, 0, // J
0, 1, 0, 0, 1, 1, 0, 0, // L 0, 1, 0, 0, 1, 0, 1, 1, // K
0, 1, 0, 0, 1, 1, 0, 1, // M 0, 1, 0, 0, 1, 1, 0, 0, // L
0, 1, 0, 0, 1, 1, 1, 0, // N 0, 1, 0, 0, 1, 1, 0, 1, // M
0, 1, 0, 0, 1, 1, 1, 1, // O 0, 1, 0, 0, 1, 1, 1, 0, // N
0, 1, 0, 1, 0, 0, 0, 0, // P 0, 1, 0, 0, 1, 1, 1, 1, // O
0, 1, 0, 1, 0, 0, 0, 1, // Q 0, 1, 0, 1, 0, 0, 0, 0, // P
0, 1, 0, 1, 0, 0, 1, 0, // R 0, 1, 0, 1, 0, 0, 0, 1, // Q
0, 1, 0, 1, 0, 0, 1, 1, // S 0, 1, 0, 1, 0, 0, 1, 0, // R
0, 1, 0, 1, 0, 1, 0, 0, // T 0, 1, 0, 1, 0, 0, 1, 1, // S
0, 1, 0, 1, 0, 1, 0, 1, // U 0, 1, 0, 1, 0, 1, 0, 0, // T
0, 1, 0, 1, 0, 1, 1, 0, // V 0, 1, 0, 1, 0, 1, 0, 1, // U
0, 1, 0, 1, 0, 1, 1, 1, // W 0, 1, 0, 1, 0, 1, 1, 0, // V
0, 1, 0, 1, 1, 0, 0, 0, // X 0, 1, 0, 1, 0, 1, 1, 1, // W
0, 1, 0, 1, 1, 0, 0, 1, // Y 0, 1, 0, 1, 1, 0, 0, 0, // X
0, 1, 0, 1, 1, 0, 1, 0, // Z 0, 1, 0, 1, 1, 0, 0, 1, // Y
}); 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
0, 0, 1, 1, 0, 0, 1, 1, // 3 0, 0, 1, 1, 0, 0, 1, 1, // 3
0, 0, 1, 1, 0, 1, 0, 0, // 4 0, 0, 1, 1, 0, 1, 0, 0, // 4
0, 0, 1, 1, 0, 1, 0, 1, // 5 0, 0, 1, 1, 0, 1, 0, 1, // 5
0, 0, 1, 1, 0, 1, 1, 0, // 6 0, 0, 1, 1, 0, 1, 1, 0, // 6
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
0, 0, 1, 1, 1, 1, 1, 1, // ? assertArrayEquals(new int[]{
0, 0, 1, 0, 0, 0, 0, 1, // ! 0, 0, 1, 1, 1, 1, 1, 1, // ?
0, 0, 1, 0, 1, 1, 1, 0, // . 0, 0, 1, 0, 0, 0, 0, 1, // !
0, 0, 1, 0, 1, 1, 0, 0, // , 0, 0, 1, 0, 1, 1, 1, 0, // .
0, 0, 1, 1, 1, 0, 1, 1, // ; 0, 0, 1, 0, 1, 1, 0, 0, // ,
0, 0, 1, 1, 1, 0, 1, 0, // : 0, 0, 1, 1, 1, 0, 1, 1, // ;
0, 1, 0, 1, 1, 1, 1, 1, // _ 0, 0, 1, 1, 1, 0, 1, 0, // :
0, 0, 1, 0, 0, 0, 0, 0, // space 0, 1, 0, 1, 1, 1, 1, 1, // _
}); 0, 0, 1, 0, 0, 0, 0, 0, // space
}, Des.stringToBits("?!.,;:_ "));
put("(){}[]", new int[]{ // Parenthèses
0, 0, 1, 0, 1, 0, 0, 0, // ( assertArrayEquals(new int[]{
0, 0, 1, 0, 1, 0, 0, 1, // ) 0, 0, 1, 0, 1, 0, 0, 0, // (
0, 1, 1, 1, 1, 0, 1, 1, // { 0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 1, 1, 1, 1, 0, 1, // } 0, 1, 1, 1, 1, 0, 1, 1, // {
0, 1, 0, 1, 1, 0, 1, 1, // [ 0, 1, 1, 1, 1, 1, 0, 1, // }
0, 1, 0, 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
0, 0, 1, 0, 0, 1, 1, 0, // & assertArrayEquals(new int[]{
0, 1, 0, 0, 0, 0, 0, 0, // @ 0, 0, 1, 0, 0, 1, 1, 0, // &
0, 0, 1, 0, 0, 0, 1, 1, // # 0, 1, 0, 0, 0, 0, 0, 0, // @
0, 1, 0, 1, 1, 1, 1, 0, // ^ 0, 0, 1, 0, 0, 0, 1, 1, // #
0, 0, 1, 0, 0, 0, 1, 0, // " 0, 1, 0, 1, 1, 1, 1, 0, // ^
0, 0, 1, 0, 0, 1, 1, 1, // ' 0, 0, 1, 0, 0, 0, 1, 0, // "
0, 1, 1, 0, 0, 0, 0, 0, // ` 0, 0, 1, 0, 0, 1, 1, 1, // '
}); 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[]{}));
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
0, 1, 1, 0, 0, 1, 0, 0, // d
0, 1, 1, 0, 0, 1, 0, 1, // e
0, 1, 1, 0, 0, 1, 1, 0, // f
0, 1, 1, 0, 0, 1, 1, 1, // g
0, 1, 1, 0, 1, 0, 0, 0, // h
0, 1, 1, 0, 1, 0, 0, 1, // i
0, 1, 1, 0, 1, 0, 1, 0, // j
0, 1, 1, 0, 1, 0, 1, 1, // k
0, 1, 1, 0, 1, 1, 0, 0, // l
0, 1, 1, 0, 1, 1, 0, 1, // m
0, 1, 1, 0, 1, 1, 1, 0, // n
0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 1, 1, 0, 0, 0, 0, // p
0, 1, 1, 1, 0, 0, 0, 1, // q
0, 1, 1, 1, 0, 0, 1, 0, // r
0, 1, 1, 1, 0, 0, 1, 1, // s
0, 1, 1, 1, 0, 1, 0, 0, // t
0, 1, 1, 1, 0, 1, 0, 1, // u
0, 1, 1, 1, 0, 1, 1, 0, // v
0, 1, 1, 1, 0, 1, 1, 1, // w
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[]{ // Minuscules
0, 1, 0, 0, 0, 0, 0, 1, // A assertEquals("abcdefghijklmnopqrstuvwxyz", Des.bitsToString(new int[]{
0, 1, 0, 0, 0, 0, 1, 0, // B 0, 1, 1, 0, 0, 0, 0, 1, // a
0, 1, 0, 0, 0, 0, 1, 1, // C 0, 1, 1, 0, 0, 0, 1, 0, // b
0, 1, 0, 0, 0, 1, 0, 0, // D 0, 1, 1, 0, 0, 0, 1, 1, // c
0, 1, 0, 0, 0, 1, 0, 1, // E 0, 1, 1, 0, 0, 1, 0, 0, // d
0, 1, 0, 0, 0, 1, 1, 0, // F 0, 1, 1, 0, 0, 1, 0, 1, // e
0, 1, 0, 0, 0, 1, 1, 1, // G 0, 1, 1, 0, 0, 1, 1, 0, // f
0, 1, 0, 0, 1, 0, 0, 0, // H 0, 1, 1, 0, 0, 1, 1, 1, // g
0, 1, 0, 0, 1, 0, 0, 1, // I 0, 1, 1, 0, 1, 0, 0, 0, // h
0, 1, 0, 0, 1, 0, 1, 0, // J 0, 1, 1, 0, 1, 0, 0, 1, // i
0, 1, 0, 0, 1, 0, 1, 1, // K 0, 1, 1, 0, 1, 0, 1, 0, // j
0, 1, 0, 0, 1, 1, 0, 0, // L 0, 1, 1, 0, 1, 0, 1, 1, // k
0, 1, 0, 0, 1, 1, 0, 1, // M 0, 1, 1, 0, 1, 1, 0, 0, // l
0, 1, 0, 0, 1, 1, 1, 0, // N 0, 1, 1, 0, 1, 1, 0, 1, // m
0, 1, 0, 0, 1, 1, 1, 1, // O 0, 1, 1, 0, 1, 1, 1, 0, // n
0, 1, 0, 1, 0, 0, 0, 0, // P 0, 1, 1, 0, 1, 1, 1, 1, // o
0, 1, 0, 1, 0, 0, 0, 1, // Q 0, 1, 1, 1, 0, 0, 0, 0, // p
0, 1, 0, 1, 0, 0, 1, 0, // R 0, 1, 1, 1, 0, 0, 0, 1, // q
0, 1, 0, 1, 0, 0, 1, 1, // S 0, 1, 1, 1, 0, 0, 1, 0, // r
0, 1, 0, 1, 0, 1, 0, 0, // T 0, 1, 1, 1, 0, 0, 1, 1, // s
0, 1, 0, 1, 0, 1, 0, 1, // U 0, 1, 1, 1, 0, 1, 0, 0, // t
0, 1, 0, 1, 0, 1, 1, 0, // V 0, 1, 1, 1, 0, 1, 0, 1, // u
0, 1, 0, 1, 0, 1, 1, 1, // W 0, 1, 1, 1, 0, 1, 1, 0, // v
0, 1, 0, 1, 1, 0, 0, 0, // X 0, 1, 1, 1, 0, 1, 1, 1, // w
0, 1, 0, 1, 1, 0, 0, 1, // Y 0, 1, 1, 1, 1, 0, 0, 0, // x
0, 1, 0, 1, 1, 0, 1, 0, // Z 0, 1, 1, 1, 1, 0, 0, 1, // y
}, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 0, 1, 1, 1, 1, 0, 1, 0, // z
}));
// Numbers // Majuscules
put(new int[]{ assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Des.bitsToString(new int[]{
0, 0, 1, 1, 0, 0, 0, 0, // 0 0, 1, 0, 0, 0, 0, 0, 1, // A
0, 0, 1, 1, 0, 0, 0, 1, // 1 0, 1, 0, 0, 0, 0, 1, 0, // B
0, 0, 1, 1, 0, 0, 1, 0, // 2 0, 1, 0, 0, 0, 0, 1, 1, // C
0, 0, 1, 1, 0, 0, 1, 1, // 3 0, 1, 0, 0, 0, 1, 0, 0, // D
0, 0, 1, 1, 0, 1, 0, 0, // 4 0, 1, 0, 0, 0, 1, 0, 1, // E
0, 0, 1, 1, 0, 1, 0, 1, // 5 0, 1, 0, 0, 0, 1, 1, 0, // F
0, 0, 1, 1, 0, 1, 1, 0, // 6 0, 1, 0, 0, 0, 1, 1, 1, // G
0, 0, 1, 1, 0, 1, 1, 1, // 7 0, 1, 0, 0, 1, 0, 0, 0, // H
0, 0, 1, 1, 1, 0, 0, 0, // 8 0, 1, 0, 0, 1, 0, 0, 1, // I
0, 0, 1, 1, 1, 0, 0, 1, // 9 0, 1, 0, 0, 1, 0, 1, 0, // J
}, "0123456789"); 0, 1, 0, 0, 1, 0, 1, 1, // K
0, 1, 0, 0, 1, 1, 0, 0, // L
0, 1, 0, 0, 1, 1, 0, 1, // M
0, 1, 0, 0, 1, 1, 1, 0, // N
0, 1, 0, 0, 1, 1, 1, 1, // O
0, 1, 0, 1, 0, 0, 0, 0, // P
0, 1, 0, 1, 0, 0, 0, 1, // Q
0, 1, 0, 1, 0, 0, 1, 0, // R
0, 1, 0, 1, 0, 0, 1, 1, // S
0, 1, 0, 1, 0, 1, 0, 0, // T
0, 1, 0, 1, 0, 1, 0, 1, // U
0, 1, 0, 1, 0, 1, 1, 0, // V
0, 1, 0, 1, 0, 1, 1, 1, // W
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
}));
// Special chars // Chiffres
put(new int[]{ assertEquals("0123456789", Des.bitsToString(new int[]{
0, 0, 1, 0, 1, 0, 1, 1, // + 0, 0, 1, 1, 0, 0, 0, 0, // 0
0, 0, 1, 0, 1, 1, 0, 1, // - 0, 0, 1, 1, 0, 0, 0, 1, // 1
0, 0, 1, 0, 1, 1, 1, 1, // / 0, 0, 1, 1, 0, 0, 1, 0, // 2
0, 0, 1, 0, 1, 0, 1, 0, // * 0, 0, 1, 1, 0, 0, 1, 1, // 3
0, 0, 1, 0, 0, 1, 0, 1, // % 0, 0, 1, 1, 0, 1, 0, 0, // 4
0, 0, 1, 1, 1, 1, 0, 1, // = 0, 0, 1, 1, 0, 1, 0, 1, // 5
}, "+-/*%="); 0, 0, 1, 1, 0, 1, 1, 0, // 6
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
}));
put(new int[]{ // Symboles mathématiques
0, 0, 1, 1, 1, 1, 1, 1, // ? assertEquals("+-/*%=", Des.bitsToString(new int[]{
0, 0, 1, 0, 0, 0, 0, 1, // ! 0, 0, 1, 0, 1, 0, 1, 1, // +
0, 0, 1, 0, 1, 1, 1, 0, // . 0, 0, 1, 0, 1, 1, 0, 1, // -
0, 0, 1, 0, 1, 1, 0, 0, // , 0, 0, 1, 0, 1, 1, 1, 1, // /
0, 0, 1, 1, 1, 0, 1, 1, // ; 0, 0, 1, 0, 1, 0, 1, 0, // *
0, 0, 1, 1, 1, 0, 1, 0, // : 0, 0, 1, 0, 0, 1, 0, 1, // %
0, 1, 0, 1, 1, 1, 1, 1, // _ 0, 0, 1, 1, 1, 1, 0, 1, // =
0, 0, 1, 0, 0, 0, 0, 0, // space }));
}, "?!.,;:_ ");
put(new int[]{ // Ponctuations
0, 0, 1, 0, 1, 0, 0, 0, // ( assertEquals("?!.,;:_ ", Des.bitsToString(new int[]{
0, 0, 1, 0, 1, 0, 0, 1, // ) 0, 0, 1, 1, 1, 1, 1, 1, // ?
0, 1, 1, 1, 1, 0, 1, 1, // { 0, 0, 1, 0, 0, 0, 0, 1, // !
0, 1, 1, 1, 1, 1, 0, 1, // } 0, 0, 1, 0, 1, 1, 1, 0, // .
0, 1, 0, 1, 1, 0, 1, 1, // [ 0, 0, 1, 0, 1, 1, 0, 0, // ,
0, 1, 0, 1, 1, 1, 0, 1, // ] 0, 0, 1, 1, 1, 0, 1, 1, // ;
}, "(){}[]"); 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
0, 0, 1, 0, 0, 1, 1, 0, // & assertEquals("(){}[]", Des.bitsToString(new int[]{
0, 1, 0, 0, 0, 0, 0, 0, // @ 0, 0, 1, 0, 1, 0, 0, 0, // (
0, 0, 1, 0, 0, 0, 1, 1, // # 0, 0, 1, 0, 1, 0, 0, 1, // )
0, 1, 0, 1, 1, 1, 1, 0, // ^ 0, 1, 1, 1, 1, 0, 1, 1, // {
0, 0, 1, 0, 0, 0, 1, 0, // " 0, 1, 1, 1, 1, 1, 0, 1, // }
0, 0, 1, 0, 0, 1, 1, 1, // ' 0, 1, 0, 1, 1, 0, 1, 1, // [
0, 1, 1, 0, 0, 0, 0, 0, // ` 0, 1, 0, 1, 1, 1, 0, 1, // ]
}, "&@#^\"'`"); }));
// Custom test // Caractères spéciaux
put(new int[]{ assertEquals("&@#^\"'`", Des.bitsToString(new int[]{
0, 1, 0, 0, 0, 0, 1, 0, // B 0, 0, 1, 0, 0, 1, 1, 0, // &
0, 1, 1, 0, 1, 1, 1, 1, // o 0, 1, 0, 0, 0, 0, 0, 0, // @
0, 1, 1, 0, 1, 1, 1, 0, // n 0, 0, 1, 0, 0, 0, 1, 1, // #
0, 1, 1, 0, 1, 0, 1, 0, // j 0, 1, 0, 1, 1, 1, 1, 0, // ^
0, 1, 1, 0, 1, 1, 1, 1, // o 0, 0, 1, 0, 0, 0, 1, 0, // "
0, 1, 1, 1, 0, 1, 0, 1, // u 0, 0, 1, 0, 0, 1, 1, 1, // '
0, 1, 1, 1, 0, 0, 1, 0, // r 0, 1, 1, 0, 0, 0, 0, 0, // `
}, "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()) { @Test
assertArrayEquals(test.getValue(), Des.recollageBloc(test.getKey())); void decaleGauche() {
System.out.printf("%s -> OK\n", Arrays.deepToString(test.getKey())); // 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
void generePermutation() {
// TODO
System.out.println(Arrays.toString(Des.generePermutation(64)));
}
@Test
void permutation() {
// 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
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;
} }
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("decaleGauche")
void decaleGauche() {
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
@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));
} }
} }