HillCipher.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Name: Ajinkya Arun Gurav
  3. Div : H Roll No:21
  4. GR No : 121237
  5. Subject : Network Security Lab
  6. Lab 2: Implement encryption and decryption using Hill Cipher.
  7. */
  8. import java.io.*;
  9. import java.util.Scanner;
  10. public class HillCipher {
  11. public static void main(String args[])
  12. {
  13. String PlainText=null,CipherText=null;
  14. int key[][]=new int[2][2];
  15. BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  16. Scanner in = new Scanner(System.in);
  17. System.out.println("Enter the PlainText");
  18. try {
  19. PlainText=br.readLine();
  20. System.out.println("Enter the key matrix; 4 elements");
  21. for(int i=0;i<2;i++)
  22. {
  23. for(int j=0;j<2;j++)
  24. {
  25. key[i][j]=in.nextInt();
  26. }
  27. }
  28. CipherText=encrypt(PlainText,key);
  29. System.out.println("Ciphertext is : "+CipherText);
  30. PlainText=decrypt(CipherText,key);
  31. System.out.println("PlainText is : "+PlainText);
  32. } catch (Exception e) {
  33. // TODO Auto-generated catch block
  34. System.out.println("Exception is: "+e.getCause());
  35. }
  36. }
  37. static String encrypt(String ptext, int key[][] )
  38. {
  39. int p[][]=new int[1][2];
  40. int result[][]=new int[1][2];
  41. String temp="";
  42. //HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
  43. /*for(int i=0;i<ptext.length();i++)
  44. hm.put(ptext.charAt(i),i); */
  45. try{
  46. p=make1Dmat(ptext);
  47. }
  48. catch(Exception e)
  49. {
  50. System.out.println("Exception is: "+e.getLocalizedMessage()+"-----"+e.getMessage());
  51. }
  52. result=MulMatrix(key,p);
  53. for(int j=0;j<result[0].length;j++)
  54. {
  55. result[0][j]=(result[0][j]%26);
  56. temp=temp+((char)(result[0][j]+97));
  57. }
  58. /*for(Map.Entry m:hm.entrySet()){
  59. System.out.println(m.getKey()+" "+m.getValue());
  60. } */
  61. return temp;
  62. }
  63. static int[][] make1Dmat(String txt)
  64. {
  65. int [][] p=new int[1][txt.length()];
  66. for(int i=0;i<txt.length();i++)
  67. {
  68. p[0][i]=txt.charAt(i)-97;
  69. }
  70. return p;
  71. }
  72. static String decrypt(String ctext, int key[][] )
  73. {
  74. //System.out.println((-43)%26);
  75. int result[][]=new int[1][2];
  76. int InvKey[][]=new int[2][2];
  77. String temp="";
  78. InvKey=FindInverse(key);
  79. result=make1Dmat(ctext);
  80. result=MulMatrix(InvKey, result);
  81. for(int j=0;j<result[0].length;j++)
  82. {
  83. result[0][j]= (result[0][j]%26)< 0 ? 26 + (result[0][j]%26) : (result[0][j]%26) % 26;
  84. temp=temp+((char)(result[0][j]+97));
  85. }
  86. return temp;
  87. }
  88. static int[][] FindInverse(int[][] key)
  89. {
  90. int IKey[][]=new int[2][2];
  91. int temp=0;
  92. int det=0;
  93. int i=0;
  94. det=determinant(key);
  95. System.out.println("Determinant is : "+det);
  96. for(i=0;i<26;i++)
  97. {
  98. if((det*i)%26==1)
  99. {
  100. break;
  101. }
  102. }
  103. det=i-26;
  104. for(int y=0;y<2;y++)
  105. {
  106. for(int z=0;z<2;z++)
  107. {
  108. if(y==0 && z==0)
  109. {
  110. temp=0;
  111. IKey[y][z]=key[y][z];
  112. IKey[y+1][z+1]=key[y+1][z+1];
  113. temp=IKey[y][z];
  114. IKey[y][z]=IKey[y+1][z+1];
  115. IKey[y+1][z+1]=temp;
  116. }
  117. else if(y==0 && z==1)
  118. {
  119. IKey[y][z]=-key[y][z];
  120. IKey[y+1][z-1]=-key[y+1][z-1];
  121. }
  122. IKey[y][z]=(det*IKey[y][z])%26;
  123. }
  124. }
  125. return IKey;
  126. }
  127. static int determinant(int[][] matrix){ //method sig. takes a matrix (two dimensional array), returns determinant.
  128. {
  129. int sum=0;
  130. int s;
  131. if(matrix.length==1){ //bottom case of recursion. size 1 matrix determinant is itself.
  132. return(matrix[0][0]);
  133. }
  134. for(int i=0;i<matrix.length;i++){ //finds determinant using row-by-row expansion
  135. int[][]smaller= new int[matrix.length-1][matrix.length-1]; //creates smaller matrix- values not in same row, column
  136. for(int a=1;a<matrix.length;a++){
  137. for(int b=0;b<matrix.length;b++){
  138. if(b<i){
  139. smaller[a-1][b]=matrix[a][b];
  140. }
  141. else if(b>i){
  142. smaller[a-1][b-1]=matrix[a][b];
  143. }
  144. }
  145. }
  146. if(i%2==0){ //sign changes based on i
  147. s=1;
  148. }
  149. else{
  150. s=-1;
  151. }
  152. sum+=s*matrix[0][i]*(determinant(smaller)); //recursive step: determinant of larger determined by smaller.
  153. }
  154. return(sum);
  155. }//returns determinant value. once stack is finished, returns final determinant.
  156. }
  157. static int[][] MulMatrix(int key[][],int p[][])
  158. {
  159. //int result[][]=new int[1][2];
  160. int rowsInP = p.length;
  161. int columnsInP = p[0].length; // same as rows in key
  162. int columnsInKey = key[0].length;
  163. int[][] result = new int[rowsInP][columnsInKey];
  164. for (int i = 0; i < rowsInP; i++) {
  165. for (int j = 0; j < columnsInKey; j++) {
  166. for (int k = 0; k < columnsInP; k++) {
  167. result[i][j] = result[i][j] + p[i][k] * key[k][j];
  168. }
  169. }
  170. }
  171. return result;
  172. }
  173. }
  174. /*
  175. *OUTPUT:
  176. Enter the PlainText
  177. me
  178. Enter the key matrix; 4 elements
  179. 9
  180. 4
  181. 5
  182. 7
  183. Ciphertext is : yy
  184. Determinant is : 43
  185. PlainText is : me
  186. */