You are on page 1of 5

/* * To change this template, choose Tools * and open the template in the editor.

*/ package javaapplication1; import import import import java.io.BufferedReader; java.io.IOException; java.io.InputStreamReader; java.util.StringTokenizer;

Templates

/** * * @author Ambuj */ public class PlayFair { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); char key[][]={{'l','g','d','b','a'},{'q','m','h','e','c'},{'u','r','n',' i','f'},{'x','v','s','o','k'},{'z','y','w','t','p'}}; System.out.println("Enter the string"); String s=br.readLine(); StringTokenizer st = new StringTokenizer(s," ",false); String t="",t1=""; while (st.hasMoreElements()) { t += st.nextElement(); } String pt=t; pt=pt.concat("ffff"); System.out.println("String is"+t); // StringBuffer sb=new StringBuffer(t); StringBuffer sb1=new StringBuffer(pt); int cap=sb1.capacity(); System.out.println("string is "+sb1); int i=0,j=0; char b; int count=0; while(j<t.length()) { char a=t.charAt(j); System.out.println("char at i is "+a); if(j==(t.length())-1) b='x'; else b=t.charAt(j+1); System.out.println("char at i+1 is "+b); if(a==b) { sb1.setCharAt(i, a); // System.out.println("a= "+a); sb1.insert(i+1,"x"); // System.out.println(sb1);

sb1.setCharAt(i+2, b); //System.out.println("b= "+b); i=i+3; count++; } else { //sb1.append('x'); sb1.setCharAt(i, a); // System.out.println("a= "+a); sb1.setCharAt(i+1, b); // System.out.println("a= "+b); i=i+2; } j=j+2; System.out.println("value of j "+j); } for(i=0;i<sb1.length();i++) { if((sb1.length()%2)!=0) { sb1.append('x'); } } System.out.println("new String is "+sb1+" "+sb1.length()); //Encryption of plain text if(count==1) sb1.delete(t.length()+count,sb1.length()); else sb1.delete(t.length()+1+count, sb1.length()); System.out.println(sb1+" "+count); for(i=0;i<5;i++) { for(j=0;j<5;j++) { //System.out.print("\t"+key[i][j]); } //System.out.println(""); } int row,colm; int ro[]=new int[50]; int col[]=new int[50]; for(i=0;i<sb1.length();i++) { char c=sb1.charAt(i); for(j=0;j<5;j++) { for(int k=0;k<5;k++) { if(c==key[j][k]) { ro[i]=j; col[i]=k; } } } } i=0;

while(i<sb1.length()) { //row operation if(ro[i]==ro[i+1]) { int x=ro[i]; int y=col[i]; int x1=ro[i+1]; int y1=(col[(i+1)]); char ch=key[x][(y+1)%5]; char ch1=key[x1][(y1+1)%5]; sb1.setCharAt(i, ch); sb1.setCharAt(i+1, ch1); i=i+2; } else if(col[i]==col[i+1]) { int x=ro[i]; int y=col[i]; int x1=ro[i+1]; int y1=col[i+1]; char ch=key[(x+1)%5][y]; char ch1=key[(x1+1)%5][y1]; sb1.setCharAt(i, ch); sb1.setCharAt(i+1, ch1); i=i+2; } else { int x=ro[i]; int y=col[i]; int x1=ro[i+1]; int y1=col[i+1]; char ch=key[x][y1]; char ch1=key[x1][y]; sb1.setCharAt(i, ch); sb1.setCharAt(i+1, ch1); i=i+2; } //column operation } System.out.println("String is After Encryption "+sb1); //program for decryption i=0; for(i=0;i<sb1.length();i++) { char c=sb1.charAt(i); System.out.println("vlaue of char c is "+c); for(j=0;j<5;j++) { for(int k=0;k<5;k++) { if(c==key[j][k]) {

ro[i]=j; col[i]=k; } } } } i=0; StringBuffer sb2=new StringBuffer(sb1); while(i<sb1.length()) { //row operation if(ro[i]==ro[i+1]) { int x=ro[i]; int y=col[i]; System.out.println("value of x and y is "+x+" "+y); int x1=ro[i+1]; int y1=(col[(i+1)]); if(y-1<0) y=y+4; if(y1-1<0) y1=y1+4; char ch=key[x][(y-1)%5]; char ch1=key[x1][(y1-1)%5]; sb2.setCharAt(i, ch); sb2.setCharAt(i+1, ch1); System.out.println("row same "+sb2); i=i+2; } else if(col[i]==col[i+1]) { int x=ro[i]; int y=col[i]; System.out.println("value of x and y is "+x+" "+y); int x1=ro[i+1]; int y1=col[i+1]; if(x-1<0) x=x-1+5; if(x1-1<0) x1=x1-1+5; char ch=key[(x-1)%5][y]; char ch1=key[(x1-1)%5][y1]; sb2.setCharAt(i, ch); sb2.setCharAt(i+1, ch1); i=i+2; } else { int x=ro[i]; int y=col[i]; System.out.println("value of x and y is "+x+" "+y); int x1=ro[i+1]; int y1=col[i+1]; char ch=key[x][y1]; char ch1=key[x1][y]; sb2.setCharAt(i, ch); sb2.setCharAt(i+1, ch1);

i=i+2; } //column operation System.out.println("String is After Decryption "+sb2); } System.out.println("String is After Decryption "+sb2); } }

You might also like