Lab Expt 7
Aim of the Experiment: Implementation of S-DES algorithm for data encryption
About the experiment:
Simplified Data Encryption Standard is a simplest version of Data Encryption Standard which
only takes a 10-bit key and 8-bit plain text, whereas Data Encryption Standard, or also DES as a
short form proceeds the plaintext of 64-bit.
The S-DES encryption algorithm takes an 8-bit block of plaintext (example: 10111101) and a 10-
bit key as input and produces an 8-bit block of ciphertext as output. The S-DES decryption
algorithm takes an 8-bit block of ciphertext and the same 10-bit key used to produce that
ciphertext as input and produces the original 8-bit block of plaintext.
Simplified DES, developed by Professor Edward Schaefer of Santa Clara University, is an
educational rather than a secure encryption algorithm. It has similar properties and structure to
DES with much smaller parameters.
PROGRAM CODE:-
import java.util.Arrays;
public class SDES {
private static final int[] IP = {1, 5, 2, 0, 3, 7, 4, 6};
private static final int[] IP_INV = {3, 0, 2, 4, 6, 1, 7, 5};
private static final int[] P10 = {2, 4, 1, 6, 3, 9, 0, 8, 7, 5};
private static final int[] P4 = {1, 3, 2, 0};
private static final int[][] S0 = {{1, 0, 3, 2}, {3, 2, 1, 0}, {0, 2, 1, 3}, {3, 1, 3, 2}};
private static final int[][] S1 = {{0, 1, 2, 3}, {2, 0, 1, 3}, {3, 0, 1, 2}, {2, 1, 0, 3}};
public static int[] permute(int[] input, int[] table) {
int[] result = new int[table.length];
for (int i = 0; i < table.length; i++) result[i] = input[table[i]];
return result;
}
public static int[] xor(int[] a, int[] b) {
int[] result = new int[a.length];
for (int i = 0; i < a.length; i++) result[i] = a[i] ^ b[i];
return result;
}
public static int[] sBoxSubstitution(int[] input, int[][] sBox) {
int row = (input[0] << 1) | input[3], col = (input[1] << 1) | input[2];
int value = sBox[row][col];
return new int[]{(value >> 1) & 1, value & 1};
}
public static int[] f(int[] right, int[] key) {
int[] expanded = {right[3], right[0], right[1], right[2]};
int[] xorResult = xor(expanded, key);
int[] left = Arrays.copyOfRange(xorResult, 0, 4);
int[] rightPart = Arrays.copyOfRange(xorResult, 4, 8);
left = sBoxSubstitution(left, S0);
rightPart = sBoxSubstitution(rightPart, S1);
int[] combined = {left[0], left[1], rightPart[0], rightPart[1]};
return permute(combined, P4);
}
public static int[][] generateKeys(int[] key) {
key = permute(key, P10);
key = leftShift(key, 1); int[] K1 = permute(key, P4);
key = leftShift(key, 2); int[] K2 = permute(key, P4);
return new int[][]{K1, K2};
}
public static int[] leftShift(int[] key, int shifts) {
for (int i = 0; i < shifts; i++) {
key = leftShiftByOne(key, 5);
key = leftShiftByOne(key, 10);
}
return key;
}
public static int[] leftShiftByOne(int[] arr, int length) {
int[] result = new int[length];
System.arraycopy(arr, 1, result, 0, length - 1);
result[length - 1] = arr[0];
return result;
}
public static int[] encrypt(int[] plaintext, int[] key) {
int[][] keys = generateKeys(key);
plaintext = permute(plaintext, IP);
int[] left = Arrays.copyOfRange(plaintext, 0, 4);
int[] right = Arrays.copyOfRange(plaintext, 4, 8);
left = xor(left, f(right, keys[0]));
int[] temp = left;
left = right;
right = temp;
left = xor(left, f(right, keys[1]));
int[] combined = new int[8];
System.arraycopy(left, 0, combined, 0, 4);
System.arraycopy(right, 0, combined, 4, 4);
return permute(combined, IP_INV);
}
public static void main(String[] args) {
int[] key = {1, 0, 1, 0, 1, 1, 0, 1, 0, 1};
int[] plaintext = {0, 0, 1, 1, 0, 1, 1, 0};
System.out.println("Ciphertext: " + Arrays.toString(encrypt(plaintext, key)));
}
}
OUTPUT:-