mirror of
https://github.com/LucasVbr/data-encryption-standard.git
synced 2026-05-13 17:12:10 +00:00
Decoupage/Recollage
This commit is contained in:
+32
-34
@@ -17,7 +17,7 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public class Des {
|
public class Des {
|
||||||
|
|
||||||
private static final int TAILLE_BLOC = 64;
|
public static final int TAILLE_BLOC = 64;
|
||||||
private static final int TAILLE_SOUS_BLOC = 32;
|
private static final int TAILLE_SOUS_BLOC = 32;
|
||||||
private static final int NB_ROUND = 1;
|
private static final int NB_ROUND = 1;
|
||||||
private static final int[] TAB_DECALAGE = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
|
private static final int[] TAB_DECALAGE = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
|
||||||
@@ -51,9 +51,6 @@ public class Des {
|
|||||||
{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 ArrayList<int[][]> table_S;
|
|
||||||
|
|
||||||
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,
|
||||||
@@ -65,6 +62,7 @@ public class Des {
|
|||||||
28, 29, 30, 31, 32, 1,
|
28, 29, 30, 31, 32, 1,
|
||||||
};
|
};
|
||||||
public ArrayList<int[]> table_cles;
|
public ArrayList<int[]> table_cles;
|
||||||
|
private final ArrayList<int[][]> table_S;
|
||||||
private int[] masterKey = new int[64];
|
private int[] masterKey = new int[64];
|
||||||
|
|
||||||
public Des() {
|
public Des() {
|
||||||
@@ -264,6 +262,32 @@ public class Des {
|
|||||||
return listePermut;
|
return listePermut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the last character if it contains only zeros
|
||||||
|
*
|
||||||
|
* @param message_decrypte
|
||||||
|
* @return int[] = array without the last 8 bits if they were all zeroes
|
||||||
|
*/
|
||||||
|
public static int[] removeCharNull(int[] message_decrypte) {
|
||||||
|
|
||||||
|
int[][] blocsOfOctet = decoupage(message_decrypte, message_decrypte.length / 8);
|
||||||
|
|
||||||
|
ArrayList<Integer> msg_decrypt = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int[] octet : blocsOfOctet) {
|
||||||
|
StringBuilder stringBuilderOctet = new StringBuilder();
|
||||||
|
for (int i : octet) stringBuilderOctet.append(i);
|
||||||
|
String stringOctet = stringBuilderOctet.toString();
|
||||||
|
int c = Integer.parseInt(stringOctet, 2);
|
||||||
|
|
||||||
|
if ((char) c != 0) {
|
||||||
|
for (int i : octet) msg_decrypt.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg_decrypt.stream().mapToInt(i -> i).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
/* Fonctions */
|
/* Fonctions */
|
||||||
public int[] fonction_S(int[] tab, int index_S) {
|
public int[] fonction_S(int[] tab, int index_S) {
|
||||||
|
|
||||||
@@ -284,32 +308,6 @@ public class Des {
|
|||||||
return resultat;
|
return resultat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the last character if it contains only zeros
|
|
||||||
*
|
|
||||||
* @param message_decrypte
|
|
||||||
* @return int[] = array without the last 8 bits if they were all zeroes
|
|
||||||
*/
|
|
||||||
public static int[] removeCharNull(int[] message_decrypte) {
|
|
||||||
|
|
||||||
int[][] blocsOfOctet = decoupage(message_decrypte, message_decrypte.length / 8);
|
|
||||||
|
|
||||||
ArrayList<Integer> msg_decrypt = new ArrayList<>();
|
|
||||||
|
|
||||||
for (int[] octet: blocsOfOctet){
|
|
||||||
StringBuilder stringBuilderOctet = new StringBuilder();
|
|
||||||
for (int i : octet) stringBuilderOctet.append(i);
|
|
||||||
String stringOctet = stringBuilderOctet.toString();
|
|
||||||
int c = Integer.parseInt(stringOctet, 2);
|
|
||||||
|
|
||||||
if ((char) c != 0) {
|
|
||||||
for (int i : octet) msg_decrypt.add(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg_decrypt.stream().mapToInt(i->i).toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Genere */
|
/* Genere */
|
||||||
public void genereCle(int n) {
|
public void genereCle(int n) {
|
||||||
int[] newCle = new int[56];
|
int[] newCle = new int[56];
|
||||||
@@ -335,15 +333,15 @@ public class Des {
|
|||||||
|
|
||||||
int[][] newS = new int[4][16];
|
int[][] newS = new int[4][16];
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
for (int i = 0; i< 4 ; i++){
|
for (int i = 0; i < 4; i++) {
|
||||||
ArrayList<Integer> liste_valeurs = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15));
|
ArrayList<Integer> liste_valeurs = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
|
||||||
for (int y = 0; y<16 ; y++){
|
for (int y = 0; y < 16; y++) {
|
||||||
int index = random.nextInt(liste_valeurs.size());
|
int index = random.nextInt(liste_valeurs.size());
|
||||||
newS[i][y] = liste_valeurs.get(index);
|
newS[i][y] = liste_valeurs.get(index);
|
||||||
liste_valeurs.remove(index);
|
liste_valeurs.remove(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.table_S.add(n,newS);
|
this.table_S.add(n, newS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] fonction_F(int indice_cle, int[] Dn) {
|
public int[] fonction_F(int indice_cle, int[] Dn) {
|
||||||
|
|||||||
@@ -2,10 +2,26 @@ package cryptography;
|
|||||||
|
|
||||||
public class TripleDes {
|
public class TripleDes {
|
||||||
|
|
||||||
private final Des[] listeDes;
|
private static final int NB_DES = 3;
|
||||||
|
private final Des[] listeDes = new Des[NB_DES];
|
||||||
|
|
||||||
public TripleDes() {
|
public TripleDes() {
|
||||||
this.listeDes = new Des[]{new Des(), new Des(), new Des(),};
|
for (int i = 0; i < NB_DES; i++) {
|
||||||
|
listeDes[i] = new Des();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TripleDes(int[][] masterKeys) {
|
||||||
|
if (masterKeys.length != NB_DES)
|
||||||
|
throw new IllegalArgumentException("masterKeys parameter should have 3 elements");
|
||||||
|
for (int[] masterKey : masterKeys) {
|
||||||
|
if (masterKey.length != Des.TAILLE_BLOC)
|
||||||
|
throw new IllegalArgumentException("Each elements in masterKeys should have 64 bits");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < NB_DES; i++) {
|
||||||
|
listeDes[i] = new Des(masterKeys[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] crypte(String messageClair) {
|
public int[] crypte(String messageClair) {
|
||||||
|
|||||||
@@ -300,7 +300,7 @@ class DesTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("xor")
|
@DisplayName("xor")
|
||||||
void xor() {
|
void xor() {
|
||||||
|
|
||||||
final int[][][] TESTS = {
|
final int[][][] TESTS = {
|
||||||
{
|
{
|
||||||
{0, 1, 0, 1}, // Table 1
|
{0, 1, 0, 1}, // Table 1
|
||||||
@@ -322,66 +322,103 @@ class DesTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("decoupage")
|
@DisplayName("decoupage")
|
||||||
void decoupage() {
|
void decoupage() {
|
||||||
fail(); // TODO
|
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}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}};
|
||||||
|
|
||||||
@Test
|
for (Map.Entry<int[][], int[][]> test : TESTS.entrySet()) {
|
||||||
@DisplayName("generePermutation")
|
assertArrayEquals(test.getValue(), Des.decoupage(test.getKey()[0], test.getKey()[1][0]));
|
||||||
void generePermutation() {
|
System.out.printf("(%s, %s) -> OK\n", Arrays.toString(test.getKey()[0]), test.getKey()[1][0]);
|
||||||
fail(); // TODO
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("permutation")
|
|
||||||
void permutation() {
|
|
||||||
fail(); // TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("invPermutation")
|
|
||||||
void invPermutation() {
|
|
||||||
fail(); // TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("recollageBloc")
|
@DisplayName("recollageBloc")
|
||||||
void recollageBloc() {
|
void recollageBloc() {
|
||||||
fail(); // TODO
|
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}
|
||||||
|
);
|
||||||
|
}};
|
||||||
|
|
||||||
|
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")
|
@DisplayName("decaleGauche")
|
||||||
void decaleGauche() {
|
void decaleGauche() {
|
||||||
fail(); // TODO
|
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
|
@Test
|
||||||
@DisplayName("genereCle")
|
@DisplayName("genereCle")
|
||||||
void genereCle() {
|
void genereCle() {
|
||||||
fail(); // TODO
|
fail("Not yet implemented"); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("fonction_S")
|
@DisplayName("fonction_S")
|
||||||
void fonction_S() {
|
void fonction_S() {
|
||||||
fail(); // TODO
|
fail("Not yet implemented"); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("fonction_F")
|
@DisplayName("fonction_F")
|
||||||
void fonction_F() {
|
void fonction_F() {
|
||||||
fail(); // TODO
|
fail("Not yet implemented"); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("crypte")
|
@DisplayName("crypte")
|
||||||
void crypte() {
|
void crypte() {
|
||||||
fail(); // TODO
|
fail("Not yet implemented"); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("decrypte")
|
@DisplayName("decrypte")
|
||||||
void decrypte() {
|
void decrypte() {
|
||||||
fail(); // TODO
|
fail("Not yet implemented"); // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cryptography;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class TripleDesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void crypte() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void decrypte() {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user