S-DES:: Experiment: 4 Aim
S-DES:: Experiment: 4 Aim
S-DES:: Experiment: 4 Aim
EXPERIMENT: 4
AIM: Implementation of S-DES algorithm for data encryption along with key generation of
S-DES.
S-DES:
import java.util.*;
import java.lang.*;
class Sdes{
private String p10 = "9325104867";
private String p8 = "73901482";
private String ep = "20133120";
private String p4 = "2103";
private String ip = "36210754";
private String ip_inv = "43207615";
private int[][] s1 = {{2, 3, 0 , 2}, {2, 1, 3, 1}, {0, 1, 3, 2}, {3, 2, 1, 3}};
private int[][] s2 = {{1, 3, 2, 2}, {3, 1, 0, 2}, {1, 2, 0, 3}, {3, 0, 2, 1}};
temp.append(this.key_input.charAt(getNumericValue(this.p10.charAt(i))));
}
p10_output = temp.toString();
//Left Shifted by 1
String ls1_half_1 = cyclicLeftShift(half_1, 1);
String ls1_half_2 = cyclicLeftShift(half_2, 1 );
//P8 Permutation
temp = new StringBuilder();
for(int i=0; i<this.p8.length(); i++){
int idx = getNumericValue(this.p8.charAt(i));
if(idx<5)
temp.append(ls1_half_1.charAt(idx));
else
temp.append(ls1_half_2.charAt(idx-5));
}
//Key-1
this.k1 = temp.toString();
//Left Shifted by 2
String ls2_half_1 = cyclicLeftShift(ls1_half_1, 2);
String ls2_half_2 = cyclicLeftShift(ls1_half_2, 2 );
//P8 Permutation
temp = new StringBuilder();
for(int i=0; i<this.p8.length(); i++){
int idx = getNumericValue(this.p8.charAt(i));
if(idx<5)
temp.append(ls2_half_1.charAt(idx));
else
temp.append(ls2_half_2.charAt(idx-5));
}
//Key-2
IT117-Jaimish Trivedi
this.k2 = temp.toString();
//Helper function for getting Index location by converting Binary string to Integer
private int getIndex(String a){
return Integer.parseInt(a, 2);
}
return temp.toString();
}
if(half_1.equals("0")) half_1="00";
if(half_2.equals("0")) half_2="00";
if(half_1.equals("1")) half_1="01";
if(half_2.equals("1")) half_2="01";
//P4 Permutation
String p4_output = permutation_4(half_1+half_2);
System.out.println("\tP4 : "+p4_output);
return l+r;
}
//Helper function for swapping left and right bits of Binary String
private String swap(String s){
StringBuilder temp = new StringBuilder();
temp.append(s.substring(4, 8));
temp.append(s.substring(0, 4));
return temp.toString();
IT117-Jaimish Trivedi
//Encryption
public String encrypt(String text){
System.out.println("Encrypting........");
return final_output;
}
//Decryption
public String decrypt(String text){
System.out.println("Decrypting........");
System.out.print("\tLeft : "+l);
System.out.println(" Right : "+r);
return final_output;
}
Output: