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) {
|
||||
int[] resultat = new int[tab1.length];
|
||||
for (int i = 0; i < resultat.length; i++) {
|
||||
resultat[i] = (tab1[i] + tab2[i]) % 2;
|
||||
resultat[i] = tab1[i] ^ tab2[i];
|
||||
}
|
||||
|
||||
return resultat;
|
||||
@@ -278,6 +278,32 @@ 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);
|
||||
|
||||
|
||||
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 */
|
||||
public void genereCle(int n) {
|
||||
int[] newCle = new int[56];
|
||||
@@ -364,32 +390,6 @@ public class Des {
|
||||
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) {
|
||||
int[][] decoupe = decoupage(messageCode, messageCode.length / TAILLE_BLOC);
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cryptography;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -22,6 +20,7 @@ class DesTest {
|
||||
// }
|
||||
|
||||
@Test
|
||||
@DisplayName("stringToBits")
|
||||
void stringToBits() {
|
||||
HashMap<String, int[]> TESTS = new HashMap<>() {{
|
||||
// Empty string
|
||||
@@ -159,6 +158,7 @@ class DesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("stringToBits")
|
||||
void bitsToString() {
|
||||
HashMap<int[], String> TESTS = new HashMap<>() {{
|
||||
// Empty string
|
||||
@@ -296,62 +296,84 @@ class DesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("decoupage")
|
||||
void decoupage() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generePermutation")
|
||||
void generePermutation() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("permutation")
|
||||
void permutation() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("invPermutation")
|
||||
void invPermutation() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("recollageBloc")
|
||||
void recollageBloc() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("decaleGauche")
|
||||
void decaleGauche() {
|
||||
assertTrue(false); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
void xor() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("genereCle")
|
||||
void genereCle() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("fonction_S")
|
||||
void fonction_S() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("fonction_F")
|
||||
void fonction_F() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("crypte")
|
||||
void crypte() {
|
||||
assertTrue(false); // TODO
|
||||
fail(); // TODO
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("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