S-DES:: Experiment: 4 Aim

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

IT117-Jaimish Trivedi

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​}};

​private​ String key_input;


​private​ String k1;
​private​ String k2;

​public​ ​Sdes​(String key_input){


​this​.key_input = key_input;
generateKeys();
}

​//Helper function for Left shift operation


​private​ String ​cyclicLeftShift​(String s, ​int​ k){
k = k%s.length();
​return​ s.substring(k) + s.substring(​0​, k);
}

​//Helper function to get numeric value of Character


​private​ ​int​ ​getNumericValue​(​char​ c){
​return​ Character.getNumericValue(c);
}

​//Helper function to generate subkeys


​private​ ​void​ ​generateKeys​(){
String p10_output = ​new​ String();
String half_1 = ​new​ String();
String half_2 = ​new​ String();

​//P10 Permutation Output


StringBuilder temp = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<​this​.p10.length(); i++){
IT117-Jaimish Trivedi

temp.append(​this​.key_input.charAt(getNumericValue(​this​.p10.charAt(i))));
}
p10_output = temp.toString();

​//1st Half of P10 Permutation


temp = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<​5​; i++){
temp.append(getNumericValue(p10_output.charAt(i)));
}
half_1 = temp.toString();

​//2nd Half of P10 Permutation


temp = ​new​ StringBuilder();
​for​(​int​ i=​5​; i<​10​; i++){
temp.append(getNumericValue(p10_output.charAt(i)));
}
half_2 = 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();

System.out.println(​"\tK1 : "​ + ​this​.k1);


System.out.println(​"K2 : "​ + ​this​.k2);
}

​//Helper function for applying IP or IP Inverse, based on the requirement


​private​ String ​initialPermutation​(String text, ​boolean​ doInverse){
StringBuilder temp = ​new​ StringBuilder();
​if​(!doInverse){
​for​(​int​ i=​0​; i<​8​; i++){
temp.append(text.charAt(getNumericValue(ip.charAt(i))));
}
}​else​{
​for​(​int​ i=​0​; i<​8​; i++){
temp.append(text.charAt(getNumericValue(ip_inv.charAt(i))));
}
}
​return​ temp.toString();
}

​//Helper function for applying Expansion permutation


​private​ String ​expansion​(String r){
StringBuilder temp = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<​8​; i++){
temp.append(r.charAt(getNumericValue(ep.charAt(i))));
}
​return​ temp.toString();
}

​//Helper function for XOR Operation


​private​ String ​xor​(String a, String b){
StringBuilder temp = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<a.length(); i++){
temp.append(getNumericValue(a.charAt(i))^getNumericValue(b.charAt(i)));
}
​return​ temp.toString();
}

​//Helper function for getting Index location by converting Binary string to Integer
​private​ ​int​ ​getIndex​(String a){
​return​ Integer.parseInt(a, ​2​);
}

​//Helper function for applying P4 Permutation


​private​ String ​permutation_4​(String a){
StringBuilder temp = ​new​ StringBuilder();
​for​(​int​ i=​0​; i<​4​; i++){
temp.append(a.charAt(getNumericValue(​this​.p4.charAt(i))));
}
IT117-Jaimish Trivedi

​return​ temp.toString();
}

​//Helper function, that applies Complex Function


​//with parameters left bits, right bits and key value
​private​ String ​applyFunction​(String l, String r, String k){
​//Expansion Permutation on Right bits
String ep_output = expansion(r);
System.out.println(​"\tExpansion : "​+ep_output);

​//XORing Right bits with Key


String xor_r_k = xor(ep_output, k);
System.out.println(​"\tXOR with Key : "​+xor_r_k);

​//Two halves of the XOR Output


String r1 = xor_r_k.substring(​0​, 4
​ ​);
String r2 = xor_r_k.substring(​4​, 8​ ​);

​//Get the row and column indices of S-Boxes to be picked


​int​ s1_r = getIndex(​""​+r1.charAt(​0​)+r1.charAt(​3​));
​int​ s1_c = getIndex(​""​+r1.charAt(​1​)+r1.charAt(​2​));
​int​ s2_r = getIndex(​""​+r2.charAt(​0​)+r2.charAt(​3​));
​int​ s2_c = getIndex(​""​+r2.charAt(​1​)+r2.charAt(​2​));

​//Get the Binary representation of the Values picked from S-Boxes


String half_1 = Integer.toBinaryString(s1[s1_r][s1_c]);
String half_2 = Integer.toBinaryString(s2[s2_r][s2_c]);

​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);

