mirror of
https://github.com/LucasVbr/data-encryption-standard.git
synced 2026-05-13 17:12:10 +00:00
edit xor and xor tests
This commit is contained in:
+27
-27
@@ -186,7 +186,7 @@ public class Des {
|
|||||||
public static int[] xor(int[] tab1, int[] tab2) {
|
public static int[] xor(int[] tab1, int[] tab2) {
|
||||||
int[] resultat = new int[tab1.length];
|
int[] resultat = new int[tab1.length];
|
||||||
for (int i = 0; i < resultat.length; i++) {
|
for (int i = 0; i < resultat.length; i++) {
|
||||||
resultat[i] = (tab1[i] + tab2[i]) % 2;
|
resultat[i] = tab1[i] ^ tab2[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultat;
|
return resultat;
|
||||||
@@ -278,6 +278,32 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
|
int[] octet = blocsOfOctet[blocsOfOctet.length - 1];
|
||||||
|
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) {
|
||||||
|
int[] newTab = new int[message_decrypte.length - 8];
|
||||||
|
System.arraycopy(message_decrypte, 0, newTab, 0, newTab.length);
|
||||||
|
return newTab;
|
||||||
|
} else {
|
||||||
|
return message_decrypte;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Genere */
|
/* Genere */
|
||||||
public void genereCle(int n) {
|
public void genereCle(int n) {
|
||||||
int[] newCle = new int[56];
|
int[] newCle = new int[56];
|
||||||
@@ -364,32 +390,6 @@ public class Des {
|
|||||||
return recollageBloc(msg_crypte_bit);
|
return recollageBloc(msg_crypte_bit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
|
|
||||||
int[] octet = blocsOfOctet[blocsOfOctet.length-1];
|
|
||||||
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){
|
|
||||||
int[] newTab = new int[message_decrypte.length-8];
|
|
||||||
System.arraycopy(message_decrypte, 0, newTab, 0, newTab.length);
|
|
||||||
return newTab;
|
|
||||||
}else{
|
|
||||||
return message_decrypte;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String decrypte(int[] messageCode) {
|
public String decrypte(int[] messageCode) {
|
||||||
int[][] decoupe = decoupage(messageCode, messageCode.length / TAILLE_BLOC);
|
int[][] decoupe = decoupage(messageCode, messageCode.length / TAILLE_BLOC);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package cryptography;
|
package cryptography;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Order;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -22,6 +20,7 @@ class DesTest {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("stringToBits")
|
||||||
void stringToBits() {
|
void stringToBits() {
|
||||||
HashMap<String, int[]> TESTS = new HashMap<>() {{
|
HashMap<String, int[]> TESTS = new HashMap<>() {{
|
||||||
// Empty string
|
// Empty string
|
||||||
@@ -159,6 +158,7 @@ class DesTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("stringToBits")
|
||||||
void bitsToString() {
|
void bitsToString() {
|
||||||
HashMap<int[], String> TESTS = new HashMap<>() {{
|
HashMap<int[], String> TESTS = new HashMap<>() {{
|
||||||
// Empty string
|
// Empty string
|
||||||
@@ -296,62 +296,84 @@ class DesTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("decoupage")
|
||||||
void decoupage() {
|
void decoupage() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("generePermutation")
|
||||||
void generePermutation() {
|
void generePermutation() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("permutation")
|
||||||
void permutation() {
|
void permutation() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("invPermutation")
|
||||||
void invPermutation() {
|
void invPermutation() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("recollageBloc")
|
||||||
void recollageBloc() {
|
void recollageBloc() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("decaleGauche")
|
||||||
void decaleGauche() {
|
void decaleGauche() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void xor() {
|
|
||||||
assertTrue(false); // TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("genereCle")
|
||||||
void genereCle() {
|
void genereCle() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("fonction_S")
|
||||||
void fonction_S() {
|
void fonction_S() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("fonction_F")
|
||||||
void fonction_F() {
|
void fonction_F() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("crypte")
|
||||||
void crypte() {
|
void crypte() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("decrypte")
|
||||||
void decrypte() {
|
void decrypte() {
|
||||||
assertTrue(false); // TODO
|
fail(); // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("xor")
|
||||||
|
void xor() {
|
||||||
|
final int[][][] TESTS = {
|
||||||
|
{
|
||||||
|
{0, 1, 0, 1}, // Table 1
|
||||||
|
{0, 0, 1, 1}, // Table 2
|
||||||
|
{0, 1, 1, 0}, // Result expected
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[][] test : TESTS) {
|
||||||
|
assertArrayEquals(Des.xor(test[0], test[1]), test[2]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user