This commit is contained in:
Lucàs
2022-10-11 15:13:18 +02:00
parent 4459ad264a
commit 3cc49f6515
4 changed files with 106 additions and 32 deletions
Binary file not shown.
BIN
View File
Binary file not shown.
+71 -17
View File
@@ -7,7 +7,6 @@ package cryptography;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
@@ -21,8 +20,22 @@ public class Des {
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[] PERM_INITIALE = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7};
private static final 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}};
private static final int[] E = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1};
private static final 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}
};
private static final int[] E = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1,
};
private final int[] masterKey = new int[64];
public ArrayList<int[]> table_cles;
@@ -149,9 +162,7 @@ public class Des {
int[] permInit = generePermutation(newCle.length);
permutation(permInit, newCle);
for ( int i = 0; i < newCle.length ; i++){
newCle[i] = this.masterKey[i];
}
System.arraycopy(this.masterKey, 0, newCle, 0, newCle.length);
int[][] cleDecoupe = decoupage(newCle, 2);
assert cleDecoupe != null;
@@ -161,27 +172,70 @@ public class Des {
newCle = recollageBloc(cleDecoupe);
int[] lastPerm = generePermutation(lastCle.length);
for ( int i = 0; i < lastCle.length ; i++){
lastCle[i] = newCle[i];
}
System.arraycopy(newCle, 0, lastCle, 0, lastCle.length);
permutation(lastPerm, lastCle);
this.table_cles.add(lastCle);
}
public static int[] fonction_S(int[] tab) {
//xor entre this.E et la cle trouvé à cette ronde
//découpage en 8 blocs de 6 bits
String l = "" + tab[0] + tab[5];
int ligne = Integer.parseInt(l, 2);
String c = "" + tab[1] + tab[2] + tab[3] + tab[4];
int colonne = Integer.parseInt(c, 2);
// chaque blocs de 6 bits on fait le truc avec bit 1 et bit 6 et l'autre truc pour avoir ligne et colonne de S
return null;
String coordonneeStr = Integer.toString(S[ligne][colonne], 2);
int coordonneeInt = Integer.parseInt(coordonneeStr);
int[] resultat = new int[4];
for (int i = 0; i < resultat.length; i++, coordonneeInt /= 10) {
resultat[i] = coordonneeInt % 10;
}
public static int[] fonction_F( int[] uneCle, int[] unD){
return null;
return resultat;
}
private static int[] crypte(String message_clair) {
// int[] msg_crypte = message_clair.byt
return null; // Todo Bouchon
public int[] fonction_F(int[] unCle) {
//xor entre this.E et la cle trouvé à cette ronde
// this.genereCle(this.table_cles.size());
int[] Dn = xor(E, unCle);
//découpage en 8 blocs de 6 bits
int[][] decoupe = decoupage(Dn, 8);
int[][] bloc = new int[8][4];
for (int i = 0; i < decoupe.length; i++) {
int[] tab = fonction_S(decoupe[i]);
System.arraycopy(tab, 0, bloc[i], 0, tab.length);
}
return recollageBloc(bloc);
}
public int[] crypte(String message_clair) {
int[] msg_bit = stringToBits(message_clair);
int[][] decoupe = decoupage(msg_bit, (int) msg_bit.length / 64);
int[][] msg_crypte_bit = new int[(int)msg_bit.length / 64][64];
for (int n = 0; n < 16; n++) {
this.genereCle(n);
}
for (int i = 0; i < decoupe.length - 1; i++) {
permutation(PERM_INITIALE, decoupe[i]);
int[][] decoupe2 = decoupage(decoupe[i], 2);
for (int n = 0; n < 16; n++) {
// this.genereCle(n);
decoupe2[1] = fonction_F(this.table_cles.get(n));
decoupe2[0] = fonction_F(this.table_cles.get(n));
}
msg_crypte_bit[i] = recollageBloc(decoupe2);
}
return recollageBloc(msg_crypte_bit);
}
public static String decrypte(int[] messageCode) {
+22 -2
View File
@@ -7,7 +7,7 @@ import java.util.Arrays;
public class testDes {
public static void testStringToBits() {
System.out.println(Arrays.toString(Des.stringToBits("Bonjour")));
System.out.println(Arrays.toString(Des.stringToBits("13")));
}
public static void testBitsToString() {
@@ -79,6 +79,21 @@ public class testDes {
System.out.println(Arrays.toString(a.table_cles.get(0)));
}
public static void testFonctionS() {
System.out.println(Arrays.toString(Des.fonction_S(new int[]{1, 1, 1, 1, 1, 1})));
}
public static void testFonctionF() {
Des des = new Des();
// System.out.println(Arrays.toString(des.fonction_F()));
}
private static void testCrypte() {
Des des = new Des();
System.out.println(Arrays.toString(des.crypte("Hello World!")));
}
public static void main(String[] args) {
// testStringToBits();
// testBitsToString();
@@ -89,6 +104,11 @@ public class testDes {
// testRecollage();
// testDecaleGauche();
// testXor();
testGenereCle();
// testGenereCle();
// testFonctionS();
// testFonctionF();
testCrypte();
// testDecrypte();
}
}