From 4459ad264a286f0ae7ba563412811127a6d6fde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= Date: Tue, 4 Oct 2022 15:06:16 +0200 Subject: [PATCH] TODO fonction S --- src/cryptography/Des.java | 83 ++++++++++++++++++++++++++++-- src/cryptography/test/testDes.java | 30 +++++++++-- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/cryptography/Des.java b/src/cryptography/Des.java index 6dcd03b..089310d 100644 --- a/src/cryptography/Des.java +++ b/src/cryptography/Des.java @@ -6,7 +6,6 @@ package cryptography; import java.math.BigInteger; -import java.sql.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; @@ -25,13 +24,13 @@ public class Des { 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 int[] masterKey = new int[64]; - private ArrayList table_cles; + private final int[] masterKey = new int[64]; + public ArrayList table_cles; public Des() { Random random = new Random(); for (int i = 0; i < masterKey.length; i++) { - this.masterKey[0] = random.nextInt(2); + this.masterKey[i] = random.nextInt(2); } this.table_cles = new ArrayList<>(); } @@ -103,18 +102,92 @@ public class Des { if (i % z == 0 && i > 0) { y++; } - newTab[y][i - y * z] = i; + newTab[y][i - y * z] = bloc[i]; } return newTab; } return null; } + public static int[] recollageBloc(int[][] blocs) { + int[] bloc = new int[blocs.length * blocs[0].length]; + int y = 0; + for (int[] ints : blocs) { + for (int anInt : ints) { + bloc[y] = anInt; + y++; + } + } + + return bloc; + } + + public static int[] decaleGauche(int[] blocs, int nbCran) { + int[] newBloc = new int[blocs.length]; + + for (int i = 0; i < blocs.length ; i++){ + int y = (i + nbCran) % blocs.length; + newBloc[i] = blocs[y]; + } + + return newBloc; + } + + public static int[] xor(int[] tab1, int[] tab2) { + int[] resultat = new int[tab1.length]; + + for (int i = 0; i < resultat.length ; i++) { + resultat[i] = (tab1[i] + tab2[i]) % 2; + } + + return resultat; + } + + public void genereCle(int n) { + int[] newCle = new int[56]; + int[] lastCle = new int[48]; + int[] permInit = generePermutation(newCle.length); + permutation(permInit, newCle); + + for ( int i = 0; i < newCle.length ; i++){ + newCle[i] = this.masterKey[i]; + } + + int[][] cleDecoupe = decoupage(newCle, 2); + assert cleDecoupe != null; + cleDecoupe[0] = decaleGauche(cleDecoupe[0], TAB_DECALAGE[n]); + cleDecoupe[1] = decaleGauche(cleDecoupe[1], TAB_DECALAGE[n]); + + newCle = recollageBloc(cleDecoupe); + + int[] lastPerm = generePermutation(lastCle.length); + for ( int i = 0; i < lastCle.length ; i++){ + lastCle[i] = newCle[i]; + } + 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 + // 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; + } + + public static int[] fonction_F( int[] uneCle, int[] unD){ + return null; + } + private static int[] crypte(String message_clair) { // int[] msg_crypte = message_clair.byt return null; // Todo Bouchon } + public static String decrypte( int[] messageCode){ + return null; + } + } diff --git a/src/cryptography/test/testDes.java b/src/cryptography/test/testDes.java index 8640902..dad49a9 100644 --- a/src/cryptography/test/testDes.java +++ b/src/cryptography/test/testDes.java @@ -54,9 +54,29 @@ public class testDes { } public static void testDecoupage() { - System.out.println(Arrays.deepToString(Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4))); + } + public static void testRecollage() { + int[][] decoupage = Des.decoupage(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, 4); + + System.out.println(Arrays.deepToString(decoupage)); + assert decoupage != null; + System.out.println(Arrays.toString(Des.recollageBloc(decoupage))); + } + + public static void testDecaleGauche() { + 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))); + } + + public static void testXor() { + System.out.println(Arrays.toString(Des.xor(new int[]{1, 1, 0, 0}, new int[]{1, 0, 1, 0}))); + } + + public static void testGenereCle() { + Des a = new Des(); + a.genereCle(1); + System.out.println(Arrays.toString(a.table_cles.get(0))); } public static void main(String[] args) { @@ -64,7 +84,11 @@ public class testDes { // testBitsToString(); // testGenerePermutation(); // testPermuation(); - //testInvPermuation(); - testDecoupage(); + // testInvPermuation(); + // testDecoupage(); + // testRecollage(); + // testDecaleGauche(); + // testXor(); + testGenereCle(); } }