You are on page 1of 16

4) Cryptography (Without using Java Security/Cryptography Packages) Generalized Caesar Cipher, Mono-alphabetic Substitution Cipher, Transposition (Columnar) cipher,

P-Box, S-Box, Product Cipher, One Time Pad. Above programs in item no. 4 should be implemented in Java. For these programs, students need to implement the corresponding algorithms themselves in Java. Communication may be done using sockets. For that Java socket programming is to be used.

For setting the path: My Computer->Advance->Environment Variable-> New-> Path-> C:\Program File\Java\jdk1.6.0\bin; -> ok->ok (where ever ur jdk bin file is stored).. Then Go to run->cmd-> C:\Documents and Settings\lcit\Desktop>javac CaesarEncrypt.java C:\Documents and Settings\lcit\Desktop>java CaesarEncrypt divya 5

Generalized Caesar Cipher Sender + receiver


import java.io.*; public class CaesarEncrypt { public String encryptMsg(String s,int key) { int i=0; String cipherMsg=""; s=s.toUpperCase(); for(i=0;i<s.length();i++) {

if(s.charAt(i)>='A' && s.charAt(i)<='Z') { cipherMsg=cipherMsg+(char) ((((s.charAt(i)+key)-'A')%26)+'A'); } else cipherMsg=cipherMsg+s.charAt(i); } return(cipherMsg); } public String decryptMsg(String s,int key) { int i=0; String cipherMsg="";

for(i=0;i<s.length();i++) { if(s.charAt(i)>='A' && s.charAt(i)<='Z') { cipherMsg=cipherMsg+(char) ((((s.charAt(i)-key)+'A')%26)+'A'); } else cipherMsg=cipherMsg+s.charAt(i); } return(cipherMsg); } public static void main(String args[]) { CaesarEncrypt a=new CaesarEncrypt(); CaesarDecrypt a1=new CaesarDecrypt(); String i,j; int k1,k2; k1=Integer.parseInt(args[1]); i=a.encryptMsg(args[0],k1); System.out.println(i); k2=Integer.parseInt(args[1]); j=a1.decryptMsg(args[0],k2); System.out.println(j); } } Output C:\Documents and Settings\lcit\Desktop>javac CaesarEncrypt.java C:\Documents and Settings\lcit\Desktop>java CaesarEncrypt divya 5 INADF divya

MonoAlpha Substitution Cipher Sender + receiver


import java.io.*; public class MonoAlphaEncryption{ String cipherMsg=""; public String encryptMsg(String strMsg) { char a[][]={ {'A','X'},{'B','N'},{'C','B'},{'D','G'},{'E','O'},{'F','J'},{'G','U'},{'H','D'},{'I','M'},{'J','K'} ,{'K','P'},{'L','T'},{'M','A'},{'N','Q'},{'O','L'},{'P','Z'},{'Q','C'},{'R','F'},{'S','Y'},{'T','H' },{'U','W'},{'V','I'},{'W','V'},{'X','R'},{'Y','E'},{'Z','S'} }; int i; try{ strMsg=strMsg.toUpperCase(); for(i=0;i<strMsg.length();i++) {

cipherMsg=cipherMsg+(a[strMsg.charAt(i)-65][1]); } } catch(Exception e) { System.out.println(e); } return(cipherMsg); } public String decryptMsg(String cipherMsg) { String strMsg=""; char a[][]={{'X','A'},{'N','B'},{'B','C'},{'G','D'},{'O','E'},{'J','F'},{'U','G'},{'D','H'},{'M','I'}, {'K','J'},{'P','K'},{'T','L'},{'A','M'},{'Q','N'},{'L','O'},{'Z','P'},{'C','Q'},{'F','R'},{'Y','S'} ,{'H','T'},{'W','U'},{'T','V'},{'V','W'},{'R','X'},{'E','Y'},{'S','Z'}}; int i; try{

cipherMsg=cipherMsg.toUpperCase(); for(i=0;i<cipherMsg.length();i++) {

strMsg=strMsg+(a[cipherMsg.charAt(i)-65][1]); } } catch(Exception e) { System.out.println(e); } return(strMsg); }

public static void main(String args[]) { MonoAlphaEncryption a=new MonoAlphaEncryption(); String i,j; i=a. encryptMsg(args[0]); System.out.println(i); j=a.decryptMsg(args[0]); System.out.println(j); } } Output:C:\Documents and Settings\lcit\Desktop>javac MonoAlphaEncryption.java C:\Documents and Settings\lcit\Desktop>java MonoAlphaEncryption divya GMIEX DIVYA

Transposition (Columnar) cipher Sender and Receiver


