playFair_old.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Iterator;
  7. public class playFair_old {
  8. public static void main(String args[])
  9. {
  10. String keyword=null,ptext=null,cipherText=null,string=null,string1=null;
  11. int Key=0,count=0,tableCount=0;
  12. char[][] table=new char[6][6];
  13. int[] chkRepeat=new int[26];
  14. int[] intchkRepeat=new int[10];
  15. int[] matrixIndices=null;
  16. int[] eleLocation=new int[26];
  17. int[] DeceleLocation=new int[26];
  18. String[] splitPtext=null;
  19. ArrayList ptxt=new ArrayList();
  20. ArrayList ctxt=new ArrayList();
  21. ArrayList Newptxt=new ArrayList();
  22. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  23. try {
  24. System.out.println("Enter the Keyword");
  25. keyword=br.readLine();
  26. for(int i=0;i<26;i++)
  27. {
  28. chkRepeat[i]=0;
  29. }
  30. for(int i=0;i<10;i++)
  31. {
  32. chkRepeat[i]=0;
  33. }
  34. for(int i=0;i<6;i++)
  35. {
  36. for(int j=0;j<6;j++)
  37. {
  38. if(count<keyword.length())
  39. {
  40. if(keyword.charAt(count)==' ' || chkRepeat[keyword.charAt(count)-97]==1)
  41. {
  42. count++;
  43. j--;
  44. }
  45. else if(count<keyword.length())
  46. {
  47. table[i][j]=keyword.charAt(count);
  48. chkRepeat[(int)keyword.charAt(count)-97]=1;
  49. count++;
  50. tableCount++;
  51. }
  52. }
  53. else if(tableCount>25)
  54. {
  55. for(int s=0;s<10;s++)
  56. {
  57. if(intchkRepeat[s]==0)
  58. {
  59. table[i][j]=(char) (s+48);
  60. intchkRepeat[s]=1;
  61. break;
  62. }
  63. }
  64. }
  65. else
  66. for(int k=0;k<26;k++)
  67. {
  68. if(chkRepeat[k]==0)
  69. {
  70. table[i][j]=(char) (97+k);
  71. chkRepeat[k]=1;
  72. tableCount++;
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. for(int i=0;i<6;i++)
  79. {
  80. for(int j=0;j<6;j++)
  81. {
  82. System.out.print(" table["+i+"]"+"["+j+"]= "+table[i][j]+"\t");
  83. }
  84. System.out.println("");
  85. }
  86. System.out.println("Enter the PlainText");
  87. ptext=br.readLine();
  88. for(int ListCount=0;ListCount<ptext.length();ListCount++)
  89. {
  90. ptxt.add(ptext.charAt(ListCount)+"");
  91. if(ListCount!=0 && ptxt.get(ListCount).equals(ptxt.get(ListCount-1)) && ListCount%2!=0 )
  92. {
  93. ptxt.add(ListCount,"x");
  94. }
  95. }
  96. if(ptxt.size()%2!=0)
  97. {
  98. ptxt.add("x");
  99. }
  100. {
  101. System.out.println(ptxt);
  102. }
  103. int l=0,n=0;
  104. matrixIndices=new int[ptxt.size()];
  105. for(int i=0;i<6;i++)
  106. {
  107. for(int j=0;j<6;j++)
  108. {
  109. for(int h=0;h<ptxt.size();h++)
  110. {
  111. string=(String) ptxt.get(h);
  112. if(chkRepeat[((char)(string.charAt(0))-97)]==0)
  113. {
  114. matrixIndices[h]=eleLocation[(char)(string.charAt(0))-97];
  115. }
  116. else if(ptxt.get(h).equals(table[i][j]+""))
  117. {
  118. matrixIndices[h]=i*6+j;
  119. chkRepeat[table[i][j]-97]=0;
  120. eleLocation[table[i][j]-97]=i*6+j;
  121. }
  122. }
  123. }
  124. }
  125. int rowCurrent=0,colCurrent=0,row=0,col=0;
  126. for(int i=0;i<matrixIndices.length;i++)
  127. {
  128. row=matrixIndices[i]/6;
  129. col=matrixIndices[i]%6;
  130. System.out.print("["+row+"]"+"["+col+"] ="+matrixIndices[i]+",");
  131. }
  132. System.out.println("Ciphertext is: "+ctxt);
  133. //row=matrixIndices%6
  134. //col=matrixIndices/6
  135. row=0;
  136. col=0;
  137. for(int i=0;i<ptxt.size();i++)
  138. {
  139. if(i%2==0)
  140. {
  141. string=(String) ptxt.get(i);
  142. string1=(String) ptxt.get(i+1);
  143. rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
  144. colCurrent=eleLocation[(char)(string.charAt(0))-97]%6;
  145. row=eleLocation[(char)(string1.charAt(0))-97]/6;
  146. col=eleLocation[(char)(string1.charAt(0))-97]%6;
  147. if(rowCurrent==row)
  148. {
  149. ctxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
  150. ctxt.add(i+1,""+table[row][(col+1)%6]);
  151. i++;
  152. }
  153. else if(colCurrent==col)
  154. {
  155. ctxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
  156. ctxt.add(i+1,""+table[(row+1)%6][col]);
  157. i++;
  158. }
  159. else
  160. {
  161. ctxt.add(i,""+table[rowCurrent][col]);
  162. }
  163. }
  164. else if(i%2==1)
  165. {
  166. string=(String) ptxt.get(i);
  167. string1=(String) ptxt.get(i-1);
  168. rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
  169. colCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
  170. row=eleLocation[(char)(string1.charAt(0))-97]/6;
  171. col=eleLocation[(char)(string1.charAt(0))-97]%6;
  172. ctxt.add(i,""+table[rowCurrent][col]);
  173. }
  174. }
  175. System.out.println("Ciphertext is: "+ctxt);
  176. ////////////////////////////////////////Decryption//////////////////////////////////////////////////
  177. row=0;
  178. col=0;
  179. rowCurrent=0;
  180. colCurrent=0;
  181. for(int i=0;i<6;i++)
  182. {
  183. for(int j=0;j<6;j++)
  184. {
  185. for(int h=0;h<ctxt.size();h++)
  186. {
  187. string=(String) ctxt.get(h);
  188. if(chkRepeat[((char)(string.charAt(0))-97)]==0)
  189. {
  190. matrixIndices[h]=DeceleLocation[(char)(string.charAt(0))-97];
  191. }
  192. else if(ctxt.get(h).equals(table[i][j]+""))
  193. {
  194. matrixIndices[h]=i*6+j;
  195. chkRepeat[table[i][j]-97]=0;
  196. DeceleLocation[table[i][j]-97]=i*6+j;
  197. }
  198. }
  199. }
  200. }
  201. for(int i=0;i<ctxt.size();i++)
  202. {
  203. if(i%2==0)
  204. {
  205. string=(String) ctxt.get(i);
  206. string1=(String) ctxt.get(i+1);
  207. rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
  208. colCurrent=DeceleLocation[(char)(string.charAt(0))-97]%6;
  209. row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
  210. col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
  211. if(rowCurrent==row)
  212. {
  213. Newptxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
  214. Newptxt.add(i+1,""+table[row][(col+1)%6]);
  215. i++;
  216. System.out.println("Plaintext is: "+Newptxt);
  217. }
  218. else if(colCurrent==col)
  219. {
  220. Newptxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
  221. Newptxt.add(i+1,""+table[(row+1)%6][col]);
  222. i++;
  223. System.out.println("Plaintext is: "+Newptxt);
  224. }
  225. else
  226. {
  227. Newptxt.add(i,""+table[rowCurrent][col]);
  228. System.out.println("Plaintext is: "+Newptxt);
  229. }
  230. }
  231. else if(i%2==1)
  232. {
  233. string=(String) ctxt.get(i);
  234. string1=(String) ctxt.get(i-1);
  235. rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
  236. colCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
  237. row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
  238. col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
  239. Newptxt.add(i,""+table[rowCurrent][col]);
  240. System.out.println("Plaintext is: "+Newptxt);
  241. }
  242. }
  243. System.out.println("Plaintext is: "+Newptxt);
  244. } catch (Exception e) {
  245. // TODO Auto-generated catch block
  246. System.out.println("Exception:"+e.getCause()+" ------ "+e.getLocalizedMessage()+" ------ "+e.getMessage());
  247. }
  248. }
  249. }