mirror of
https://github.com/LucasVbr/data-encryption-standard.git
synced 2026-05-14 09:06:13 +00:00
Update DesTest.java
This commit is contained in:
+311
-347
@@ -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,403 +20,369 @@ 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[]{
|
||||
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
|
||||
});
|
||||
// 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
|
||||
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
|
||||
}, Des.stringToBits("abcdefghijklmnopqrstuvwxyz"));
|
||||
|
||||
put("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 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
|
||||
0, 1, 0, 0, 0, 1, 0, 0, // D
|
||||
0, 1, 0, 0, 0, 1, 0, 1, // E
|
||||
0, 1, 0, 0, 0, 1, 1, 0, // F
|
||||
0, 1, 0, 0, 0, 1, 1, 1, // G
|
||||
0, 1, 0, 0, 1, 0, 0, 0, // H
|
||||
0, 1, 0, 0, 1, 0, 0, 1, // I
|
||||
0, 1, 0, 0, 1, 0, 1, 0, // J
|
||||
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
|
||||
});
|
||||
// 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
|
||||
0, 1, 0, 0, 0, 1, 0, 0, // D
|
||||
0, 1, 0, 0, 0, 1, 0, 1, // E
|
||||
0, 1, 0, 0, 0, 1, 1, 0, // F
|
||||
0, 1, 0, 0, 0, 1, 1, 1, // G
|
||||
0, 1, 0, 0, 1, 0, 0, 0, // H
|
||||
0, 1, 0, 0, 1, 0, 0, 1, // I
|
||||
0, 1, 0, 0, 1, 0, 1, 0, // J
|
||||
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
|
||||
}, Des.stringToBits("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
||||
|
||||
// Numbers
|
||||
put("0123456789", 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
|
||||
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, 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
|
||||
});
|
||||
// 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
|
||||
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, 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
|
||||
}, Des.stringToBits("0123456789"));
|
||||
|
||||
// Special chars
|
||||
put("+-/*%=", 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, // =
|
||||
});
|
||||
// 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[]{
|
||||
0, 0, 1, 1, 1, 1, 1, 1, // ?
|
||||
0, 0, 1, 0, 0, 0, 0, 1, // !
|
||||
0, 0, 1, 0, 1, 1, 1, 0, // .
|
||||
0, 0, 1, 0, 1, 1, 0, 0, // ,
|
||||
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
|
||||
});
|
||||
// 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, // .
|
||||
0, 0, 1, 0, 1, 1, 0, 0, // ,
|
||||
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
|
||||
}, Des.stringToBits("?!.,;:_ "));
|
||||
|
||||
put("(){}[]", 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, // ]
|
||||
});
|
||||
// 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[]{
|
||||
0, 0, 1, 0, 0, 1, 1, 0, // &
|
||||
0, 1, 0, 0, 0, 0, 0, 0, // @
|
||||
0, 0, 1, 0, 0, 0, 1, 1, // #
|
||||
0, 1, 0, 1, 1, 1, 1, 0, // ^
|
||||
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());
|
||||
}
|
||||
// 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, // #
|
||||
0, 1, 0, 1, 1, 1, 1, 0, // ^
|
||||
0, 0, 1, 0, 0, 0, 1, 0, // "
|
||||
0, 0, 1, 0, 0, 1, 1, 1, // '
|
||||
0, 1, 1, 0, 0, 0, 0, 0, // `
|
||||
}, Des.stringToBits("&@#^\"'`"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("stringToBits")
|
||||
void bitsToString() {
|
||||
HashMap<int[], String> TESTS = new HashMap<>() {{
|
||||
// Empty string
|
||||
put(new int[]{}, "");
|
||||
|
||||
// abc, maj
|
||||
put(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");
|
||||
// Chaine vide
|
||||
assertEquals("", Des.bitsToString(new int[]{}));
|
||||
|
||||
put(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
|
||||
0, 1, 0, 0, 0, 1, 0, 0, // D
|
||||
0, 1, 0, 0, 0, 1, 0, 1, // E
|
||||
0, 1, 0, 0, 0, 1, 1, 0, // F
|
||||
0, 1, 0, 0, 0, 1, 1, 1, // G
|
||||
0, 1, 0, 0, 1, 0, 0, 0, // H
|
||||
0, 1, 0, 0, 1, 0, 0, 1, // I
|
||||
0, 1, 0, 0, 1, 0, 1, 0, // J
|
||||
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
|
||||
}, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
// 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
|
||||
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
|
||||
}));
|
||||
|
||||
// Numbers
|
||||
put(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
|
||||
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, 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
|
||||
}, "0123456789");
|
||||
// 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
|
||||
0, 1, 0, 0, 0, 1, 0, 0, // D
|
||||
0, 1, 0, 0, 0, 1, 0, 1, // E
|
||||
0, 1, 0, 0, 0, 1, 1, 0, // F
|
||||
0, 1, 0, 0, 0, 1, 1, 1, // G
|
||||
0, 1, 0, 0, 1, 0, 0, 0, // H
|
||||
0, 1, 0, 0, 1, 0, 0, 1, // I
|
||||
0, 1, 0, 0, 1, 0, 1, 0, // J
|
||||
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
|
||||
put(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, // =
|
||||
}, "+-/*%=");
|
||||
// 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
|
||||
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, 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[]{
|
||||
0, 0, 1, 1, 1, 1, 1, 1, // ?
|
||||
0, 0, 1, 0, 0, 0, 0, 1, // !
|
||||
0, 0, 1, 0, 1, 1, 1, 0, // .
|
||||
0, 0, 1, 0, 1, 1, 0, 0, // ,
|
||||
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
|
||||
}, "?!.,;:_ ");
|
||||
// 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[]{
|
||||
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, // ]
|
||||
}, "(){}[]");
|
||||
// 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, // .
|
||||
0, 0, 1, 0, 1, 1, 0, 0, // ,
|
||||
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[]{
|
||||
0, 0, 1, 0, 0, 1, 1, 0, // &
|
||||
0, 1, 0, 0, 0, 0, 0, 0, // @
|
||||
0, 0, 1, 0, 0, 0, 1, 1, // #
|
||||
0, 1, 0, 1, 1, 1, 1, 0, // ^
|
||||
0, 0, 1, 0, 0, 0, 1, 0, // "
|
||||
0, 0, 1, 0, 0, 1, 1, 1, // '
|
||||
0, 1, 1, 0, 0, 0, 0, 0, // `
|
||||
}, "&@#^\"'`");
|
||||
// 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, // ]
|
||||
}));
|
||||
|
||||
// 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());
|
||||
}
|
||||
// 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, // #
|
||||
0, 1, 0, 1, 1, 1, 1, 0, // ^
|
||||
0, 0, 1, 0, 0, 0, 1, 0, // "
|
||||
0, 0, 1, 0, 0, 1, 1, 1, // '
|
||||
0, 1, 1, 0, 0, 0, 0, 0, // `
|
||||
}));
|
||||
}
|
||||
|
||||
@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
|
||||
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)));
|
||||
}
|
||||
|
||||
@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
|
||||
@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() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user