|
| 1 | +package Ciphers; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/* |
| 6 | +* Java Implementation of Hill Cipher |
| 7 | +* Hill cipher is a polyalphabetic substitution cipher. Each letter is represented by a number belonging to the set Z26 where A=0 , B=1, ..... Z=25. |
| 8 | +* To encrypt a message, each block of n letters (since matrix size is n x n) is multiplied by an invertible n × n matrix, against modulus 26. |
| 9 | +* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption. |
| 10 | +* The cipher key and plaintext/ciphertext are user inputs. |
| 11 | +* @author Ojasva Jain |
| 12 | +*/ |
| 13 | + |
| 14 | +public class HillCipher{ |
| 15 | +static Scanner in = new Scanner (System.in); |
| 16 | + |
| 17 | +/* Following function encrypts the message |
| 18 | +*/ |
| 19 | +static void encrypt(String message) |
| 20 | +{ |
| 21 | + message = message.toUpperCase(); |
| 22 | + // Get key matrix |
| 23 | + System.out.println("Enter key matrix size"); |
| 24 | + int n = in.nextInt(); |
| 25 | + System.out.println("Enter Key/encryptionKey matrix "); |
| 26 | + int keyMatrix[][] = new int [n][n]; |
| 27 | + for(int i=0;i<n;i++){ |
| 28 | + for(int j=0;j<n;j++){ |
| 29 | + keyMatrix[i][j] = in.nextInt(); |
| 30 | + } |
| 31 | + } |
| 32 | + //check if det = 0 |
| 33 | + if(determinant(keyMatrix,n)%26 == 0) |
| 34 | + { |
| 35 | + System.out.println("Invalid key, as determinant = 0. Program Terminated"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + int [][]messageVector = new int[n][1]; |
| 40 | + String CipherText=""; |
| 41 | + int cipherMatrix [][] = new int [n][1]; |
| 42 | + int j = 0; |
| 43 | + while(j<message.length()){ |
| 44 | + for (int i = 0; i < n; i++){ |
| 45 | + if(j>=message.length()){ messageVector[i][0] = 23;} |
| 46 | + else |
| 47 | + messageVector[i][0] = (message.charAt(j))%65; |
| 48 | + System.out.println(messageVector[i][0]); |
| 49 | + j++; |
| 50 | + } |
| 51 | + int x, i; |
| 52 | + for (i = 0; i < n; i++) |
| 53 | + { |
| 54 | + cipherMatrix[i][0] = 0; |
| 55 | + |
| 56 | + for (x = 0; x < n; x++) |
| 57 | + { |
| 58 | + cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; |
| 59 | + } |
| 60 | + System.out.println(cipherMatrix[i][0]); |
| 61 | + cipherMatrix[i][0] = cipherMatrix[i][0] % 26; |
| 62 | + } |
| 63 | + for (i = 0; i < n; i++) |
| 64 | + CipherText += (char)(cipherMatrix[i][0] + 65); |
| 65 | +} |
| 66 | +System.out.println("Ciphertext: "+ CipherText); |
| 67 | +} |
| 68 | +//Following function decrypts a message |
| 69 | +static void decrypt(String message) |
| 70 | +{ |
| 71 | + message = message.toUpperCase(); |
| 72 | + // Get key matrix |
| 73 | + System.out.println("Enter key matrix size"); |
| 74 | + int n = in.nextInt(); |
| 75 | + System.out.println("Enter inverseKey/decryptionKey matrix "); |
| 76 | + int keyMatrix[][] = new int [n][n]; |
| 77 | + for(int i=0;i<n;i++){ |
| 78 | + for(int j=0;j<n;j++){ |
| 79 | + keyMatrix[i][j] = in.nextInt(); |
| 80 | + } |
| 81 | + } |
| 82 | + //check if det = 0 |
| 83 | + if(determinant(keyMatrix,n)%26 == 0) |
| 84 | + { |
| 85 | + System.out.println("Invalid key, as determinant = 0. Program Terminated"); |
| 86 | + return; |
| 87 | + } |
| 88 | + //solving for the required plaintext message |
| 89 | + int [][]messageVector = new int[n][1]; |
| 90 | + String PlainText=""; |
| 91 | + int plainMatrix [][] = new int [n][1]; |
| 92 | + int j = 0; |
| 93 | + while(j<message.length()){ |
| 94 | + for (int i = 0; i < n; i++){ |
| 95 | + if(j>=message.length()){ messageVector[i][0] = 23;} |
| 96 | + else |
| 97 | + messageVector[i][0] = (message.charAt(j))%65; |
| 98 | + System.out.println(messageVector[i][0]); |
| 99 | + j++; |
| 100 | + } |
| 101 | + int x, i; |
| 102 | + for (i = 0; i < n; i++) |
| 103 | + { |
| 104 | + plainMatrix[i][0] = 0; |
| 105 | + |
| 106 | + for (x = 0; x < n; x++) |
| 107 | + { |
| 108 | + plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; |
| 109 | + } |
| 110 | + |
| 111 | + plainMatrix[i][0] = plainMatrix[i][0] % 26; |
| 112 | + } |
| 113 | + for (i = 0; i < n; i++) |
| 114 | + PlainText += (char)(plainMatrix[i][0] + 65); |
| 115 | + } |
| 116 | + System.out.println("Plaintext: "+PlainText); |
| 117 | +} |
| 118 | + |
| 119 | +// Determinant calculator |
| 120 | +public static int determinant(int a[][], int n){ |
| 121 | + int det = 0, sign = 1, p = 0, q = 0; |
| 122 | + |
| 123 | + if(n==1){ |
| 124 | + det = a[0][0]; |
| 125 | + } |
| 126 | + else{ |
| 127 | + int b[][] = new int[n-1][n-1]; |
| 128 | + for(int x = 0 ; x < n ; x++){ |
| 129 | + p=0;q=0; |
| 130 | + for(int i = 1;i < n; i++){ |
| 131 | + for(int j = 0; j < n;j++){ |
| 132 | + if(j != x){ |
| 133 | + b[p][q++] = a[i][j]; |
| 134 | + if(q % (n-1) == 0){ |
| 135 | + p++; |
| 136 | + q=0; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + det = det + a[0][x] *determinant(b, n-1) * sign; |
| 142 | + sign = -sign; |
| 143 | + } |
| 144 | + } |
| 145 | + return det; |
| 146 | +} |
| 147 | + |
| 148 | +// Function to implement Hill Cipher |
| 149 | +static void hillcipher(String message) |
| 150 | +{ |
| 151 | + message.toUpperCase(); |
| 152 | + System.out.println("What do you want to process from the message?"); |
| 153 | + System.out.println("Press 1: To Encrypt"); |
| 154 | + System.out.println("Press 2: To Decrypt"); |
| 155 | + short sc = in.nextShort(); |
| 156 | + if(sc == 1) |
| 157 | + encrypt(message); |
| 158 | + else if(sc == 2) |
| 159 | + decrypt(message); |
| 160 | + else |
| 161 | + System.out.println("Invalid input, program terminated."); |
| 162 | +} |
| 163 | + |
| 164 | +// Driver code |
| 165 | +public static void main(String[] args) |
| 166 | + { |
| 167 | + // Get the message to be encrypted |
| 168 | + System.out.println("Enter message"); |
| 169 | + String message = in.nextLine(); |
| 170 | + hillcipher(message); |
| 171 | + } |
| 172 | +} |
0 commit comments