import java.io.*; public class ColumnTransSender { String columns1[]={"","","","",""}; public static void main(String s[]) { String columns[]={"","","","",""}; String key="21403"; String msg = "Divya is good girl"; String result = ""; int i; for(i = 0; i < msg.length(); i++) columns[i % key.length()] += msg.charAt(i); for(i=i%key.length();i<key.length();i++) columns[i] += "a"; for(i = 0; i < key.length(); i++) System.out.println("Data is : " + columns[i]); for(i = 0; i < key.length(); i++) { result += columns[Integer.parseInt(""+key.charAt(i))]; } System.out.println("Data in Result : " + result); ColumnTransSender d = new ColumnTransSender(); d.decrypt(result,key); } public void decrypt(String result,String key) { int i=0; for(int j = 0; j<key.length();j++,i+=Integer.parseInt(""+result.length()/key.length())) { columns1[Integer.parseInt(""+key.charAt(j))] += result.substring(i,i+Integer.parseInt(""+result.length()/key.length())); }

for( i = 0; i < columns1.length; i++) System.out.println("Data in again : " + columns1[i]); } } Output

C:\Documents and Settings\lcit\Desktop>java ColumnTransSender Data is : D oi Data is : iior Data is : vsdl Data is : y a Data is : agga Data in Result : vsdliioraggaD oiy a Data in again : D oi Data in again : iior Data in again : vsdl Data in again : y a Data in again : agga

P-Box Sender +receiver


import java.io.*; public class pbox { String[] pbox(String[] strData) { String[] cipherData = new String[8]; cipherData[0]=strData[3]; cipherData[1]=strData[6]; cipherData[2]=strData[0]; cipherData[3]=strData[7]; cipherData[4]=strData[1]; cipherData[5]=strData[2]; cipherData[6]=strData[4]; cipherData[7]=strData[5]; return cipherData; } String[] pbox1(String[] cipherData) { String[] strData = new String[8]; strData[3]=cipherData[0]; strData[6]=cipherData[1]; strData[0]=cipherData[2]; strData[7]=cipherData[3]; strData[1]=cipherData[4]; strData[2]=cipherData[5]; strData[4]=cipherData[6]; strData[5]=cipherData[7]; return strData; }

public static void main(String a[]) { int i=0; pbox p=new pbox(); String[] strData = new String[8]; String[] cipherData = new String[8]; String[] str1Data = new String[8]; String[] cipher1Data = new String[8];

try{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); for(i=0;i<8;i++) { System.out.print("Enter " + (i+1) + " Bit of Data Byte : "); strData[i]=stdin.readLine(); } cipherData=p.pbox(strData); for(i=0;i<8;i++) { System.out.println("The encrypted data" + cipherData[i]); } cipher1Data=p.pbox1(cipherData); for(i=0;i<8;i++) { System.out.println("The decreypted data"+cipher1Data[i]); } } catch(Exception e) { } } } Output :C:\Documents and Settings\lcit\Desktop>javac pbox.java C:\Documents and Settings\lcit\Desktop>java pbox Enter 1 Bit of Data Byte : 1 Enter 2 Bit of Data Byte : 0 Enter 3 Bit of Data Byte : 0 Enter 4 Bit of Data Byte : 1 Enter 5 Bit of Data Byte : 0 Enter 6 Bit of Data Byte : 0 Enter 7 Bit of Data Byte : 1 Enter 8 Bit of Data Byte : 0 The encrypted data--1 The encrypted data--1 The encrypted data--1 The encrypted data--0 The encrypted data--0 The encrypted data--0 The encrypted data--0 The encrypted data--0 The decreypted data--1

The decreypted The decreypted The decreypted The decreypted The decreypted The decreypted The decreypted

data--0 data--0 data--1 data--0 data--0 data--1 data--0

