import java.util.*;
import java.io.*;
class des
{
static int ip[] = new int[] {2,6,3,1,4,8,5,7};
static int ip_inv[] = new int[] {4,1,3,5,7,2,8,6};
static int p_4[] = new int[] {2,4,3,1};
static int p_10[] = new int[] {3,5,2,7,4,10,1,9,8,6};
static int p_8[] = new int[] {6,3,7,4,8,5,10,9};
static int left_one[] = new int[] {2,3,4,5,1,7,8,9,10,6};
static int left_two[] = new int[] {3,4,5,1,2,8,9,10,6,7};
static int expand[] = new int[] {4,1,2,3,2,3,4,1};
static int sbox0[][] = new int[][] {{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
static int sbox1[][] = new int[][] {{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String plainText, key;
System.out.println(“Enter the 8 bit plain text”);
plainText=sc.nextLine();
System.out.println(“Enter the 10 bit key”);
key=sc.nextLine();
int plain_arr[]=new int[8];
int key_arr[]=new int[10];
for(int i=0;i<plain_arr.length;i++)
plain_arr[i] = Integer.parseInt(plainText.charAt(i)+””);
for(int i=0;i<key_arr.length;i++)
key_arr[i] = Integer.parseInt(key.charAt(i)+””);
key_arr = apply_p10(key_arr);
int key1[],key2[];
key_arr = left_shift_1(key_arr);
int k;
key1 = apply_p8(key_arr);
System.out.println();
System.out.print(“The first key is : “);
for( k=0;k<key1.length;k++)
System.out.print(key1[k]);
key_arr = left_shift_2(key_arr);
key2 = apply_p8(key_arr);
System.out.println();
System.out.print(“The second key is : “);
for(k=0;k<key2.length;k++)
System.out.print(key2[k]);
plain_arr = apply_ip(plain_arr);
System.out.println();
System.out.print(“The plaintext after initial permutation is : “);
for(k=0;k<plain_arr.length;k++)
System.out.print(plain_arr[k]);
int left[],right[];
left = first_half(plain_arr);
right = second_half(plain_arr);
System.out.println();
System.out.print(“Left: “);
for(k=0;k<left.length;k++)
System.out.print(left[k]);
System.out.println();
System.out.print(“Right: “);
for(k=0;k<right.length;k++)
System.out.print(right[k]);
System.out.println();
right = apply_expansion(right);
right = apply_exor8(key1,right);
right = sbox(right);
right = apply_p4(right);
right = apply_exor4(left,right);
plain_arr = final_arr(second_half(plain_arr),right);
System.out.println();
System.out.print(“After applying expansion sbox and p4 on key 1 : “);
for(k=0;k<plain_arr.length;k++)
System.out.print(plain_arr[k]);
left = first_half(plain_arr);
right = second_half(plain_arr);
right = apply_expansion(right);
right = apply_exor8(key2,right);
right = sbox(right);
right = apply_p4(right);
right = apply_exor4(left,right);
plain_arr = final_arr(right,second_half(plain_arr));
System.out.println();
System.out.print(“After applying expansion sbox and p4 on key 2 : “);
for(k=0;k<plain_arr.length;k++)
System.out.print(plain_arr[k]);
System.out.println();
System.out.println(“After Inverse Permutation”);
plain_arr = apply_ipinv(plain_arr);
System.out.print(“Ciphertext is : “);
show(plain_arr);
}
static int[] apply_p10(int a[]){
int temp[] = new int[10];
for(int i=0;i<10;i++)
temp[i] = a[p_10[i]-1];
return temp;
}
static int[] apply_p8(int a[]){
int i;
int temp[] = new int[8];
for( i=0;i<8;i++)
temp[i] = a[p_8[i]-1];
// System.out.print(temp[i]);
return temp;
}
static int[] left_shift_1(int a[]){
int temp[] = new int[10];
for(int i=0;i<10;i++)
temp[i] = a[left_one[i]-1];
return temp;
}
static int[] left_shift_2(int a[]){
int temp[] = new int[10];
for(int i=0;i<10;i++)
temp[i] = a[left_two[i]-1];
return temp;
}
static int[] apply_ip(int a[]){
int temp[] = new int[8];
for(int i=0;i<8;i++)
temp[i] = a[ip[i]-1];
return temp;
}
static int[] apply_ipinv(int a[]){
int temp[] = new int[8];
for(int i=0;i<8;i++)
temp[i] = a[ip_inv[i]-1];
return temp;
}
static int[] apply_expansion(int a[]){
int temp[] = new int[8];
for(int i=0;i<8;i++)
temp[i] = a[expand[i]-1];
return temp;
}
static int[] first_half(int a[]){
int temp[] = new int[4];
for(int i=0;i<4;i++)
temp[i] = a[i];
return temp;
}
static int[] second_half(int a[]){
int temp[] = new int[4];
for(int i=4;i<8;i++)
temp[i-4] = a[i];
return temp;
}
static int[] apply_p4(int a[]){
int b[] = new int[4];
for(int i=0;i<4;i++)
b[i] = a[p_4[i]-1];
return b;
}
static int[] apply_exor8(int a[],int b[]){
int temp[] = new int[8];
for(int i=0;i<8;i++)
temp[i] = a[i]^b[i];
return temp;
}
static int[] apply_exor4(int a[],int b[]){
int temp[] = new int[4];
for(int i=0;i<4;i++)
temp[i] = a[i]^b[i];
return temp;
}
static int[] sbox(int a[]){
String s0 = Integer.toBinaryString(sbox0[a[0]*2+a[3]*1][a[1]*2+a[2]*1]);
if(s0.length()==1) s0 = “0” + s0;
String s1 = Integer.toBinaryString(sbox1[a[4]*2+a[7]*1][a[5]*2+a[6]*1]);
if(s1.length()==1) s1 = “0” + s1;
String temp = s0+s1;
int temp_arr[] = new int[4];
for(int i=0;i<4;i++)
temp_arr[i] = Integer.parseInt(temp.charAt(i)+””);
return temp_arr;
}
static int[] final_arr(int a[],int b[]){
int temp[] = new int[8];
for(int i=0;i<4;i++)
temp[i] = a[i];
for(int i=0;i<4;i++)
temp[i+4] = b[i];
return temp;
}
static void show(int a[]){
for(int i=0;i<a.length;i++)
System.out.print(a[i]+””);
System.out.println();
}
}