From 3538a6642dbfe2aebc90e721915bc86ea343f3e6 Mon Sep 17 00:00:00 2001 From: Ojasva Jain <44553464+ojasva@users.noreply.github.com> Date: Mon, 4 Oct 2021 02:04:18 +0530 Subject: [PATCH 1/8] Hill Cipher Implementation in JAVA --- Ciphers/HillCipher.java | 172 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Ciphers/HillCipher.java diff --git a/Ciphers/HillCipher.java b/Ciphers/HillCipher.java new file mode 100644 index 000000000000..0a3b50308d21 --- /dev/null +++ b/Ciphers/HillCipher.java @@ -0,0 +1,172 @@ +package Ciphers; + +import java.util.*; + +/* +* Java Implementation of Hill Cipher +* 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. +* 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. +* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption. +* The cipher key and plaintext/ciphertext are user inputs. +* @author Ojasva Jain +*/ + +public class HillCipher{ +static Scanner in = new Scanner (System.in); + +/* Following function encrypts the message +*/ +static void encrypt(String message) +{ + message = message.toUpperCase(); + // Get key matrix + System.out.println("Enter key matrix size"); + int n = in.nextInt(); + System.out.println("Enter Key/encryptionKey matrix "); + int keyMatrix[][] = new int [n][n]; + for(int i=0;i=message.length()){ messageVector[i][0] = 23;} + else + messageVector[i][0] = (message.charAt(j))%65; + System.out.println(messageVector[i][0]); + j++; + } + int x, i; + for (i = 0; i < n; i++) + { + cipherMatrix[i][0] = 0; + + for (x = 0; x < n; x++) + { + cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; + } + System.out.println(cipherMatrix[i][0]); + cipherMatrix[i][0] = cipherMatrix[i][0] % 26; + } + for (i = 0; i < n; i++) + CipherText += (char)(cipherMatrix[i][0] + 65); +} +System.out.println("Ciphertext: "+ CipherText); +} +//Following function decrypts a message +static void decrypt(String message) +{ + message = message.toUpperCase(); + // Get key matrix + System.out.println("Enter key matrix size"); + int n = in.nextInt(); + System.out.println("Enter inverseKey/decryptionKey matrix "); + int keyMatrix[][] = new int [n][n]; + for(int i=0;i=message.length()){ messageVector[i][0] = 23;} + else + messageVector[i][0] = (message.charAt(j))%65; + System.out.println(messageVector[i][0]); + j++; + } + int x, i; + for (i = 0; i < n; i++) + { + plainMatrix[i][0] = 0; + + for (x = 0; x < n; x++) + { + plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; + } + + plainMatrix[i][0] = plainMatrix[i][0] % 26; + } + for (i = 0; i < n; i++) + PlainText += (char)(plainMatrix[i][0] + 65); + } + System.out.println("Plaintext: "+PlainText); +} + +// Determinant calculator +public static int determinant(int a[][], int n){ + int det = 0, sign = 1, p = 0, q = 0; + + if(n==1){ + det = a[0][0]; + } + else{ + int b[][] = new int[n-1][n-1]; + for(int x = 0 ; x < n ; x++){ + p=0;q=0; + for(int i = 1;i < n; i++){ + for(int j = 0; j < n;j++){ + if(j != x){ + b[p][q++] = a[i][j]; + if(q % (n-1) == 0){ + p++; + q=0; + } + } + } + } + det = det + a[0][x] *determinant(b, n-1) * sign; + sign = -sign; + } + } + return det; +} + +// Function to implement Hill Cipher +static void hillcipher(String message) +{ + message.toUpperCase(); + System.out.println("What do you want to process from the message?"); + System.out.println("Press 1: To Encrypt"); + System.out.println("Press 2: To Decrypt"); + short sc = in.nextShort(); + if(sc == 1) + encrypt(message); + else if(sc == 2) + decrypt(message); + else + System.out.println("Invalid input, program terminated."); +} + +// Driver code +public static void main(String[] args) + { + // Get the message to be encrypted + System.out.println("Enter message"); + String message = in.nextLine(); + hillcipher(message); + } +} From 4e6878ebc66253d797eed4b900d8a992b7f407b0 Mon Sep 17 00:00:00 2001 From: Ojasva Jain <44553464+ojasva@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:18:42 +0530 Subject: [PATCH 2/8] Determinant of a Matrix --- Maths/DeterminantOfMatrix.java | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Maths/DeterminantOfMatrix.java diff --git a/Maths/DeterminantOfMatrix.java b/Maths/DeterminantOfMatrix.java new file mode 100644 index 000000000000..f5ed09ca167d --- /dev/null +++ b/Maths/DeterminantOfMatrix.java @@ -0,0 +1,61 @@ +package Maths; +import java.util.*; + +/* +* @author Ojasva Jain +* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant +*/ +public class DeterminantOfMatrix +{ + // Determinant calculator + //@return determinant of the input matrix + static int determinant(int a[][], int n) + { + int det = 0, sign = 1, p = 0, q = 0; + if(n==1) + { + det = a[0][0]; + } + else + { + int b[][] = new int[n-1][n-1]; + for(int x = 0 ; x < n ; x++) + { + p=0;q=0; + for(int i = 1;i < n; i++) + { + for(int j = 0; j < n;j++) + { + if(j != x) + { + b[p][q++] = a[i][j]; + if(q % (n-1) == 0) + { + p++; + q=0; + } + } + } + } + det = det + a[0][x] *determinant(b, n-1) * sign; + sign = -sign; + } + } + return det; + } + //Driver Method + public static void main(String [] args){ + Scanner in = new Scanner(System.in); + //Input Matrix + System.out.println("Enter matrix size (Square matrix only)"); + int n = in.nextInt(); + System.out.println("Enter matrix"); + int a [][] = new int [n][n]; + for(int i=0;i Date: Tue, 5 Oct 2021 01:20:18 +0530 Subject: [PATCH 3/8] Delete HillCipher.java --- Ciphers/HillCipher.java | 172 ---------------------------------------- 1 file changed, 172 deletions(-) delete mode 100644 Ciphers/HillCipher.java diff --git a/Ciphers/HillCipher.java b/Ciphers/HillCipher.java deleted file mode 100644 index 0a3b50308d21..000000000000 --- a/Ciphers/HillCipher.java +++ /dev/null @@ -1,172 +0,0 @@ -package Ciphers; - -import java.util.*; - -/* -* Java Implementation of Hill Cipher -* 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. -* 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. -* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption. -* The cipher key and plaintext/ciphertext are user inputs. -* @author Ojasva Jain -*/ - -public class HillCipher{ -static Scanner in = new Scanner (System.in); - -/* Following function encrypts the message -*/ -static void encrypt(String message) -{ - message = message.toUpperCase(); - // Get key matrix - System.out.println("Enter key matrix size"); - int n = in.nextInt(); - System.out.println("Enter Key/encryptionKey matrix "); - int keyMatrix[][] = new int [n][n]; - for(int i=0;i=message.length()){ messageVector[i][0] = 23;} - else - messageVector[i][0] = (message.charAt(j))%65; - System.out.println(messageVector[i][0]); - j++; - } - int x, i; - for (i = 0; i < n; i++) - { - cipherMatrix[i][0] = 0; - - for (x = 0; x < n; x++) - { - cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; - } - System.out.println(cipherMatrix[i][0]); - cipherMatrix[i][0] = cipherMatrix[i][0] % 26; - } - for (i = 0; i < n; i++) - CipherText += (char)(cipherMatrix[i][0] + 65); -} -System.out.println("Ciphertext: "+ CipherText); -} -//Following function decrypts a message -static void decrypt(String message) -{ - message = message.toUpperCase(); - // Get key matrix - System.out.println("Enter key matrix size"); - int n = in.nextInt(); - System.out.println("Enter inverseKey/decryptionKey matrix "); - int keyMatrix[][] = new int [n][n]; - for(int i=0;i=message.length()){ messageVector[i][0] = 23;} - else - messageVector[i][0] = (message.charAt(j))%65; - System.out.println(messageVector[i][0]); - j++; - } - int x, i; - for (i = 0; i < n; i++) - { - plainMatrix[i][0] = 0; - - for (x = 0; x < n; x++) - { - plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; - } - - plainMatrix[i][0] = plainMatrix[i][0] % 26; - } - for (i = 0; i < n; i++) - PlainText += (char)(plainMatrix[i][0] + 65); - } - System.out.println("Plaintext: "+PlainText); -} - -// Determinant calculator -public static int determinant(int a[][], int n){ - int det = 0, sign = 1, p = 0, q = 0; - - if(n==1){ - det = a[0][0]; - } - else{ - int b[][] = new int[n-1][n-1]; - for(int x = 0 ; x < n ; x++){ - p=0;q=0; - for(int i = 1;i < n; i++){ - for(int j = 0; j < n;j++){ - if(j != x){ - b[p][q++] = a[i][j]; - if(q % (n-1) == 0){ - p++; - q=0; - } - } - } - } - det = det + a[0][x] *determinant(b, n-1) * sign; - sign = -sign; - } - } - return det; -} - -// Function to implement Hill Cipher -static void hillcipher(String message) -{ - message.toUpperCase(); - System.out.println("What do you want to process from the message?"); - System.out.println("Press 1: To Encrypt"); - System.out.println("Press 2: To Decrypt"); - short sc = in.nextShort(); - if(sc == 1) - encrypt(message); - else if(sc == 2) - decrypt(message); - else - System.out.println("Invalid input, program terminated."); -} - -// Driver code -public static void main(String[] args) - { - // Get the message to be encrypted - System.out.println("Enter message"); - String message = in.nextLine(); - hillcipher(message); - } -} From 457036712be2f8b4fe3767ef260e0ee68264bb6d Mon Sep 17 00:00:00 2001 From: Ojasva Jain <44553464+ojasva@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:22:36 +0530 Subject: [PATCH 4/8] Add files via upload --- Ciphers/HillCipher.java | 172 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Ciphers/HillCipher.java diff --git a/Ciphers/HillCipher.java b/Ciphers/HillCipher.java new file mode 100644 index 000000000000..0a3b50308d21 --- /dev/null +++ b/Ciphers/HillCipher.java @@ -0,0 +1,172 @@ +package Ciphers; + +import java.util.*; + +/* +* Java Implementation of Hill Cipher +* 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. +* 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. +* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption. +* The cipher key and plaintext/ciphertext are user inputs. +* @author Ojasva Jain +*/ + +public class HillCipher{ +static Scanner in = new Scanner (System.in); + +/* Following function encrypts the message +*/ +static void encrypt(String message) +{ + message = message.toUpperCase(); + // Get key matrix + System.out.println("Enter key matrix size"); + int n = in.nextInt(); + System.out.println("Enter Key/encryptionKey matrix "); + int keyMatrix[][] = new int [n][n]; + for(int i=0;i=message.length()){ messageVector[i][0] = 23;} + else + messageVector[i][0] = (message.charAt(j))%65; + System.out.println(messageVector[i][0]); + j++; + } + int x, i; + for (i = 0; i < n; i++) + { + cipherMatrix[i][0] = 0; + + for (x = 0; x < n; x++) + { + cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; + } + System.out.println(cipherMatrix[i][0]); + cipherMatrix[i][0] = cipherMatrix[i][0] % 26; + } + for (i = 0; i < n; i++) + CipherText += (char)(cipherMatrix[i][0] + 65); +} +System.out.println("Ciphertext: "+ CipherText); +} +//Following function decrypts a message +static void decrypt(String message) +{ + message = message.toUpperCase(); + // Get key matrix + System.out.println("Enter key matrix size"); + int n = in.nextInt(); + System.out.println("Enter inverseKey/decryptionKey matrix "); + int keyMatrix[][] = new int [n][n]; + for(int i=0;i=message.length()){ messageVector[i][0] = 23;} + else + messageVector[i][0] = (message.charAt(j))%65; + System.out.println(messageVector[i][0]); + j++; + } + int x, i; + for (i = 0; i < n; i++) + { + plainMatrix[i][0] = 0; + + for (x = 0; x < n; x++) + { + plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0]; + } + + plainMatrix[i][0] = plainMatrix[i][0] % 26; + } + for (i = 0; i < n; i++) + PlainText += (char)(plainMatrix[i][0] + 65); + } + System.out.println("Plaintext: "+PlainText); +} + +// Determinant calculator +public static int determinant(int a[][], int n){ + int det = 0, sign = 1, p = 0, q = 0; + + if(n==1){ + det = a[0][0]; + } + else{ + int b[][] = new int[n-1][n-1]; + for(int x = 0 ; x < n ; x++){ + p=0;q=0; + for(int i = 1;i < n; i++){ + for(int j = 0; j < n;j++){ + if(j != x){ + b[p][q++] = a[i][j]; + if(q % (n-1) == 0){ + p++; + q=0; + } + } + } + } + det = det + a[0][x] *determinant(b, n-1) * sign; + sign = -sign; + } + } + return det; +} + +// Function to implement Hill Cipher +static void hillcipher(String message) +{ + message.toUpperCase(); + System.out.println("What do you want to process from the message?"); + System.out.println("Press 1: To Encrypt"); + System.out.println("Press 2: To Decrypt"); + short sc = in.nextShort(); + if(sc == 1) + encrypt(message); + else if(sc == 2) + decrypt(message); + else + System.out.println("Invalid input, program terminated."); +} + +// Driver code +public static void main(String[] args) + { + // Get the message to be encrypted + System.out.println("Enter message"); + String message = in.nextLine(); + hillcipher(message); + } +} From 2821b9f610d0ff191bab16d831ed2a5e023ba855 Mon Sep 17 00:00:00 2001 From: Ojasva Jain <44553464+ojasva@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:31:17 +0530 Subject: [PATCH 5/8] Delete DeterminantOfMatrix.java --- Maths/DeterminantOfMatrix.java | 61 ---------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 Maths/DeterminantOfMatrix.java diff --git a/Maths/DeterminantOfMatrix.java b/Maths/DeterminantOfMatrix.java deleted file mode 100644 index f5ed09ca167d..000000000000 --- a/Maths/DeterminantOfMatrix.java +++ /dev/null @@ -1,61 +0,0 @@ -package Maths; -import java.util.*; - -/* -* @author Ojasva Jain -* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant -*/ -public class DeterminantOfMatrix -{ - // Determinant calculator - //@return determinant of the input matrix - static int determinant(int a[][], int n) - { - int det = 0, sign = 1, p = 0, q = 0; - if(n==1) - { - det = a[0][0]; - } - else - { - int b[][] = new int[n-1][n-1]; - for(int x = 0 ; x < n ; x++) - { - p=0;q=0; - for(int i = 1;i < n; i++) - { - for(int j = 0; j < n;j++) - { - if(j != x) - { - b[p][q++] = a[i][j]; - if(q % (n-1) == 0) - { - p++; - q=0; - } - } - } - } - det = det + a[0][x] *determinant(b, n-1) * sign; - sign = -sign; - } - } - return det; - } - //Driver Method - public static void main(String [] args){ - Scanner in = new Scanner(System.in); - //Input Matrix - System.out.println("Enter matrix size (Square matrix only)"); - int n = in.nextInt(); - System.out.println("Enter matrix"); - int a [][] = new int [n][n]; - for(int i=0;i Date: Wed, 6 Oct 2021 15:23:14 +0530 Subject: [PATCH 6/8] DeterminantOfMatrix Finding determinant of a matrix. --- Maths/DeterminantOfMatrix.java | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Maths/DeterminantOfMatrix.java diff --git a/Maths/DeterminantOfMatrix.java b/Maths/DeterminantOfMatrix.java new file mode 100644 index 000000000000..f5ed09ca167d --- /dev/null +++ b/Maths/DeterminantOfMatrix.java @@ -0,0 +1,61 @@ +package Maths; +import java.util.*; + +/* +* @author Ojasva Jain +* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant +*/ +public class DeterminantOfMatrix +{ + // Determinant calculator + //@return determinant of the input matrix + static int determinant(int a[][], int n) + { + int det = 0, sign = 1, p = 0, q = 0; + if(n==1) + { + det = a[0][0]; + } + else + { + int b[][] = new int[n-1][n-1]; + for(int x = 0 ; x < n ; x++) + { + p=0;q=0; + for(int i = 1;i < n; i++) + { + for(int j = 0; j < n;j++) + { + if(j != x) + { + b[p][q++] = a[i][j]; + if(q % (n-1) == 0) + { + p++; + q=0; + } + } + } + } + det = det + a[0][x] *determinant(b, n-1) * sign; + sign = -sign; + } + } + return det; + } + //Driver Method + public static void main(String [] args){ + Scanner in = new Scanner(System.in); + //Input Matrix + System.out.println("Enter matrix size (Square matrix only)"); + int n = in.nextInt(); + System.out.println("Enter matrix"); + int a [][] = new int [n][n]; + for(int i=0;i Date: Tue, 12 Oct 2021 17:03:09 +0530 Subject: [PATCH 7/8] Inverse Of a Square Matrix This feature finds inverse of a matrix using gaussian elimination. --- Misc/InverseOfMatrix.java | 130 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Misc/InverseOfMatrix.java diff --git a/Misc/InverseOfMatrix.java b/Misc/InverseOfMatrix.java new file mode 100644 index 000000000000..f229e8c86088 --- /dev/null +++ b/Misc/InverseOfMatrix.java @@ -0,0 +1,130 @@ +import java.util.Scanner; + +/* +* Wikipedia link : https://en.wikipedia.org/wiki/Invertible_matrix +* +* Here we use gauss elimination method to find the inverse of a given matrix. +* To understand gauss elimination method to find inverse of a matrix: https://www.sangakoo.com/en/unit/inverse-matrix-method-of-gaussian-elimination +* +* We can also find the inverse of a matrix +*/ +public class InverseOfMatrix +{ + public static void main(String argv[]) + { + Scanner input = new Scanner(System.in); + System.out.println("Enter the matrix size (Square matrix only): "); + int n = input.nextInt(); + double a[][]= new double[n][n]; + System.out.println("Enter the elements of matrix: "); + for(int i=0; i=0; --j) + { + x[j][i] = b[index[j]][i]; + for (int k=j+1; k c1) c1 = c0; + } + c[i] = c1; + } + + // Search the pivoting element from each column + int k = 0; + for (int j=0; j pi1) + { + pi1 = pi0; + k = i; + } + } + // Interchange rows according to the pivoting order + int itmp = index[j]; + index[j] = index[k]; + index[k] = itmp; + for (int i=j+1; i Date: Sat, 16 Oct 2021 18:50:31 +0530 Subject: [PATCH 8/8] Update InverseOfMatrix.java --- Misc/InverseOfMatrix.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/InverseOfMatrix.java b/Misc/InverseOfMatrix.java index f229e8c86088..ed11d77ed205 100644 --- a/Misc/InverseOfMatrix.java +++ b/Misc/InverseOfMatrix.java @@ -1,3 +1,4 @@ +package Misc; import java.util.Scanner; /* @@ -127,4 +128,4 @@ public static void gaussian(double a[][], int index[]) } } } -} \ No newline at end of file +}