Files
data-encryption-standard/test/cryptography/DesTest.java
T
2022-11-04 15:37:12 +01:00

388 lines
13 KiB
Java

/*
* DesTest.java, 26/10/2022
* INU Champollion 2022-2023, L3 INFO
* pas de copyright, aucun droits
*/
package cryptography;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Gaël BURGUÈS
* @author Laurian DUFRECHOU
* @author Lucàs VABRE
*/
class DesTest {
@Test
void stringToBits() {
// Chaine vide
assertArrayEquals(new int[]{}, Des.stringToBits(""));
// 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"));
// 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"));
// 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"));
// 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("+-/*%="));
// 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("?!.,;:_ "));
// 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("(){}[]"));
// 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
void bitsToString() {
// 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
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
}));
// 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
}));
// 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
}));
// 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, // =
}));
// 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
}));
// 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, // ]
}));
// 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
void xor() {
assertArrayEquals(
new int[]{0, 1, 1, 0},
Des.xor(new int[]{0, 1, 0, 1}, new int[]{0, 0, 1, 1})
);
}
@Test
void decoupage() {
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
void recollageBloc() {
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},})
);
}
@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
void genereCle() {
// TODO
Des a = new Des();
a.genereCle(1);
System.out.println(Arrays.toString(a.table_cles.get(0)));
}
@Test
void fonction_S() {
// TODO
Des des = new Des();
//System.out.println(Arrays.toString(des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}, 1)));
}
@Test
void fonction_F() {
// TODO
Des des = new Des();
//System.out.println(Arrays.toString(des.fonction_F(1, )));
}
@Test
void crypte() {
// 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
void decrypte() {
// 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));
}
}