​//XORing Left bits and P4 Output


l = xor(l, p4_output);
System.out.println(​"\tLeft bits : "​+l);

​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........"​);

​//Initial Permutation on Plain text


String ip_output = initialPermutation(text, ​false​);
System.out.println(​"\tIP Output : "​+ip_output);

​//Two halves of Plain text


String l = ip_output.substring(​0​, ​4​);
String r = ip_output.substring(​4​, ​8​);
System.out.print(​"\tLeft : "​+l);
System.out.println(​" Right : "​+r);

​//Applying complex function with key K1


String function_output = applyFunction(l, r, ​this​.k1);
System.out.println(​"\tFK1 : "​+function_output);

​//Swapping the left and right bits


String swapped = swap(function_output);
System.out.println(​"\tSwap : "​+swapped);
l = swapped.substring(​0​, ​4​);
r = swapped.substring(​4​, ​8​);
System.out.print(​"\tLeft : "​+l);
System.out.println(​" Right : "​+r);

​//Applying complex function with key K2


function_output = applyFunction(l, r, ​this​.k2);
System.out.println(​"\tFK2 : "​+function_output);

​//Inverse of Initial Permutation


String final_output = initialPermutation(function_output, ​true​);
System.out.println(​"\tIP Inverse : "​+final_output);

​return​ final_output;
}

​//Decryption
​public​ String ​decrypt​(String text){
System.out.println(​"Decrypting........"​);

​//Initial Permutation on Cipher text


String ip_output = initialPermutation(text, ​false​);
System.out.println(​"\tIP Output : "​+ip_output);

​//Two halves of Cipher text


String l = ip_output.substring(​0​, 4
​ ​);
String r = ip_output.substring(​4​, 8​ ​);
IT117-Jaimish Trivedi

System.out.print(​"\tLeft : "​+l);
System.out.println(​" Right : "​+r);

​//Applying complex function with key K2


String function_output = applyFunction(l, r, ​this​.k2);
System.out.println(​"\tFK2 : "​+function_output);

​//Swapping left and right bits


String swapped = swap(function_output);
System.out.println(​"\tSwap : "​+swapped);
l = swapped.substring(​0​, ​4​);
r = swapped.substring(​4​, ​8​);
System.out.print(​"\tLeft : "​+l);
System.out.println(​" Right : "​+r);

​//Applying complex function with key K2


function_output = applyFunction(l, r, ​this​.k1);
System.out.println(​"\tFK1 : "​+function_output);

​//Inverse of Initial Permutation


String final_output = initialPermutation(function_output, ​true​);
System.out.println(​"\tIP Inverse : "​+final_output);

​return​ final_output;
}

​public​ ​static​ ​void​ ​main​(String[] args) {


​//Get the 10-bit Key from user
System.out.print(​"Enter 10-bit key : "​);
Scanner sc = ​new​ Scanner(System.in);
String key = sc.nextLine();
Sdes s = ​new​ Sdes(key);
​//Get the 8-bit Input from user
System.out.print(​"Enter 8-bit text : "​);
String text = sc.nextLine();

String encrypted = s.encrypt(text);


System.out.println(​"Encrypted Text : "​ + encrypted);
System.out.println(​"Decrypted Text : "​ + s.decrypt(encrypted));
}
}
IT117-Jaimish Trivedi

Output:

You might also like