Decoupage/Recollage

This commit is contained in:
Lucàs
2022-10-27 22:18:30 +02:00
parent 8b594bfe84
commit 51a98e486f
4 changed files with 127 additions and 62 deletions
+32 -34
View File
@@ -17,7 +17,7 @@ import java.util.Random;
*/
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 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};
@@ -51,9 +51,6 @@ public class Des {
{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 = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
@@ -65,6 +62,7 @@ public class Des {
28, 29, 30, 31, 32, 1,
};
public ArrayList<int[]> table_cles;
private final ArrayList<int[][]> table_S;
private int[] masterKey = new int[64];
public Des() {
@@ -264,6 +262,32 @@ public class Des {
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 */
public int[] fonction_S(int[] tab, int index_S) {
@@ -284,32 +308,6 @@ public class Des {
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 */
public void genereCle(int n) {
int[] newCle = new int[56];
@@ -335,15 +333,15 @@ public class Des {
int[][] newS = new int[4][16];
Random random = new Random();
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));
for (int y = 0; y<16 ; y++){
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));
for (int y = 0; y < 16; y++) {
int index = random.nextInt(liste_valeurs.size());
newS[i][y] = liste_valeurs.get(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) {
+18 -2
View File
@@ -2,10 +2,26 @@ package cryptography;
public class TripleDes {
private final Des[] listeDes;
private static final int NB_DES = 3;
private final Des[] listeDes = new Des[NB_DES];
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) {
+63 -26
View File
@@ -300,7 +300,7 @@ class DesTest {
@Test
@DisplayName("xor")
void xor() {
final int[][][] TESTS = {
{
{0, 1, 0, 1}, // Table 1
@@ -322,66 +322,103 @@ class DesTest {
@Test
@DisplayName("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
@DisplayName("generePermutation")
void generePermutation() {
fail(); // TODO
}
@Test
@DisplayName("permutation")
void permutation() {
fail(); // TODO
}
@Test
@DisplayName("invPermutation")
void invPermutation() {
fail(); // TODO
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
@DisplayName("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
@DisplayName("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
@DisplayName("genereCle")
void genereCle() {
fail(); // TODO
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("fonction_S")
void fonction_S() {
fail(); // TODO
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("fonction_F")
void fonction_F() {
fail(); // TODO
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("crypte")
void crypte() {
fail(); // TODO
fail("Not yet implemented"); // TODO
}
@Test
@DisplayName("decrypte")
void decrypte() {
fail(); // TODO
fail("Not yet implemented"); // TODO
}
}
+14
View File
@@ -0,0 +1,14 @@
package cryptography;
import org.junit.jupiter.api.Test;
class TripleDesTest {
@Test
void crypte() {
}
@Test
void decrypte() {
}
}