mirror of
https://github.com/LucasVbr/data-encryption-standard.git
synced 2026-05-14 01:22:07 +00:00
test crypte decrypte, permutation, recollage, decale, ...
This commit is contained in:
+27
-22
@@ -44,13 +44,13 @@ public class Des {
|
|||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* private static final int[][] S = {
|
/* private static final int[][] S = {
|
||||||
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
|
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
|
||||||
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
|
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
|
||||||
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
|
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
|
||||||
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
|
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
private static final int[] E = {
|
private static final int[] E = {
|
||||||
32, 1, 2, 3, 4, 5,
|
32, 1, 2, 3, 4, 5,
|
||||||
4, 5, 6, 7, 8, 9,
|
4, 5, 6, 7, 8, 9,
|
||||||
@@ -61,15 +61,14 @@ public class Des {
|
|||||||
24, 25, 26, 27, 28, 29,
|
24, 25, 26, 27, 28, 29,
|
||||||
28, 29, 30, 31, 32, 1,
|
28, 29, 30, 31, 32, 1,
|
||||||
};
|
};
|
||||||
public ArrayList<int[]> table_cles;
|
|
||||||
private final ArrayList<int[][]> table_S;
|
private final ArrayList<int[][]> table_S;
|
||||||
|
public ArrayList<int[]> table_cles;
|
||||||
private int[] masterKey = new int[64];
|
private int[] masterKey = new int[64];
|
||||||
|
|
||||||
public Des() {
|
public Des() {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
for (int i = 0; i < masterKey.length; i++) {
|
for (int i = 0; i < masterKey.length; i++)
|
||||||
this.masterKey[i] = random.nextInt(2);
|
this.masterKey[i] = random.nextInt(2);
|
||||||
}
|
|
||||||
this.table_cles = new ArrayList<>();
|
this.table_cles = new ArrayList<>();
|
||||||
this.table_S = new ArrayList<>();
|
this.table_S = new ArrayList<>();
|
||||||
}
|
}
|
||||||
@@ -224,25 +223,29 @@ public class Des {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Permutations */
|
/* Permutations */
|
||||||
public static void permutation(int[] tab_permutation, int[] bloc) {
|
public static int[] permutation(int[] tableauPermutation, int[] bloc) {
|
||||||
int[] newTab = new int[bloc.length];
|
if (tableauPermutation.length != bloc.length)
|
||||||
for (int i = 0; i < bloc.length; i++) {
|
throw new IllegalArgumentException("tableauPermutation et bloc n'ont pas la même taille");
|
||||||
|
|
||||||
newTab[i] = bloc[tab_permutation[i] % tab_permutation.length];
|
int[] resultat = new int[bloc.length];
|
||||||
|
for (int i = 0; i < bloc.length; i++) {
|
||||||
|
resultat[i] = bloc[tableauPermutation[i] % tableauPermutation.length];
|
||||||
}
|
}
|
||||||
|
|
||||||
System.arraycopy(newTab, 0, bloc, 0, newTab.length);
|
System.arraycopy(resultat, 0, bloc, 0, resultat.length);
|
||||||
|
return resultat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void invPermutation(int[] tab_permutation, int[] bloc) {
|
public static int[] invPermutation(int[] tableauPermutation, int[] bloc) {
|
||||||
int[] newTab = new int[bloc.length];
|
if (tableauPermutation.length != bloc.length)
|
||||||
|
throw new IllegalArgumentException("tableauPermutation et bloc n'ont pas la même taille");
|
||||||
|
|
||||||
|
int[] resultat = new int[bloc.length];
|
||||||
for (int i = 0; i < bloc.length; i++) {
|
for (int i = 0; i < bloc.length; i++) {
|
||||||
|
resultat[tableauPermutation[i] % tableauPermutation.length] = bloc[i];
|
||||||
newTab[tab_permutation[i] % tab_permutation.length] = bloc[i];
|
|
||||||
}
|
}
|
||||||
|
System.arraycopy(resultat, 0, bloc, 0, resultat.length);
|
||||||
System.arraycopy(newTab, 0, bloc, 0, newTab.length);
|
return resultat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] generePermutation(int taille) {
|
public static int[] generePermutation(int taille) {
|
||||||
@@ -309,7 +312,7 @@ public class Des {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Genere */
|
/* Genere */
|
||||||
public void genereCle(int n) {
|
public int[][] genereCle(int n) {
|
||||||
int[] newCle = new int[56];
|
int[] newCle = new int[56];
|
||||||
int[] lastCle = new int[48];
|
int[] lastCle = new int[48];
|
||||||
int[] permInit = generePermutation(newCle.length);
|
int[] permInit = generePermutation(newCle.length);
|
||||||
@@ -327,6 +330,8 @@ public class Des {
|
|||||||
System.arraycopy(newCle, 0, lastCle, 0, lastCle.length);
|
System.arraycopy(newCle, 0, lastCle, 0, lastCle.length);
|
||||||
permutation(lastPerm, lastCle);
|
permutation(lastPerm, lastCle);
|
||||||
this.table_cles.add(lastCle);
|
this.table_cles.add(lastCle);
|
||||||
|
|
||||||
|
return table_cles.toArray(new int[][]{});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void genereS(int n) {
|
public void genereS(int n) {
|
||||||
|
|||||||
@@ -89,13 +89,12 @@ public class testDes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void testFonctionS() {
|
public static void testFonctionS() {
|
||||||
// System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1})));
|
//System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1})));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testFonctionF() {
|
public static void testFonctionF() {
|
||||||
Des des = new Des();
|
//Des des = new Des();
|
||||||
|
//System.out.println(Arrays.toString(des.fonction_F()));
|
||||||
// System.out.println(Arrays.toString(des.fonction_F()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void testCrypte() {
|
private static void testCrypte() {
|
||||||
|
|||||||
+137
-57
@@ -5,12 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package cryptography;
|
package cryptography;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
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.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gaël BURGUÈS
|
* @author Gaël BURGUÈS
|
||||||
@@ -19,6 +22,51 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
*/
|
*/
|
||||||
class DesTest {
|
class DesTest {
|
||||||
|
|
||||||
|
private final ArrayList<Des> FIXTURE = new ArrayList<>();
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void init() {
|
||||||
|
int[][] MASTER_KEYS = {{
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
}, {
|
||||||
|
1, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 1, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 1, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 1, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 1, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 1, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 1, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 1,
|
||||||
|
}, {
|
||||||
|
1, 0, 1, 0, 1, 0, 1, 0,
|
||||||
|
0, 1, 0, 1, 0, 1, 0, 1,
|
||||||
|
1, 0, 1, 0, 1, 0, 1, 0,
|
||||||
|
0, 1, 0, 1, 0, 1, 0, 1,
|
||||||
|
1, 0, 1, 0, 1, 0, 1, 0,
|
||||||
|
0, 1, 0, 1, 0, 1, 0, 1,
|
||||||
|
1, 0, 1, 0, 1, 0, 1, 0,
|
||||||
|
0, 1, 0, 1, 0, 1, 0, 1,
|
||||||
|
}, {
|
||||||
|
1, 1, 0, 1, 1, 1, 0, 1,
|
||||||
|
0, 0, 0, 1, 0, 0, 0, 1,
|
||||||
|
0, 0, 0, 1, 1, 0, 1, 1,
|
||||||
|
1, 1, 1, 0, 1, 0, 1, 1,
|
||||||
|
1, 0, 0, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 0, 0, 0, 1, 0, 0,
|
||||||
|
0, 0, 0, 0, 1, 0, 1, 1,
|
||||||
|
0, 0, 1, 1, 1, 0, 1, 1,
|
||||||
|
}};
|
||||||
|
|
||||||
|
for (int[] masterKey : MASTER_KEYS) FIXTURE.add(new Des(masterKey));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void stringToBits() {
|
void stringToBits() {
|
||||||
// Chaine vide
|
// Chaine vide
|
||||||
@@ -297,92 +345,124 @@ class DesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void decaleGauche() {
|
void decaleGauche() {
|
||||||
// TODO
|
assertArrayEquals(new int[]{0, 0, 1, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 1));
|
||||||
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)));
|
assertArrayEquals(new int[]{0, 1, 0, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 2));
|
||||||
|
assertArrayEquals(new int[]{0, 0, 0, 1}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 4));
|
||||||
|
assertArrayEquals(new int[]{0, 1, 0, 0}, Des.decaleGauche(new int[]{0, 0, 0, 1}, 10));
|
||||||
|
|
||||||
|
assertArrayEquals(
|
||||||
|
new int[]{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3},
|
||||||
|
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
|
||||||
void generePermutation() {
|
void generePermutation() {
|
||||||
// TODO
|
// Longueur de la permuation
|
||||||
System.out.println(Arrays.toString(Des.generePermutation(64)));
|
assertEquals(64, Des.generePermutation(64).length);
|
||||||
|
|
||||||
|
/* Utilisation d'ensemble pour détecter la présence d'éléments, de doublons, etc... */
|
||||||
|
int[] unePermuation = Des.generePermutation(64);
|
||||||
|
|
||||||
|
Set<Integer> ensemblePermutation = new HashSet<>();
|
||||||
|
for (int valeur : unePermuation) ensemblePermutation.add(valeur);
|
||||||
|
|
||||||
|
// Pas de Doublons
|
||||||
|
assertEquals(ensemblePermutation.size(), unePermuation.length);
|
||||||
|
|
||||||
|
// contiens les éléments de 0 a n-1 (n est la longueur de la liste)
|
||||||
|
for (int i = 0; i < unePermuation.length; i++) {
|
||||||
|
assertTrue(ensemblePermutation.contains(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ne contiens pas n (la longueur de la liste)
|
||||||
|
assertFalse(ensemblePermutation.contains(64));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void permutation() {
|
void permutation() {
|
||||||
// TODO
|
// Erreur si les tailles des paramètres ne sont pas les mêmes
|
||||||
int[] permutation = Des.generePermutation(64);
|
assertThrows(IllegalArgumentException.class, () -> Des.permutation(new int[]{1, 2}, new int[]{0, 1, 2, 3, 4, 5}));
|
||||||
int[] bloc = new int[permutation.length];
|
assertThrows(IllegalArgumentException.class, () -> Des.permutation(new int[]{0, 1, 2, 3, 4, 5}, new int[]{1, 2}));
|
||||||
|
|
||||||
for (int i = 0; i < bloc.length; i++) {
|
// Test de la longueur du résultat
|
||||||
bloc[i] = i;
|
assertEquals(5, Des.permutation(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 3, 4}).length);
|
||||||
}
|
|
||||||
System.out.println(Arrays.toString(bloc));
|
// Test de permuation simple
|
||||||
Des.permutation(permutation, bloc);
|
assertArrayEquals(new int[]{1, 5, 3, 4, 2, 0}, Des.permutation(
|
||||||
System.out.println(Arrays.toString(bloc));
|
new int[]{1, 5, 3, 4, 2, 0},
|
||||||
|
new int[]{0, 1, 2, 3, 4, 5}
|
||||||
|
));
|
||||||
|
|
||||||
|
// Test de permutation complexe
|
||||||
|
assertArrayEquals(new int[]{2, 0, 1, 5, 4, 3}, Des.permutation(
|
||||||
|
new int[]{1, 5, 3, 4, 2, 0},
|
||||||
|
new int[]{3, 2, 4, 1, 5, 0}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void invPermutation() {
|
void invPermutation() {
|
||||||
// TODO
|
// Erreur si les tailles des paramètres ne sont pas les mêmes
|
||||||
int[] permutation = Des.generePermutation(64);
|
assertThrows(IllegalArgumentException.class, () -> Des.invPermutation(new int[]{1, 2}, new int[]{0, 1, 2, 3, 4, 5}));
|
||||||
int[] bloc = new int[permutation.length];
|
assertThrows(IllegalArgumentException.class, () -> Des.invPermutation(new int[]{0, 1, 2, 3, 4, 5}, new int[]{1, 2}));
|
||||||
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: ");
|
// Test de la longueur du résultat
|
||||||
System.out.println(Arrays.toString(bloc));
|
assertEquals(5, Des.invPermutation(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 3, 4}).length);
|
||||||
|
|
||||||
Des.permutation(permutation, bloc);
|
// Test de permuation simple
|
||||||
System.out.println("Bloc apres permuation: ");
|
assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, Des.invPermutation(
|
||||||
System.out.println(Arrays.toString(bloc));
|
new int[]{1, 5, 3, 4, 2, 0},
|
||||||
|
new int[]{1, 5, 3, 4, 2, 0}
|
||||||
|
));
|
||||||
|
|
||||||
Des.invPermutation(permutation, bloc);
|
// Test de permutation complexe
|
||||||
System.out.println("Bloc après Inv-permuation: ");
|
assertArrayEquals(new int[]{3, 2, 4, 1, 5, 0}, Des.invPermutation(
|
||||||
System.out.println(Arrays.toString(bloc));
|
new int[]{1, 5, 3, 4, 2, 0},
|
||||||
|
new int[]{2, 0, 1, 5, 4, 3}
|
||||||
System.out.println(Arrays.equals(bloc, bloc2));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void genereCle() {
|
void genereCle() {
|
||||||
// TODO
|
for (Des des : FIXTURE) {
|
||||||
Des a = new Des();
|
System.out.println(des);
|
||||||
a.genereCle(1);
|
// 1 élément généré
|
||||||
System.out.println(Arrays.toString(a.table_cles.get(0)));
|
assertEquals(1, des.genereCle(1).length);
|
||||||
|
|
||||||
|
// Premier élément est composé de 48 éléments
|
||||||
|
assertEquals(48, des.genereCle(1)[0].length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void fonction_S() {
|
void fonction_S() {
|
||||||
// TODO
|
// TODO
|
||||||
Des des = new Des();
|
Des des = new Des();
|
||||||
//System.out.println(Arrays.toString(des.fonction_S(new int[]{1, 1, 1, 1, 1, 1}, 1)));
|
int[][] S = {
|
||||||
|
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
|
||||||
|
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
|
||||||
|
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
|
||||||
|
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// assertArrayEquals(<attendu>, des.fonction_S(...))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void fonction_F() {
|
void fonction_F() {
|
||||||
// TODO
|
// System.out.println(Arrays.toString(des.fonction_F(1, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17})));
|
||||||
Des des = new Des();
|
|
||||||
//System.out.println(Arrays.toString(des.fonction_F(1, )));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void crypte() {
|
@DisplayName("crypte() & decrypte()")
|
||||||
// TODO
|
void crypteDecrypte() {
|
||||||
Des des = new Des();
|
for (Des des : FIXTURE) {
|
||||||
int[] msg = des.crypte("Bonjour");
|
String message = "Hello World!";
|
||||||
System.out.println("en bit : " + Arrays.toString(msg));
|
int[] messageCrypte = des.crypte(message);
|
||||||
System.out.println("en string :" + Des.bitsToString(msg));
|
assertEquals(message, des.decrypte(messageCrypte));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user