77 lines
1.5 KiB
Java
77 lines
1.5 KiB
Java
package ch.fhnw.kry;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
class SPNTest {
|
|
|
|
@Test
|
|
void k() {
|
|
var spn = new SPN();
|
|
|
|
final int key = 0xFFFFFFFF;
|
|
assertEquals(0xFFFF, spn.k(key, 0));
|
|
assertEquals(0xFFFF, spn.k(key, 1));
|
|
assertEquals(0xFFFF, spn.k(key, 2));
|
|
}
|
|
|
|
@Test
|
|
void substitution() {
|
|
var spn = new SPN();
|
|
|
|
//given
|
|
int x = 0xEF45;
|
|
int r = 0x051C;
|
|
|
|
//when
|
|
int y = spn.substitution(x, SPN.SBOX_REVERSE);
|
|
|
|
// then
|
|
assertEquals(r, y);
|
|
|
|
// System.out.print(Integer.toHexString(spn.substitution(x)));
|
|
}
|
|
|
|
@Test
|
|
void swapBits() {
|
|
var spn = new SPN();
|
|
|
|
// given
|
|
int x = 0xA;
|
|
int r = 0xC;
|
|
|
|
// when
|
|
int y = spn.swapBits(x, 13, 14);
|
|
int yy = spn.swapBits(x, 14, 13);
|
|
int yyy = spn.swapBits(x, 14, 14);
|
|
|
|
// then
|
|
assertEquals(r, y);
|
|
assertEquals(r, yy);
|
|
assertEquals(x, yyy);
|
|
|
|
assertEquals(0x100A, spn.swapBits(0x80A, 3, 4));
|
|
}
|
|
|
|
@Test
|
|
void permutation() {
|
|
var spn = new SPN();
|
|
|
|
int x = spn.permutation(0xEF45);
|
|
assertEquals(0xCFC5, x);
|
|
}
|
|
|
|
@Test
|
|
void blockEncryptionDecryption() {
|
|
var spn = new SPN();
|
|
|
|
int x = 0x128F;
|
|
int key = 0x11288C00;
|
|
int y = 0xAEB4;
|
|
|
|
assertEquals(y, spn.encryptBlock(key, x));
|
|
assertEquals(x, spn.decryptBlock(key, y));
|
|
}
|
|
}
|