Sunday, 16 June 2019

Write a program to implement encryption and decryption using S-DES. Information and Network Security (2170709) GTU



Algorithm/Steps:
               1. First take the plain text in one input file i.e say input.txt.
               2. Then generate 2 8-bit subkeys from the given input 10-bit keys.
               3. Using subkeys and encryption process(as discussed), encrypt the plain text.
               4. Then the cipher text must be saved in separate output file i.e say output.txt.
               5. After encryption, in similar manner perform the decryption in the reverse order of
      keys which will give us the original plain text.
Code:
               import java.io.*;
               import java.util.*;
               public class SimpleDES {
                    static String k1;
                    static String k2;
                    public static void main(String[] args)throws Exception {
                                int ch=0;
                                String l;
                                Scanner sc=new Scanner(System.in);
                                System.out.println("Enter Your Choice:");
                                do{
                                            System.out.println("1.Encryption \n2.Decryption");
                                            ch=sc.nextInt();
                                            sc.nextLine();
                                            System.out.println("Enter key:");
                                            String k=sc.next();
                                            sc.nextLine();
                                            keyGenerator(k);
                                            System.out.println("Enter File name for read string:");
                                            String frname=sc.next();
                                            System.out.println("Enter File name for write string:");
                                            String fwname=sc.next();
                                            FileReaderfr=new FileReader(frname);
                                            BufferedReaderbr=new BufferedReader(fr);
                                            FileWriterfw=new FileWriter(fwname);
                                            BufferedWriterbw=new BufferedWriter(fw);
                                            StringTokenizerst;
                                            String c = "";
                                            if(ch==1){
                                                        while((l=br.readLine())!=null){
                                                                    int c1=0;
                                                                    if((l.length()%2)==0){
                                                                                String[] arr1=new String[(l.length()/2)];
                                                                                for(int i=0;i<(l.length()/2);i++){
                                                                                            arr1[i]=l.substring(c1,c1+2);
                                                                                            c1 = c1+2;
                                                                                }
                                                                                for(int m=0;m<(l.length()/2);m++)
                                                                                {
                                                                                            if(arr1[m].length()==2)
                                                                                            {
                                                                                                        for(int j=0;j<2;j++)
                                                                                                        {
                                                                                                                    int n;
                                                                                                        n=((arr1[m].charAt(j))-65);
                                                                                                        String x=Integer.toBinaryString(n);
                                                                                                        if(x.length()!=4)
                                                                                                        {
                                                                                                                    if(x.length()==1)
                                                                                                                                x="000"+x;
                                                                                                                    elseif(x.length()==2)
                                                                                                                                x="00"+x;
                                                                                                                    elseif(x.length()==3)
                                                                                                                                x="0"+x;
                                                                                                        }
                                                                                                        c=c+x;
                                                                                                        }
                                                                                            }
                                                                                }
                                                                                c1=0;
                                                                                String CT="",PT="",C="";
                                                                                for(int i=0;i<(c.length()/8);i++){
                                                                                            PT=c.substring(c1,c1+8);
                                                                                            C=Encryption(c);
                                                                                            CT=CT+C;
                                                                                            c1 = c1+8;
                                                                                }
                                                                                c1=0;intX;char d=' ';String str="";
                                                                                for(int i=0;i<(CT.length()/4);i++){
                                                                                            PT=CT.substring(c1,c1+4);
                                                                                            X=Integer.parseInt(PT,2);
                                                                                            d=(char)(X+65);
                                                                                            str=str+d;
                                                                                            c1 = c1+4;
                                                                                }
                                                                                bw.write(str);
                                                                    }
                                                                    else{
                                                                                System.out.println("Invalid");
                                                                    }
                                                        }
                                            }
                                            else if(ch==2){
                                                        while((l=br.readLine())!=null){
                                                                    int c1=0;
                                                                    if((l.length()%2)==0){
                                                                                String[] arr1=new String[(l.length()/2)];
                                                                                for(int i=0;i<(l.length()/2);i++){
                                                                                            arr1[i]=l.substring(c1,c1+2);
                                                                                            c1 = c1+2;
                                                                                }
                                                                                for(int m=0;m<(l.length()/2);m++){
                                                                                            if(arr1[m].length()==2)
                                                                                            {
                                                                                                        for(int j=0;j<2;j++)
                                                                                                        {
                                                                                                                    int n;
                                                                                                        n=((arr1[m].charAt(j))-65);
                                                                                                        String x=Integer.toBinaryString(n);
                                                                                                        if(x.length()!=4)
                                                                                                        {
                                                                                                                    if(x.length()==1)
                                                                                                                                x="000"+x;
                                                                                                                    elseif(x.length()==2)
                                                                                                                                x="00"+x;
                                                                                                                    elseif(x.length()==3)
                                                                                                                                x="0"+x;
                                                                                                        }
                                                                                                        c=c+x;
                                                                                            }
                                                                                            }
                                                                                }
                                                                                c1=0;String CT="",PT="",C="";
                                                                                for(int i=0;i<(c.length()/8);i++)
                                                                                {
                                                                                            PT=c.substring(c1,c1+8);
                                                                                            C=Decryption(c);
                                                                                            CT=CT+C;
                                                                                            c1 = c1+8;
                                                                                }
                                                                                c1=0;intX;char d=' ';String str="";
                                                                                for(int i=0;i<(CT.length()/4);i++)
                                                                                {
                                                                                            PT=CT.substring(c1,c1+4);
                                                                                            X=Integer.parseInt(PT,2);
                                                                                            d=(char)(X+65);
                                                                                            str=str+d;
                                                                                            c1 = c1+4;
                                                                                }
                                                                                bw.write(str);
                                                                    }
                                                                    else{
                                                                                System.out.println("Invalid");
                                                                    }
                                                        }
                                            }
                                            br.close();
                                            bw.close();
                                }while(ch!=0);
                    }
                    static String Decryption(String PT){
                                String CT = "";
                                char[] c1=PT.toCharArray();PT=IP(c1);
                                String L=PT.substring(0,4);
                                char[] Left=L.toCharArray();
                                String R=PT.substring(4,8);
                                char[] left=R.toCharArray();
                                char[] Right=R.toCharArray();PT=EP(Right);
                                Right=PT.toCharArray();
                                char[] a1=k2.toCharArray();
                                R=xor(Right,a1);
                                String s0=R.substring(0,4);
                                String s1=R.substring(4,8);
                                char[] ss0=s0.toCharArray();s0=S0(ss0);
                                char[] ss1=s1.toCharArray();s1=S1(ss1);
                                String p4=s0+s1;a1=p4.toCharArray();
                                p4=P4(a1);a1=p4.toCharArray();
                                R=xor(Left,a1);String right=R;
                                Right=R.toCharArray();PT=EP(Right);
                                Right=PT.toCharArray();
                                a1=k1.toCharArray();R=xor(Right,a1);
                                s0=R.substring(0,4);s1=R.substring(4,8);
                                ss0=s0.toCharArray();s0=S0(ss0);
                                ss1=s1.toCharArray(); s1=S1(ss1);
                                p4=s0+s1;a1=p4.toCharArray();
                                p4=P4(a1);a1=p4.toCharArray();
                                R=xor(left,a1);CT=R+right;
                                a1=CT.toCharArray();
                                CT=IPI(a1);return CT;
                    }
                    static String Encryption(String PT){
                                String CT = "";
                                char[] c1=PT.toCharArray();PT=IP(c1);
                                String L=PT.substring(0,4);
                                char[] Left=L.toCharArray();
                                String R=PT.substring(4,8);
                                char[] left=R.toCharArray();
                                char[] Right=R.toCharArray();PT=EP(Right);
                                Right=PT.toCharArray();
                                char[] a1=k1.toCharArray();
                                R=xor(Right,a1);
                                String s0=R.substring(0,4);
                                String s1=R.substring(4,8);
                                char[] ss0=s0.toCharArray();s0=S0(ss0);
                                char[] ss1=s1.toCharArray();s1=S1(ss1);
                                String p4=s0+s1;a1=p4.toCharArray();
                                p4=P4(a1);a1=p4.toCharArray();
                                R=xor(Left,a1);String right=R;
                                Right=R.toCharArray();PT=EP(Right);
                                Right=PT.toCharArray();
                                a1=k2.toCharArray();R=xor(Right,a1);
                                s0=R.substring(0,4);s1=R.substring(4,8);
                                ss0=s0.toCharArray();s0=S0(ss0);
                                ss1=s1.toCharArray(); s1=S1(ss1);
                                p4=s0+s1;a1=p4.toCharArray();
                                p4=P4(a1);a1=p4.toCharArray();
                                R=xor(left,a1);CT=R+right;
                                a1=CT.toCharArray();
                                CT=IPI(a1);return CT;
                    }
                    static String S0( char input[] ) {
                                int SBox0[][] = {{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2} };
                                char rowChars[] = new char[2];
                                char columnChars[] = new char[2];
                                rowChars[0] = input[0];
                                rowChars[1] = input[3];
                                columnChars[0] = input[1];
                                columnChars[1] = input[2];
                                String rowString = new String(rowChars);
                                String columnString = new String(columnChars);
                    int result = SBox0[Integer.parseInt(rowString,2)][Integer.parseInt(columnString,2)];
                                String resultString = Integer.toBinaryString(result);
                                if (resultString.equals("1"))
                                resultString = "01";
                                if (resultString.equals("0"))
                                resultString = "00";
                                char output[] = new char[2];
                                output[0] = resultString.charAt(0);
                                output[1] = resultString.charAt(1);
                                String str=new String(output);
                                return str;
                    }
                    static String S1( char input[] ) {
                                int SBox1[][] = {{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3} };
                                char rowChars[] = new char[2];
                                char columnChars[] = new char[2];
                                rowChars[0] = input[0];
                                rowChars[1] = input[3];
                                columnChars[0] = input[1];
                                columnChars[1] = input[2];
                                String rowString = new String(rowChars);
                                String columnString = new String(columnChars);
                    int result = SBox1[Integer.parseInt(rowString,2)][Integer.parseInt(columnString,2)];
                                String resultString = Integer.toBinaryString(result);
                                if (resultString.equals("1"))
                                resultString = "01";
                                if (resultString.equals("0"))
                                resultString = "00";
                                char output[] = new char[2];
                                output[0] = resultString.charAt(0);
                                output[1] = resultString.charAt(1);
                                String str=new String(output);
                                return str;
                    }
                    static String xor( char array1[], char array2[] ) {
                                if ( array1.length == 8 ) {
                                            char result[]= new char[8];
                                            for ( inti = 0; i< 8; i++ ) {
                                            if ( array1[i] == array2[i] )
                                                result[i] = '0';
                                                else
                                                result[i] = '1';
                                            }
                                            String str = new String(result);
                                            return str;
                                }
                                else{
                                            char result[] = new char[4];
                                            for ( inti = 0; i< 4; i++ ) {
                                                        if ( array1[i] == array2[i] )
                                                result[i] = '0';
                                                else
                                                result[i] = '1';  
                                            }
                                            String str = new String(result);
                                            return str;
                                }
                    }
                    static String IP(char[] c){
                                int[] m = {2,6,3,1,4,8,5,7};
                                char[] p10=new char[8];
                                for(int i=0;i<8;i++){
                                            p10[i]=c[m[i]-1];
                                }
                                String s=new String(p10);
                                return s;
                    }
                    static String IPI(char[] c){
                                int[] m = {4,1,3,5,7,2,8,6};
                                char[] p10=new char[8];
                                for(int i=0;i<8;i++){
                                            p10[i]=c[m[i]-1];
                                }
                                String s=new String(p10);
                                return s;
                    }
                    static String P4(char[] c){
                                int[] m = {2,4,3,1};
                                char[] p10=new char[4];
                                for(int i=0;i<4;i++){
                                            p10[i]=c[m[i]-1];
                                }
                                String s=new String(p10);
                                return s;
                    }
                    static String EP(char[] c){
                                int[] m = {4,1,2,3,2,3,4,1};
                                char[] p10=new char[8];
                                for(int i=0;i<8;i++){
                                            p10[i]=c[m[i]-1];
                                }
                                String s=new String(p10);
                                return s;
                    }
                    static void keyGenerator(String k){
                                char[] c1=k.toCharArray();
                                k=permitution(c1);
                                String s11,s12;
                                s11=k.substring(0,5);
                                s11=cyclicLeftShift(s11,1);
                                s12=k.substring(5,10);
                                s12=cyclicLeftShift(s12,1);
                                k=s11.concat(s12);
                                c1=k.toCharArray();
                                k1=permitution1(c1);
                                String s21,s22;
                                s21=k.substring(0,5);
                                s21=cyclicLeftShift(s21,2);
                                s22=k.substring(5,10);
                                s22=cyclicLeftShift(s22,2);
                                k=s21.concat(s22);
                                c1=k.toCharArray();
                                k2=permitution1(c1);
                    }
                    static String permitution(char[] c){
                                int[] m = {3,5,2,7,4,10,1,9,8,6};
                                char[] p10=new char[10];
                                for(int i=0;i<10;i++){
                                            p10[i]=c[m[i]-1];
                                }
                                String s=new String(p10);
                                return s;
                    }
                    static String permitution1(char[] c){
                                int[] m = {6,3,7,4,8,5,10,9};
                                char[] p8=new char[8];
                                for(int i=0;i<8;i++){
                                            p8[i]=c[m[i]-1];
                                }
                                String s=new String(p8);
                                return s;
                    }
                    static String cyclicLeftShift(String s, int k){
                                   String result="";
                                for(int i=0;i<k;i++){
                                       result = s.substring(1, s.length())+s.charAt(0);
                                       s=result;
                                   }
                                    return s;
                    }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...