123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- public class playFair_old {
- public static void main(String args[])
- {
- String keyword=null,ptext=null,cipherText=null,string=null,string1=null;
- int Key=0,count=0,tableCount=0;
- char[][] table=new char[6][6];
- int[] chkRepeat=new int[26];
- int[] intchkRepeat=new int[10];
- int[] matrixIndices=null;
- int[] eleLocation=new int[26];
- int[] DeceleLocation=new int[26];
- String[] splitPtext=null;
- ArrayList ptxt=new ArrayList();
- ArrayList ctxt=new ArrayList();
- ArrayList Newptxt=new ArrayList();
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
-
- try {
- System.out.println("Enter the Keyword");
- keyword=br.readLine();
-
- for(int i=0;i<26;i++)
- {
- chkRepeat[i]=0;
- }
- for(int i=0;i<10;i++)
- {
- chkRepeat[i]=0;
- }
-
- for(int i=0;i<6;i++)
- {
- for(int j=0;j<6;j++)
- {
- if(count<keyword.length())
- {
- if(keyword.charAt(count)==' ' || chkRepeat[keyword.charAt(count)-97]==1)
- {
- count++;
- j--;
- }
- else if(count<keyword.length())
- {
- table[i][j]=keyword.charAt(count);
- chkRepeat[(int)keyword.charAt(count)-97]=1;
- count++;
- tableCount++;
- }
- }
- else if(tableCount>25)
- {
- for(int s=0;s<10;s++)
- {
- if(intchkRepeat[s]==0)
- {
- table[i][j]=(char) (s+48);
- intchkRepeat[s]=1;
- break;
- }
- }
- }
- else
- for(int k=0;k<26;k++)
- {
- if(chkRepeat[k]==0)
- {
- table[i][j]=(char) (97+k);
- chkRepeat[k]=1;
- tableCount++;
- break;
- }
- }
- }
-
- }
-
- for(int i=0;i<6;i++)
- {
- for(int j=0;j<6;j++)
- {
-
- System.out.print(" table["+i+"]"+"["+j+"]= "+table[i][j]+"\t");
- }
- System.out.println("");
- }
- System.out.println("Enter the PlainText");
- ptext=br.readLine();
- for(int ListCount=0;ListCount<ptext.length();ListCount++)
- {
- ptxt.add(ptext.charAt(ListCount)+"");
- if(ListCount!=0 && ptxt.get(ListCount).equals(ptxt.get(ListCount-1)) && ListCount%2!=0 )
- {
- ptxt.add(ListCount,"x");
- }
- }
- if(ptxt.size()%2!=0)
- {
- ptxt.add("x");
- }
- {
- System.out.println(ptxt);
- }
- int l=0,n=0;
- matrixIndices=new int[ptxt.size()];
- for(int i=0;i<6;i++)
- {
- for(int j=0;j<6;j++)
- {
- for(int h=0;h<ptxt.size();h++)
- {
-
- string=(String) ptxt.get(h);
- if(chkRepeat[((char)(string.charAt(0))-97)]==0)
- {
- matrixIndices[h]=eleLocation[(char)(string.charAt(0))-97];
- }
-
- else if(ptxt.get(h).equals(table[i][j]+""))
- {
- matrixIndices[h]=i*6+j;
- chkRepeat[table[i][j]-97]=0;
- eleLocation[table[i][j]-97]=i*6+j;
- }
- }
- }
-
- }
- int rowCurrent=0,colCurrent=0,row=0,col=0;
- for(int i=0;i<matrixIndices.length;i++)
- {
- row=matrixIndices[i]/6;
- col=matrixIndices[i]%6;
- System.out.print("["+row+"]"+"["+col+"] ="+matrixIndices[i]+",");
- }
- System.out.println("Ciphertext is: "+ctxt);
- //row=matrixIndices%6
- //col=matrixIndices/6
- row=0;
- col=0;
- for(int i=0;i<ptxt.size();i++)
- {
-
- if(i%2==0)
- {
- string=(String) ptxt.get(i);
- string1=(String) ptxt.get(i+1);
- rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
- colCurrent=eleLocation[(char)(string.charAt(0))-97]%6;
- row=eleLocation[(char)(string1.charAt(0))-97]/6;
- col=eleLocation[(char)(string1.charAt(0))-97]%6;
- if(rowCurrent==row)
- {
- ctxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
- ctxt.add(i+1,""+table[row][(col+1)%6]);
- i++;
- }
- else if(colCurrent==col)
- {
- ctxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
- ctxt.add(i+1,""+table[(row+1)%6][col]);
- i++;
- }
- else
- {
- ctxt.add(i,""+table[rowCurrent][col]);
- }
- }
- else if(i%2==1)
- {
- string=(String) ptxt.get(i);
- string1=(String) ptxt.get(i-1);
- rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
- colCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
- row=eleLocation[(char)(string1.charAt(0))-97]/6;
- col=eleLocation[(char)(string1.charAt(0))-97]%6;
- ctxt.add(i,""+table[rowCurrent][col]);
- }
- }
- System.out.println("Ciphertext is: "+ctxt);
- ////////////////////////////////////////Decryption//////////////////////////////////////////////////
-
-
-
- row=0;
- col=0;
- rowCurrent=0;
- colCurrent=0;
- for(int i=0;i<6;i++)
- {
- for(int j=0;j<6;j++)
- {
- for(int h=0;h<ctxt.size();h++)
- {
-
- string=(String) ctxt.get(h);
- if(chkRepeat[((char)(string.charAt(0))-97)]==0)
- {
- matrixIndices[h]=DeceleLocation[(char)(string.charAt(0))-97];
- }
-
- else if(ctxt.get(h).equals(table[i][j]+""))
- {
- matrixIndices[h]=i*6+j;
- chkRepeat[table[i][j]-97]=0;
- DeceleLocation[table[i][j]-97]=i*6+j;
- }
- }
- }
-
- }
-
- for(int i=0;i<ctxt.size();i++)
- {
-
- if(i%2==0)
- {
- string=(String) ctxt.get(i);
- string1=(String) ctxt.get(i+1);
- rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
- colCurrent=DeceleLocation[(char)(string.charAt(0))-97]%6;
- row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
- col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
- if(rowCurrent==row)
- {
- Newptxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
- Newptxt.add(i+1,""+table[row][(col+1)%6]);
- i++;
- System.out.println("Plaintext is: "+Newptxt);
- }
- else if(colCurrent==col)
- {
- Newptxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
- Newptxt.add(i+1,""+table[(row+1)%6][col]);
- i++;
- System.out.println("Plaintext is: "+Newptxt);
- }
- else
- {
- Newptxt.add(i,""+table[rowCurrent][col]);
- System.out.println("Plaintext is: "+Newptxt);
- }
- }
- else if(i%2==1)
- {
- string=(String) ctxt.get(i);
- string1=(String) ctxt.get(i-1);
- rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
- colCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
- row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
- col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
- Newptxt.add(i,""+table[rowCurrent][col]);
- System.out.println("Plaintext is: "+Newptxt);
- }
- }
- System.out.println("Plaintext is: "+Newptxt);
-
- } catch (Exception e) {
- // TODO Auto-generated catch block
- System.out.println("Exception:"+e.getCause()+" ------ "+e.getLocalizedMessage()+" ------ "+e.getMessage());
- }
-
- }
- }
|