SBOX For Socket Programming First Run the Server then Run Client Port number must be same on both side sender (Client) and receiver (Server) Sender
import java.io.*; import java.net.*; public class SBoxSender { public static void main(String a[]) { int i=0,no=0,newNo=0,j; String cipherNo="",plainNo; String[] strData = new String[8]; String[] cipherData = new String[8]; try{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter 3 Bit Input :"); plainNo=stdin.readLine(); for(i=2;i>=0;i--) { strData[2-i]=""+plainNo.charAt(i); no=no + (int) (Integer.parseInt(strData[2-i]) * (Math.pow(2,2-i))); //Generate Binary to Decimal } System.out.println("Number is : "+ no); for(i=0;i<8;i++) { if(i==no) strData[i]="1"; else strData[i]="0"; } for(i=0;i<8;i++) System.out.println(strData[i]); System.out.println("--------------------------------------------------"); SBoxSender p = new SBoxSender(); cipherData=p.pBoxEncrypt(strData); for(i=0;i<8;i++) System.out.println(cipherData[i]); for(i=0;i<8;i++) {

if(cipherData[i]=="1") newNo = i; } System.out.println("Cipher Number : "+newNo); for(j=2;j>=0;j--) { SBoxSender b = new SBoxSender(); cipherNo+=""+b.getBit(newNo,j); } System.out.println(cipherNo); Socket s1 = new Socket("localhost",4444); OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); dos.writeUTF(cipherNo); } catch(Exception e) { System.out.println(e); } } String[] pBoxEncrypt(String[] strData) { String[] cipherData1 = new String[8]; cipherData1[0]=strData[3]; cipherData1[1]=strData[6]; cipherData1[2]=strData[0]; cipherData1[3]=strData[7]; cipherData1[4]=strData[1]; cipherData1[5]=strData[2]; cipherData1[6]=strData[4]; cipherData1[7]=strData[5]; return cipherData1;

} int getBit(int Num,int pos) { int Ans = 1; Ans = Ans << pos; Ans = Ans & Num; if(Ans == 0) return 0; else return 1; }

} Output C:\Documents and Settings\lcit\Desktop>java SBoxSender Enter 3 Bit Input :000 Number is : 0 1 0 0 0 0 0 0 0 -------------------------------------------------0 0 1 0 0 0 0 0 Cipher Number : 2 010

Receiver
import java.io.*; import java.net.*; public class SBoxReceiver { public static void main(String a[]) { try{ ServerSocket s = new ServerSocket(4444); while(true) { Socket s1=s.accept(); int i=0,no=0,newNo=0,j; String cipherNo="",plainNo; String[] strData = new String[8]; String[] cipherData = new String[8]; InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); plainNo = new String (dis.readUTF()); System.out.println(plainNo);

i)));

for(i=2;i>=0;i--) { strData[2-i]=""+plainNo.charAt(i); no=no + (int) (Integer.parseInt(strData[2-i]) * (Math.pow(2,2//Generate Binary to Decimal } System.out.println("Number is : "+ no); for(i=0;i<8;i++) { if(i==no) strData[i]="1"; else strData[i]="0"; } for(i=0;i<8;i++) System.out.println(strData[i]); System.out.println("--------------------------------------------------"); SBoxReceiver p = new SBoxReceiver(); cipherData=p.pBoxDecrypt(strData); for(i=0;i<8;i++) System.out.println(cipherData[i]); for(i=0;i<8;i++) { if(cipherData[i]=="1") newNo = i; } System.out.println("Cipher Number : "+newNo); for(j=2;j>=0;j--) { SBoxReceiver b = new SBoxReceiver(); cipherNo+=""+b.getBit(newNo,j); } System.out.println(cipherNo); System.out.println("--------------------------------------------------"); } } catch(Exception e) { System.out.println(e); } } String[] pBoxDecrypt(String[] cipherData) { String[] strData1 = new String[8]; strData1[3]=cipherData[0]; strData1[6]=cipherData[1];

strData1[0]=cipherData[2]; strData1[7]=cipherData[3]; strData1[1]=cipherData[4]; strData1[2]=cipherData[5]; strData1[4]=cipherData[6]; strData1[5]=cipherData[7]; return strData1; } int getBit(int Num,int pos) { int Ans = 1; Ans = Ans << pos; Ans = Ans & Num; if(Ans == 0) return 0; else return 1; } } Output:= C:\Documents and Settings\lcit\Desktop>java SBoxReceiver 010 Number is : 2 0 0 1 0 0 0 0 0 -------------------------------------------------1 0 0 0 0 0 0 0 Cipher Number : 0 000 --------------------------------------------------

One Time Pad Sender and Receiver


import java.io.*; public class OneTimePad { public static void main(String a[]) { int i,padLen; String strData,padData,cipherData; char[] buf = new char[1024]; char chr; try{ cipherData=""; BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Data For Encryption : "); strData=stdin.readLine(); BufferedReader fileIn = new BufferedReader(new FileReader("pad.txt")); fileIn.read(buf); padData=new String(buf); padLen=padData.trim().length(); for(i=0;i<strData.length();i++) { cipherData=cipherData + (char) (strData.charAt(i) ^ padData.charAt(i%padLen));; } System.out.println(cipherData);

strData=""; for(i=0;i<cipherData.length();i++) { strData=strData +(char) (cipherData.charAt(i) ^ padData.charAt(i%padLen)); } System.out.println(strData); } catch(Exception e){ System.out.println(e); } } } Output

C:\Documents and Settings\lcit\Desktop>javac OneTimePad.java C:\Documents and Settings\lcit\Desktop>java OneTimePad Enter Data For Encryption : divya the encry divya

You might also like