From 835783dca068c013aa3aeb2b767bf964d4e06606 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:10:31 +0530 Subject: [PATCH 01/67] Create DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/DiffieHellman.java diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java new file mode 100644 index 000000000000..e4cc3281454a --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -0,0 +1,33 @@ +import java.math.BigInteger; +import java.util.*; + +public class DiffieHellman { + public static void main(String[] args) { + Scanner read = new Scanner(System.in); + System.out.println("Hello User! \nEnter your name:"); + String name = read.nextLine(); + read.nextLine(); + System.out.println("Welcome "+name+"!"); + + BigInteger n,g,x,y,k1,k2,A,B; + + System.out.println("Enter two prime numbers: "); + n = new BigInteger(read.next()); + g = new BigInteger(read.next()); + + System.out.println("Person A : Enter your secret number"); + x = new BigInteger(read.next()); + A = g.modPow(x, n); + + System.out.println("Person B : Enter your secret number"); + y = new BigInteger(read.next()); + B = g.modPow(y, n); + + k1 = B.modPow(x, n); + k2 = A.modPow(y, n); + + System.out.println("A's secret key: "+k1); + System.out.println("B's secret key: "+k2); + + } +} From 8e1d3cf53472d6a56ea57e896b5b431360bf663e Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:19:17 +0530 Subject: [PATCH 02/67] Update DiffieHellman.java --- .../java/com/thealgorithms/ciphers/DiffieHellman.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index e4cc3281454a..4d48618803b4 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -6,10 +6,10 @@ public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("Hello User! \nEnter your name:"); String name = read.nextLine(); - read.nextLine(); - System.out.println("Welcome "+name+"!"); + read.nextLine(); ++ System.out.println("Welcome " + name + "!"); - BigInteger n,g,x,y,k1,k2,A,B; + BigInteger n, g, x, y, k1, k2, A, B; System.out.println("Enter two prime numbers: "); n = new BigInteger(read.next()); @@ -26,8 +26,8 @@ public static void main(String[] args) { k1 = B.modPow(x, n); k2 = A.modPow(y, n); - System.out.println("A's secret key: "+k1); - System.out.println("B's secret key: "+k2); + System.out.println("A's secret key: " + k1); + System.out.println("B's secret key: " + k2); } } From ee197e761a82120bb7814cfb867046196bb73e9c Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:22:12 +0530 Subject: [PATCH 03/67] Update DiffieHellman.java --- src/main/java/com/thealgorithms/ciphers/DiffieHellman.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 4d48618803b4..6e8e4f7ba627 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -7,7 +7,7 @@ public static void main(String[] args) { System.out.println("Hello User! \nEnter your name:"); String name = read.nextLine(); read.nextLine(); -+ System.out.println("Welcome " + name + "!"); + System.out.println("Welcome " + name + "!"); BigInteger n, g, x, y, k1, k2, A, B; From 998592dc29e49b1e73acfe11c99d27c502c65997 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:23:17 +0530 Subject: [PATCH 04/67] Update DiffieHellman.java --- src/main/java/com/thealgorithms/ciphers/DiffieHellman.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 6e8e4f7ba627..2ad295a39a37 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -28,6 +28,5 @@ public static void main(String[] args) { System.out.println("A's secret key: " + k1); System.out.println("B's secret key: " + k2); - } } From b9b5363974d859d248073a7bba3fb0fa483ff975 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:29:10 +0530 Subject: [PATCH 05/67] Update DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 2ad295a39a37..76c93896cefc 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -1,7 +1,10 @@ import java.math.BigInteger; -import java.util.*; +import java.util.Scanner; public class DiffieHellman { + private DiffieHellman() { + throw new UnsupportedOperationException("Utility class"); + } public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("Hello User! \nEnter your name:"); @@ -9,7 +12,14 @@ public static void main(String[] args) { read.nextLine(); System.out.println("Welcome " + name + "!"); - BigInteger n, g, x, y, k1, k2, A, B; + BigInteger n; + BigInteger g; + BigInteger x; + BigInteger y; + BigInteger k1; + BigInteger k2; + BigInteger a; + BigInteger b; System.out.println("Enter two prime numbers: "); n = new BigInteger(read.next()); @@ -17,11 +27,11 @@ public static void main(String[] args) { System.out.println("Person A : Enter your secret number"); x = new BigInteger(read.next()); - A = g.modPow(x, n); + a = g.modPow(x, n); System.out.println("Person B : Enter your secret number"); y = new BigInteger(read.next()); - B = g.modPow(y, n); + b = g.modPow(y, n); k1 = B.modPow(x, n); k2 = A.modPow(y, n); From b18cc321f9c4f838b1ceed8dcac1a92168b25e30 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:30:36 +0530 Subject: [PATCH 06/67] Update DiffieHellman.java --- src/main/java/com/thealgorithms/ciphers/DiffieHellman.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 76c93896cefc..25fedaf8ee83 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -33,8 +33,8 @@ public static void main(String[] args) { y = new BigInteger(read.next()); b = g.modPow(y, n); - k1 = B.modPow(x, n); - k2 = A.modPow(y, n); + k1 = b.modPow(x, n); + k2 = a.modPow(y, n); System.out.println("A's secret key: " + k1); System.out.println("B's secret key: " + k2); From 6f5b5fc32c436df4cf03a8e890432667e7cf1644 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:34:48 +0530 Subject: [PATCH 07/67] Update DiffieHellman.java --- src/main/java/com/thealgorithms/ciphers/DiffieHellman.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 25fedaf8ee83..8652fd507767 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -1,7 +1,7 @@ import java.math.BigInteger; import java.util.Scanner; -public class DiffieHellman { +public final class DiffieHellman { private DiffieHellman() { throw new UnsupportedOperationException("Utility class"); } From bbc6927e3037d98998c32875a8507827b69fe9b7 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:42:57 +0530 Subject: [PATCH 08/67] Create MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java new file mode 100644 index 000000000000..02fd3bc020db --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -0,0 +1,79 @@ +package MonoAlphabetic_Cipher; +import java.util.Scanner; + +public class MonoAlphabetic { + public static void main(String[] args) { + Scanner read = new Scanner(System.in); + System.out.println("Hello User! \nEnter your name:"); + String name = read.nextLine(); + read.nextLine(); + + System.out.println("Welcome "+name+"!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); + int x = read.nextInt(); + read.nextLine(); + + String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + + switch (x) { + case 1: + System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); + String data = read.nextLine().toUpperCase();; + + String encryptedData = encrypt(data,key); + System.out.println("Encrypted data: " + encryptedData); + break; + + case 2: + System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); + data = read.nextLine().toUpperCase();; + + String decryptedData = decrypt(data,key); + System.out.println("Decrypted data: " + decryptedData); + break; + + default: + System.out.println("The input was invalid. Kindly restart."); + break; + } + + + } + + public static String encrypt(String data, String key){ + int idx; + char c; + StringBuffer sb = new StringBuffer(data); + + for(int i=0; i Date: Tue, 1 Oct 2024 22:51:18 +0530 Subject: [PATCH 09/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 61 +++++++++---------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 02fd3bc020db..90f6f6b8b38f 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -8,68 +8,65 @@ public static void main(String[] args) { String name = read.nextLine(); read.nextLine(); - System.out.println("Welcome "+name+"!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); - int x = read.nextInt(); + System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); int x = read.nextInt(); read.nextLine(); String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; switch (x) { case 1: - System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); - String data = read.nextLine().toUpperCase();; - - String encryptedData = encrypt(data,key); - System.out.println("Encrypted data: " + encryptedData); - break; - - case 2: - System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); - data = read.nextLine().toUpperCase();; - - String decryptedData = decrypt(data,key); - System.out.println("Decrypted data: " + decryptedData); - break; - - default: - System.out.println("The input was invalid. Kindly restart."); - break; - } - - + System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); + String data = read.nextLine().toUpperCase(); + + String encryptedData = encrypt(data, key); + System.out.println("Encrypted data: " + encryptedData); + break; + + case 2: + System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); + data = read.nextLine().toUpperCase(); + + String decryptedData = decrypt(data, key); + System.out.println("Decrypted data: " + decryptedData); + break; + + default: + System.out.println("The input was invalid. Kindly restart."); + break; + } } - public static String encrypt(String data, String key){ + public static String encrypt(String data, String key) { int idx; char c; StringBuffer sb = new StringBuffer(data); - for(int i=0; i Date: Tue, 1 Oct 2024 22:54:40 +0530 Subject: [PATCH 10/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 90f6f6b8b38f..a2937c507ad8 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -41,7 +41,7 @@ public static String encrypt(String data, String key) { char c; StringBuffer sb = new StringBuffer(data); - for(int i=0; i Date: Tue, 1 Oct 2024 22:58:35 +0530 Subject: [PATCH 11/67] Update MonoAlphabetic.java --- .../com/thealgorithms/ciphers/MonoAlphabetic.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index a2937c507ad8..6dbef113f706 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -1,7 +1,6 @@ -package MonoAlphabetic_Cipher; import java.util.Scanner; -public class MonoAlphabetic { +public final class MonoAlphabetic { public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("Hello User! \nEnter your name:"); @@ -17,7 +16,6 @@ public static void main(String[] args) { case 1: System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); String data = read.nextLine().toUpperCase(); - String encryptedData = encrypt(data, key); System.out.println("Encrypted data: " + encryptedData); break; @@ -25,7 +23,6 @@ public static void main(String[] args) { case 2: System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); data = read.nextLine().toUpperCase(); - String decryptedData = decrypt(data, key); System.out.println("Decrypted data: " + decryptedData); break; @@ -41,7 +38,7 @@ public static String encrypt(String data, String key) { char c; StringBuffer sb = new StringBuffer(data); - for(int i = 0; i < sb.length(); i++) { + for (int i = 0; i < sb.length(); i++) { idx = sb.charAt(i) - 65; c = key.charAt(idx); sb.setCharAt(i, c); @@ -54,7 +51,7 @@ public static String decrypt(String data, String key) { char c; StringBuffer sb = new StringBuffer(data); - for(int i = 0; i < sb.length(); i++) { + for (int i = 0; i < sb.length(); i++) { c = sb.charAt(i); idx = getIndex(c, key); c = (char) (idx + 65); @@ -65,8 +62,8 @@ public static String decrypt(String data, String key) { public static int getIndex(char c, String key) { int idx = -1; - for(int i = 0; i < key.length(); i++) { - if(key.charAt(i) == c) { + for (int i = 0; i < key.length(); i++) { + if (key.charAt(i) == c) { idx = i; } } From b52479ddf2b50aad79290206e2a6b0312da8f87e Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:01:47 +0530 Subject: [PATCH 12/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 6dbef113f706..18d0ea9cf1db 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -6,30 +6,29 @@ public static void main(String[] args) { System.out.println("Hello User! \nEnter your name:"); String name = read.nextLine(); read.nextLine(); - - System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); int x = read.nextInt(); - read.nextLine(); + System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); + int x = read.nextInt(); String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; switch (x) { - case 1: - System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); - String data = read.nextLine().toUpperCase(); - String encryptedData = encrypt(data, key); - System.out.println("Encrypted data: " + encryptedData); - break; + case 1: + System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); + String data = read.nextLine().toUpperCase(); + String encryptedData = encrypt(data, key); + System.out.println("Encrypted data: " + encryptedData); + break; - case 2: - System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); - data = read.nextLine().toUpperCase(); - String decryptedData = decrypt(data, key); - System.out.println("Decrypted data: " + decryptedData); - break; + case 2: + System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); + data = read.nextLine().toUpperCase(); + String decryptedData = decrypt(data, key); + System.out.println("Decrypted data: " + decryptedData); + break; - default: - System.out.println("The input was invalid. Kindly restart."); - break; + default: + System.out.println("The input was invalid. Kindly restart."); + break; } } From 0fd2b5717f711f89a75b8c6140a1caf80fb9beb5 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:03:18 +0530 Subject: [PATCH 13/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 18d0ea9cf1db..327664c68f01 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -28,8 +28,8 @@ public static void main(String[] args) { default: System.out.println("The input was invalid. Kindly restart."); - break; - } + break; + } } public static String encrypt(String data, String key) { From 749099f6fe9039a9a08eb26d338df3ec869bf6fd Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:04:25 +0530 Subject: [PATCH 14/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 327664c68f01..f56e286d808e 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -29,7 +29,7 @@ public static void main(String[] args) { default: System.out.println("The input was invalid. Kindly restart."); break; - } + } } public static String encrypt(String data, String key) { From 31909e8bfaa59e5d7402bacd0f04dc2a1533a6b0 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:06:13 +0530 Subject: [PATCH 15/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index f56e286d808e..4eee0d07d9bd 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -25,7 +25,7 @@ public static void main(String[] args) { String decryptedData = decrypt(data, key); System.out.println("Decrypted data: " + decryptedData); break; - + default: System.out.println("The input was invalid. Kindly restart."); break; From 8cbe08ad063df7cc5a1e2dec09150bbd49811a49 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:07:26 +0530 Subject: [PATCH 16/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 4eee0d07d9bd..be32777fdf07 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -18,14 +18,12 @@ public static void main(String[] args) { String encryptedData = encrypt(data, key); System.out.println("Encrypted data: " + encryptedData); break; - case 2: System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); data = read.nextLine().toUpperCase(); String decryptedData = decrypt(data, key); System.out.println("Decrypted data: " + decryptedData); break; - default: System.out.println("The input was invalid. Kindly restart."); break; @@ -68,5 +66,4 @@ public static int getIndex(char c, String key) { } return idx; } - } From 7bdbec184d8cd7371134cc81a56521efa382f545 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Tue, 1 Oct 2024 23:09:53 +0530 Subject: [PATCH 17/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index be32777fdf07..585ae721df4f 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -1,6 +1,9 @@ import java.util.Scanner; public final class MonoAlphabetic { + private MonoAlphabetic() { + throw new UnsupportedOperationException("Utility class"); + } public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("Hello User! \nEnter your name:"); From 6daef3e9f134ebc65a645183f05cdb62318f33b9 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:11:22 +0530 Subject: [PATCH 18/67] removed the main methods and added proper JUnits tests --- .../thealgorithms/ciphers/MonoAlphabetic.java | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 585ae721df4f..3e3e1f2a9ae8 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -1,42 +1,12 @@ -import java.util.Scanner; - public final class MonoAlphabetic { private MonoAlphabetic() { throw new UnsupportedOperationException("Utility class"); } - public static void main(String[] args) { - Scanner read = new Scanner(System.in); - System.out.println("Hello User! \nEnter your name:"); - String name = read.nextLine(); - read.nextLine(); - System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); - int x = read.nextInt(); - - String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; - - switch (x) { - case 1: - System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data."); - String data = read.nextLine().toUpperCase(); - String encryptedData = encrypt(data, key); - System.out.println("Encrypted data: " + encryptedData); - break; - case 2: - System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data."); - data = read.nextLine().toUpperCase(); - String decryptedData = decrypt(data, key); - System.out.println("Decrypted data: " + decryptedData); - break; - default: - System.out.println("The input was invalid. Kindly restart."); - break; - } - } public static String encrypt(String data, String key) { int idx; char c; - StringBuffer sb = new StringBuffer(data); + StringBuffer sb = new StringBuffer(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { idx = sb.charAt(i) - 65; @@ -49,7 +19,7 @@ public static String encrypt(String data, String key) { public static String decrypt(String data, String key) { int idx; char c; - StringBuffer sb = new StringBuffer(data); + StringBuffer sb = new StringBuffer(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { c = sb.charAt(i); @@ -61,12 +31,48 @@ public static String decrypt(String data, String key) { } public static int getIndex(char c, String key) { - int idx = -1; for (int i = 0; i < key.length(); i++) { if (key.charAt(i) == c) { - idx = i; + return i; } } - return idx; + return -1; + } +} + +// JUnit Tests for MonoAlphabetic Cipher +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import static org.junit.jupiter.api.Assertions.*; + +public class MonoAlphabeticTest { + + private final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + + @ParameterizedTest + @DisplayName("Encrypt Test with MonoAlphabetic Cipher") + @CsvSource({ + "HELLO, DLZZI", + "WORLD, XMFLD", + "JAVA, HBWB", + "OPENAI, IUPNMW" + }) + void testEncrypt(String plainText, String expectedCipherText) { + String encrypted = MonoAlphabetic.encrypt(plainText, key); + assertEquals(expectedCipherText, encrypted, "Encryption failed for input: " + plainText); + } + + @ParameterizedTest + @DisplayName("Decrypt Test with MonoAlphabetic Cipher") + @CsvSource({ + "DLZZI, HELLO", + "XMFLD, WORLD", + "HBWB, JAVA", + "IUPNMW, OPENAI" + }) + void testDecrypt(String cipherText, String expectedPlainText) { + String decrypted = MonoAlphabetic.decrypt(cipherText, key); + assertEquals(expectedPlainText, decrypted, "Decryption failed for input: " + cipherText); } } From bd545051290696ac7edbe5ca303a7b3df632331e Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:14:45 +0530 Subject: [PATCH 19/67] Removed the main methods and added proper JUnit tests to DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 8652fd507767..7dd65c817723 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -1,42 +1,48 @@ import java.math.BigInteger; -import java.util.Scanner; public final class DiffieHellman { private DiffieHellman() { throw new UnsupportedOperationException("Utility class"); } - public static void main(String[] args) { - Scanner read = new Scanner(System.in); - System.out.println("Hello User! \nEnter your name:"); - String name = read.nextLine(); - read.nextLine(); - System.out.println("Welcome " + name + "!"); - - BigInteger n; - BigInteger g; - BigInteger x; - BigInteger y; - BigInteger k1; - BigInteger k2; - BigInteger a; - BigInteger b; - - System.out.println("Enter two prime numbers: "); - n = new BigInteger(read.next()); - g = new BigInteger(read.next()); - - System.out.println("Person A : Enter your secret number"); - x = new BigInteger(read.next()); - a = g.modPow(x, n); - - System.out.println("Person B : Enter your secret number"); - y = new BigInteger(read.next()); - b = g.modPow(y, n); - - k1 = b.modPow(x, n); - k2 = a.modPow(y, n); - - System.out.println("A's secret key: " + k1); - System.out.println("B's secret key: " + k2); + + public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { + return base.modPow(secret, prime); + } + + public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { + return otherPublicValue.modPow(secret, prime); + } +} + +// DiffieHellmanTest.java - JUnit Tests +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import static org.junit.jupiter.api.Assertions.*; + +import java.math.BigInteger; + +public class DiffieHellmanTest { + + @ParameterizedTest + @DisplayName("Diffie-Hellman Key Exchange Test") + @CsvSource({ + "23, 5, 6, 15", + "97, 7, 12, 23", + "61, 2, 9, 19" + }) + void testDiffieHellman(String nStr, String gStr, String xStr, String yStr) { + BigInteger n = new BigInteger(nStr); + BigInteger g = new BigInteger(gStr); + BigInteger x = new BigInteger(xStr); + BigInteger y = new BigInteger(yStr); + + BigInteger a = DiffieHellman.calculatePublicValue(g, x, n); + BigInteger b = DiffieHellman.calculatePublicValue(g, y, n); + + BigInteger k1 = DiffieHellman.calculateSharedSecret(b, x, n); + BigInteger k2 = DiffieHellman.calculateSharedSecret(a, y, n); + + assertEquals(k1, k2, "Shared secret keys do not match for inputs n=" + nStr + ", g=" + gStr); } } From f021dbb2b9781112a895ea6ce2de2ca3b51056d1 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:22:02 +0530 Subject: [PATCH 20/67] updated DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 70 ++++++++++++------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 7dd65c817723..500970dfe762 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -1,48 +1,64 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.math.BigInteger; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; public final class DiffieHellman { + + // Private constructor to prevent instantiation of utility class private DiffieHellman() { throw new UnsupportedOperationException("Utility class"); } + // Method to calculate public value (g^x mod p) public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { + // Returns g^x mod p return base.modPow(secret, prime); } + // Method to calculate the shared secret key (otherPublic^secret mod p) public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { + // Returns b^x mod p or a^y mod p return otherPublicValue.modPow(secret, prime); } -} -// DiffieHellmanTest.java - JUnit Tests -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import static org.junit.jupiter.api.Assertions.*; + // *********** Unit Test Section ************** -import java.math.BigInteger; + // Method to provide test data for public key calculation + private static Stream providePublicKeyData() { + return Stream.of( + // base, secret, prime, expected public value + Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), + Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) + ); + } + + // Test for public key calculation + @ParameterizedTest + @MethodSource("providePublicKeyData") + public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { + assertEquals(expected, DiffieHellman.calculatePublicValue(base, secret, prime)); + } -public class DiffieHellmanTest { + // Method to provide test data for shared secret calculation + private static Stream provideSharedSecretData() { + return Stream.of( + // otherPublic, secret, prime, expected shared secret + Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), + Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12")) + ); + } + // Test for shared secret calculation @ParameterizedTest - @DisplayName("Diffie-Hellman Key Exchange Test") - @CsvSource({ - "23, 5, 6, 15", - "97, 7, 12, 23", - "61, 2, 9, 19" - }) - void testDiffieHellman(String nStr, String gStr, String xStr, String yStr) { - BigInteger n = new BigInteger(nStr); - BigInteger g = new BigInteger(gStr); - BigInteger x = new BigInteger(xStr); - BigInteger y = new BigInteger(yStr); - - BigInteger a = DiffieHellman.calculatePublicValue(g, x, n); - BigInteger b = DiffieHellman.calculatePublicValue(g, y, n); - - BigInteger k1 = DiffieHellman.calculateSharedSecret(b, x, n); - BigInteger k2 = DiffieHellman.calculateSharedSecret(a, y, n); - - assertEquals(k1, k2, "Shared secret keys do not match for inputs n=" + nStr + ", g=" + gStr); + @MethodSource("provideSharedSecretData") + public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime, BigInteger expected) { + assertEquals(expected, DiffieHellman.calculateSharedSecret(otherPublicValue, secret, prime)); } } From 175def50cf78f1d8a58c5d8d99282f7d2d8434c5 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:22:26 +0530 Subject: [PATCH 21/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 98 +++++++++++-------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 3e3e1f2a9ae8..681b2ed1860f 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -1,78 +1,92 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + public final class MonoAlphabetic { + + // Private constructor to prevent instantiation of utility class private MonoAlphabetic() { throw new UnsupportedOperationException("Utility class"); } + // Encryption method public static String encrypt(String data, String key) { int idx; char c; - StringBuffer sb = new StringBuffer(data.toUpperCase()); + StringBuilder sb = new StringBuilder(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { - idx = sb.charAt(i) - 65; - c = key.charAt(idx); - sb.setCharAt(i, c); + idx = sb.charAt(i) - 65; // Subtract ASCII value of 'A' to get index + c = key.charAt(idx); // Find the character at the corresponding key position + sb.setCharAt(i, c); // Replace with the key character } - return new String(sb); + return sb.toString(); } + // Decryption method public static String decrypt(String data, String key) { int idx; char c; - StringBuffer sb = new StringBuffer(data.toUpperCase()); + StringBuilder sb = new StringBuilder(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { - c = sb.charAt(i); - idx = getIndex(c, key); - c = (char) (idx + 65); - sb.setCharAt(i, c); + c = sb.charAt(i); // Get the character from encrypted data + idx = getIndex(c, key); // Get the corresponding index from the key + c = (char) (idx + 65); // Convert index back to character + sb.setCharAt(i, c); // Replace with the original character } - return new String(sb); + return sb.toString(); } + // Helper method to get index of a character in the key public static int getIndex(char c, String key) { for (int i = 0; i < key.length(); i++) { if (key.charAt(i) == c) { - return i; + return i; // Return the index if the character matches } } - return -1; + return -1; // Return -1 if character not found (should not happen for valid inputs) } -} - -// JUnit Tests for MonoAlphabetic Cipher -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import static org.junit.jupiter.api.Assertions.*; -public class MonoAlphabeticTest { + // *********** Unit Test Section ************** - private final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + // Method to provide test data for encryption + private static Stream provideEncryptionData() { + String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + return Stream.of( + // Input data, key, expected encrypted output + Arguments.of("HELLO", key, "GFSSD"), + Arguments.of("JAVA", key, "MZSM") + ); + } + // Test for encryption @ParameterizedTest - @DisplayName("Encrypt Test with MonoAlphabetic Cipher") - @CsvSource({ - "HELLO, DLZZI", - "WORLD, XMFLD", - "JAVA, HBWB", - "OPENAI, IUPNMW" - }) - void testEncrypt(String plainText, String expectedCipherText) { - String encrypted = MonoAlphabetic.encrypt(plainText, key); - assertEquals(expectedCipherText, encrypted, "Encryption failed for input: " + plainText); + @MethodSource("provideEncryptionData") + public void testEncrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.encrypt(data, key)); + } + + // Method to provide test data for decryption + private static Stream provideDecryptionData() { + String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + return Stream.of( + // Encrypted data, key, expected decrypted output + Arguments.of("GFSSD", key, "HELLO"), + Arguments.of("MZSM", key, "JAVA") + ); } + // Test for decryption @ParameterizedTest - @DisplayName("Decrypt Test with MonoAlphabetic Cipher") - @CsvSource({ - "DLZZI, HELLO", - "XMFLD, WORLD", - "HBWB, JAVA", - "IUPNMW, OPENAI" - }) - void testDecrypt(String cipherText, String expectedPlainText) { - String decrypted = MonoAlphabetic.decrypt(cipherText, key); - assertEquals(expectedPlainText, decrypted, "Decryption failed for input: " + cipherText); + @MethodSource("provideDecryptionData") + public void testDecrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.decrypt(data, key)); } } From 1d2cc01315b0cf8b87ee4f60ec872801ca861cff Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:33:47 +0530 Subject: [PATCH 22/67] Create DiffieHellmanTest.java --- .../ciphers/DiffieHellmanTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java new file mode 100644 index 000000000000..1b10695b94d3 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -0,0 +1,23 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigInteger; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class DiffieHellmanTest { + + // Method to provide test data for public key calculation + private static Stream providePublicKeyData() { + return Stream.of( + // base, secret, prime, expected public value + Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), + Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) + ); + } + + // Test for public key calculation + @ParameterizedTest + @MethodSource("providePublicKey From db4d4dc512a7a200191324f19f1524d1146494d7 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:34:55 +0530 Subject: [PATCH 23/67] Update DiffieHellmanTest.java --- .../ciphers/DiffieHellmanTest.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 1b10695b94d3..c4447d8a14e6 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -1,23 +1,27 @@ -import static org.junit.jupiter.api.Assertions.assertEquals; +package com.thealgorithms.ciphers; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; import java.math.BigInteger; -import java.util.stream.Stream; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +class DiffieHellmanTest { -public class DiffieHellmanTest { + @Test + void testDiffieHellmanSharedKey() { + BigInteger p = new BigInteger("23"); + BigInteger g = new BigInteger("5"); + BigInteger a = new BigInteger("6"); // Private key for Alice + BigInteger b = new BigInteger("15"); // Private key for Bob - // Method to provide test data for public key calculation - private static Stream providePublicKeyData() { - return Stream.of( - // base, secret, prime, expected public value - Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), - Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) - ); - } + DiffieHellman alice = new DiffieHellman(p, g, a); + DiffieHellman bob = new DiffieHellman(p, g, b); + + BigInteger A = alice.getPublicKey(); + BigInteger B = bob.getPublicKey(); - // Test for public key calculation - @ParameterizedTest - @MethodSource("providePublicKey + BigInteger aliceSharedKey = alice.computeSharedKey(B); + BigInteger bobSharedKey = bob.computeSharedKey(A); + + assertEquals(aliceSharedKey, bobSharedKey, "Shared keys do not match!"); + } +} From afdf7e4c208d9ffd733b40bf082d0760f2d216e0 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:37:15 +0530 Subject: [PATCH 24/67] Create MonoAlphabeticTest.java --- .../ciphers/MonoAlphabeticTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java new file mode 100644 index 000000000000..1f0508264693 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -0,0 +1,61 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class MonoAlphabeticTest { + private MonoAlphabetic monoAlphabetic; + + @BeforeEach + public void setUp() { + // Initialize the MonoAlphabetic cipher with a sample key + String key = "QWERTYUIOPASDFGHJKLZXCVBNM"; // Example key + monoAlphabetic = new MonoAlphabetic(key); + } + + @Test + public void testEncrypt() { + String plaintext = "HELLO"; + String expectedCiphertext = "ITSSG"; // Expected result based on the key + String actualCiphertext = monoAlphabetic.encrypt(plaintext); + + assertEquals(expectedCiphertext, actualCiphertext, "Encryption should match the expected ciphertext."); + } + + @Test + public void testDecrypt() { + String ciphertext = "ITSSG"; + String expectedPlaintext = "HELLO"; // Expected result based on the key + String actualPlaintext = monoAlphabetic.decrypt(ciphertext); + + assertEquals(expectedPlaintext, actualPlaintext, "Decryption should match the expected plaintext."); + } + + @Test + public void testEncryptAndDecrypt() { + String plaintext = "HELLO"; + String ciphertext = monoAlphabetic.encrypt(plaintext); + String decryptedText = monoAlphabetic.decrypt(ciphertext); + + assertEquals(plaintext, decryptedText, "Decrypting the ciphertext should return the original plaintext."); + } + + @Test + public void testEncryptWithSpecialCharacters() { + String plaintext = "HELLO, WORLD!"; + String expectedCiphertext = "ITSSG, GQHSG!"; + String actualCiphertext = monoAlphabetic.encrypt(plaintext); + + assertEquals(expectedCiphertext, actualCiphertext, "Encryption should correctly handle special characters."); + } + + @Test + public void testDecryptWithSpecialCharacters() { + String ciphertext = "ITSSG, GQHSG!"; + String expectedPlaintext = "HELLO, WORLD!"; + String actualPlaintext = monoAlphabetic.decrypt(ciphertext); + + assertEquals(expectedPlaintext, actualPlaintext, "Decryption should correctly handle special characters."); + } +} From 2bd00f886f2d51d2f9e82d805d05e0d582352df5 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:39:32 +0530 Subject: [PATCH 25/67] Update DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 43 +------------------ 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 500970dfe762..1f39afe10be1 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -1,16 +1,9 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.math.BigInteger; -import java.util.stream.Stream; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; public final class DiffieHellman { - + // Private constructor to prevent instantiation of utility class private DiffieHellman() { throw new UnsupportedOperationException("Utility class"); @@ -27,38 +20,4 @@ public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigI // Returns b^x mod p or a^y mod p return otherPublicValue.modPow(secret, prime); } - - // *********** Unit Test Section ************** - - // Method to provide test data for public key calculation - private static Stream providePublicKeyData() { - return Stream.of( - // base, secret, prime, expected public value - Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), - Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) - ); - } - - // Test for public key calculation - @ParameterizedTest - @MethodSource("providePublicKeyData") - public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { - assertEquals(expected, DiffieHellman.calculatePublicValue(base, secret, prime)); - } - - // Method to provide test data for shared secret calculation - private static Stream provideSharedSecretData() { - return Stream.of( - // otherPublic, secret, prime, expected shared secret - Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), - Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12")) - ); - } - - // Test for shared secret calculation - @ParameterizedTest - @MethodSource("provideSharedSecretData") - public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime, BigInteger expected) { - assertEquals(expected, DiffieHellman.calculateSharedSecret(otherPublicValue, secret, prime)); - } } From 98589d95b7e1eb3462b207f250512deee9a4ea2e Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:40:47 +0530 Subject: [PATCH 26/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 44 ------------------- 1 file changed, 44 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 681b2ed1860f..c54770e9f6a9 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -1,13 +1,5 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.stream.Stream; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - public final class MonoAlphabetic { // Private constructor to prevent instantiation of utility class @@ -53,40 +45,4 @@ public static int getIndex(char c, String key) { } return -1; // Return -1 if character not found (should not happen for valid inputs) } - - // *********** Unit Test Section ************** - - // Method to provide test data for encryption - private static Stream provideEncryptionData() { - String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; - return Stream.of( - // Input data, key, expected encrypted output - Arguments.of("HELLO", key, "GFSSD"), - Arguments.of("JAVA", key, "MZSM") - ); - } - - // Test for encryption - @ParameterizedTest - @MethodSource("provideEncryptionData") - public void testEncrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.encrypt(data, key)); - } - - // Method to provide test data for decryption - private static Stream provideDecryptionData() { - String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; - return Stream.of( - // Encrypted data, key, expected decrypted output - Arguments.of("GFSSD", key, "HELLO"), - Arguments.of("MZSM", key, "JAVA") - ); - } - - // Test for decryption - @ParameterizedTest - @MethodSource("provideDecryptionData") - public void testDecrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.decrypt(data, key)); - } } From 9fbfa38386f4f8f1cc9215ab2a4192c04af3994b Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:44:15 +0530 Subject: [PATCH 27/67] Update DiffieHellmanTest.java --- .../java/com/thealgorithms/ciphers/DiffieHellmanTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index c4447d8a14e6..b1a2f190db7e 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -1,8 +1,9 @@ package com.thealgorithms.ciphers; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; + import java.math.BigInteger; +import org.junit.jupiter.api.Test; class DiffieHellmanTest { @@ -10,7 +11,7 @@ class DiffieHellmanTest { void testDiffieHellmanSharedKey() { BigInteger p = new BigInteger("23"); BigInteger g = new BigInteger("5"); - BigInteger a = new BigInteger("6"); // Private key for Alice + BigInteger a = new BigInteger("6"); // Private key for Alice BigInteger b = new BigInteger("15"); // Private key for Bob DiffieHellman alice = new DiffieHellman(p, g, a); From c3dcd2d626ac997ebe3ef6d7af61b3e61c549cc9 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:45:28 +0530 Subject: [PATCH 28/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 1f0508264693..52199f7be808 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.ciphers; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -19,7 +20,7 @@ public void testEncrypt() { String plaintext = "HELLO"; String expectedCiphertext = "ITSSG"; // Expected result based on the key String actualCiphertext = monoAlphabetic.encrypt(plaintext); - + assertEquals(expectedCiphertext, actualCiphertext, "Encryption should match the expected ciphertext."); } @@ -28,7 +29,7 @@ public void testDecrypt() { String ciphertext = "ITSSG"; String expectedPlaintext = "HELLO"; // Expected result based on the key String actualPlaintext = monoAlphabetic.decrypt(ciphertext); - + assertEquals(expectedPlaintext, actualPlaintext, "Decryption should match the expected plaintext."); } @@ -37,7 +38,7 @@ public void testEncryptAndDecrypt() { String plaintext = "HELLO"; String ciphertext = monoAlphabetic.encrypt(plaintext); String decryptedText = monoAlphabetic.decrypt(ciphertext); - + assertEquals(plaintext, decryptedText, "Decrypting the ciphertext should return the original plaintext."); } From 8b580dfab5a9ffe1bf01151f1e9f43375ae084fe Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:46:52 +0530 Subject: [PATCH 29/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 52199f7be808..6075b887ec5f 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -47,7 +47,7 @@ public void testEncryptWithSpecialCharacters() { String plaintext = "HELLO, WORLD!"; String expectedCiphertext = "ITSSG, GQHSG!"; String actualCiphertext = monoAlphabetic.encrypt(plaintext); - + assertEquals(expectedCiphertext, actualCiphertext, "Encryption should correctly handle special characters."); } @@ -56,7 +56,7 @@ public void testDecryptWithSpecialCharacters() { String ciphertext = "ITSSG, GQHSG!"; String expectedPlaintext = "HELLO, WORLD!"; String actualPlaintext = monoAlphabetic.decrypt(ciphertext); - + assertEquals(expectedPlaintext, actualPlaintext, "Decryption should correctly handle special characters."); } } From a31ae3b48bdb9929d1189855196541ee80083de4 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:05:50 +0530 Subject: [PATCH 30/67] Update DiffieHellmanTest.java --- .../ciphers/DiffieHellmanTest.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index b1a2f190db7e..574ff2db443f 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -1,28 +1,43 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.math.BigInteger; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; -class DiffieHellmanTest { +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; - @Test - void testDiffieHellmanSharedKey() { - BigInteger p = new BigInteger("23"); - BigInteger g = new BigInteger("5"); - BigInteger a = new BigInteger("6"); // Private key for Alice - BigInteger b = new BigInteger("15"); // Private key for Bob +public class DiffieHellmanTest { - DiffieHellman alice = new DiffieHellman(p, g, a); - DiffieHellman bob = new DiffieHellman(p, g, b); + // Test for public value calculation + @ParameterizedTest + @MethodSource("providePublicKeyData") + public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { + assertEquals(expected, DiffieHellman.calculatePublicValue(base, secret, prime)); + } - BigInteger A = alice.getPublicKey(); - BigInteger B = bob.getPublicKey(); + // Test for shared secret calculation + @ParameterizedTest + @MethodSource("provideSharedSecretData") + public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime, BigInteger expected) { + assertEquals(expected, DiffieHellman.calculateSharedSecret(otherPublicValue, secret, prime)); + } - BigInteger aliceSharedKey = alice.computeSharedKey(B); - BigInteger bobSharedKey = bob.computeSharedKey(A); + // Provide test data for public key calculation + private static Stream providePublicKeyData() { + return Stream.of( + Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), + Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) + ); + } - assertEquals(aliceSharedKey, bobSharedKey, "Shared keys do not match!"); + // Provide test data for shared secret calculation + private static Stream provideSharedSecretData() { + return Stream.of( + Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), + Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12")) + ); } } From 283b5f9987b98ef87de06a592c37e20164fbde87 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:06:54 +0530 Subject: [PATCH 31/67] Update MonoAlphabeticTest.java --- .../ciphers/MonoAlphabeticTest.java | 74 +++++++------------ 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 6075b887ec5f..cde20ea3c8b3 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -2,61 +2,43 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; -public class MonoAlphabeticTest { - private MonoAlphabetic monoAlphabetic; - - @BeforeEach - public void setUp() { - // Initialize the MonoAlphabetic cipher with a sample key - String key = "QWERTYUIOPASDFGHJKLZXCVBNM"; // Example key - monoAlphabetic = new MonoAlphabetic(key); - } +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; - @Test - public void testEncrypt() { - String plaintext = "HELLO"; - String expectedCiphertext = "ITSSG"; // Expected result based on the key - String actualCiphertext = monoAlphabetic.encrypt(plaintext); - - assertEquals(expectedCiphertext, actualCiphertext, "Encryption should match the expected ciphertext."); - } +public class MonoAlphabeticTest { - @Test - public void testDecrypt() { - String ciphertext = "ITSSG"; - String expectedPlaintext = "HELLO"; // Expected result based on the key - String actualPlaintext = monoAlphabetic.decrypt(ciphertext); + private static final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; - assertEquals(expectedPlaintext, actualPlaintext, "Decryption should match the expected plaintext."); + // Test for encryption + @ParameterizedTest + @MethodSource("provideEncryptionData") + public void testEncrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.encrypt(data, key)); } - @Test - public void testEncryptAndDecrypt() { - String plaintext = "HELLO"; - String ciphertext = monoAlphabetic.encrypt(plaintext); - String decryptedText = monoAlphabetic.decrypt(ciphertext); - - assertEquals(plaintext, decryptedText, "Decrypting the ciphertext should return the original plaintext."); + // Test for decryption + @ParameterizedTest + @MethodSource("provideDecryptionData") + public void testDecrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.decrypt(data, key)); } - @Test - public void testEncryptWithSpecialCharacters() { - String plaintext = "HELLO, WORLD!"; - String expectedCiphertext = "ITSSG, GQHSG!"; - String actualCiphertext = monoAlphabetic.encrypt(plaintext); - - assertEquals(expectedCiphertext, actualCiphertext, "Encryption should correctly handle special characters."); + // Provide test data for encryption + private static Stream provideEncryptionData() { + return Stream.of( + Arguments.of("HELLO", key, "GFSSD"), + Arguments.of("JAVA", key, "MZSM") + ); } - @Test - public void testDecryptWithSpecialCharacters() { - String ciphertext = "ITSSG, GQHSG!"; - String expectedPlaintext = "HELLO, WORLD!"; - String actualPlaintext = monoAlphabetic.decrypt(ciphertext); - - assertEquals(expectedPlaintext, actualPlaintext, "Decryption should correctly handle special characters."); + // Provide test data for decryption + private static Stream provideDecryptionData() { + return Stream.of( + Arguments.of("GFSSD", key, "HELLO"), + Arguments.of("MZSM", key, "JAVA") + ); } } From 5e8b5364eeb2d877f253e35ef630e3b3ee31ee4a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:13:28 +0530 Subject: [PATCH 32/67] Update DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 1f39afe10be1..4bb3b7dc500f 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -4,20 +4,35 @@ public final class DiffieHellman { - // Private constructor to prevent instantiation of utility class - private DiffieHellman() { - throw new UnsupportedOperationException("Utility class"); + private final BigInteger base; + private final BigInteger secret; + private final BigInteger prime; + + // Constructor to initialize base, secret, and prime + public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) { + this.base = base; + this.secret = secret; + this.prime = prime; } // Method to calculate public value (g^x mod p) - public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { + public BigInteger calculatePublicValue() { // Returns g^x mod p return base.modPow(secret, prime); } // Method to calculate the shared secret key (otherPublic^secret mod p) - public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { + public BigInteger calculateSharedSecret(BigInteger otherPublicValue) { // Returns b^x mod p or a^y mod p return otherPublicValue.modPow(secret, prime); } + + // Static utility methods for direct calculation (if needed) + public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { + return base.modPow(secret, prime); + } + + public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { + return otherPublicValue.modPow(secret, prime); + } } From 32889e7ba993da47ce1052ddce2c2f2ad6d235c6 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:15:36 +0530 Subject: [PATCH 33/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index c54770e9f6a9..bc6659e50f41 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -2,13 +2,15 @@ public final class MonoAlphabetic { - // Private constructor to prevent instantiation of utility class - private MonoAlphabetic() { - throw new UnsupportedOperationException("Utility class"); + private final String key; + + // Constructor to initialize the key for encryption and decryption + public MonoAlphabetic(String key) { + this.key = key.toUpperCase(); // Store the key in uppercase to match the encryption/decryption logic } // Encryption method - public static String encrypt(String data, String key) { + public String encrypt(String data) { int idx; char c; StringBuilder sb = new StringBuilder(data.toUpperCase()); @@ -22,14 +24,14 @@ public static String encrypt(String data, String key) { } // Decryption method - public static String decrypt(String data, String key) { + public String decrypt(String data) { int idx; char c; StringBuilder sb = new StringBuilder(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { c = sb.charAt(i); // Get the character from encrypted data - idx = getIndex(c, key); // Get the corresponding index from the key + idx = getIndex(c); // Get the corresponding index from the key c = (char) (idx + 65); // Convert index back to character sb.setCharAt(i, c); // Replace with the original character } @@ -37,7 +39,7 @@ public static String decrypt(String data, String key) { } // Helper method to get index of a character in the key - public static int getIndex(char c, String key) { + private int getIndex(char c) { for (int i = 0; i < key.length(); i++) { if (key.charAt(i) == c) { return i; // Return the index if the character matches @@ -45,4 +47,13 @@ public static int getIndex(char c, String key) { } return -1; // Return -1 if character not found (should not happen for valid inputs) } + + // Static utility methods for encryption/decryption without creating an instance + public static String encrypt(String data, String key) { + return new MonoAlphabetic(key).encrypt(data); + } + + public static String decrypt(String data, String key) { + return new MonoAlphabetic(key).decrypt(data); + } } From 577a9b7bb7cd268a9febd97c92d982c4b6785f39 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:18:11 +0530 Subject: [PATCH 34/67] Update DiffieHellmanTest.java --- .../com/thealgorithms/ciphers/DiffieHellmanTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 574ff2db443f..d7b15c8878e0 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -27,17 +27,11 @@ public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger se // Provide test data for public key calculation private static Stream providePublicKeyData() { - return Stream.of( - Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), - Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) - ); + return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"))); } // Provide test data for shared secret calculation private static Stream provideSharedSecretData() { - return Stream.of( - Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), - Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12")) - ); + return Stream.of(Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12"))); } } From 697eed562a2738b1c1f6f7bd20d5e2d42f6af67c Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:19:00 +0530 Subject: [PATCH 35/67] Update MonoAlphabeticTest.java --- .../com/thealgorithms/ciphers/MonoAlphabeticTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index cde20ea3c8b3..63be7e31630f 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -28,17 +28,11 @@ public void testDecrypt(String data, String key, String expected) { // Provide test data for encryption private static Stream provideEncryptionData() { - return Stream.of( - Arguments.of("HELLO", key, "GFSSD"), - Arguments.of("JAVA", key, "MZSM") - ); + return Stream.of(Arguments.of("HELLO", key, "GFSSD"), Arguments.of("JAVA", key, "MZSM")); } // Provide test data for decryption private static Stream provideDecryptionData() { - return Stream.of( - Arguments.of("GFSSD", key, "HELLO"), - Arguments.of("MZSM", key, "JAVA") - ); + return Stream.of(Arguments.of("GFSSD", key, "HELLO"), Arguments.of("MZSM", key, "JAVA")); } } From c7cafbcc3ef411da72081ee8ecaf3a254dfa526a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:20:01 +0530 Subject: [PATCH 36/67] Update MonoAlphabeticTest.java --- src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 63be7e31630f..b3c6718be51d 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; - import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; From eea1c0c3f4959adb9bfc71d8ac584b98999665f2 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:20:35 +0530 Subject: [PATCH 37/67] Update DiffieHellmanTest.java --- src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index d7b15c8878e0..839b0bc099c0 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -4,7 +4,6 @@ import java.math.BigInteger; import java.util.stream.Stream; - import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; From f2ccca5a473c0fe0a5e1c7e9327827281552df8a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:29:59 +0530 Subject: [PATCH 38/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index bc6659e50f41..54be9f43caa2 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -11,41 +11,32 @@ public MonoAlphabetic(String key) { // Encryption method public String encrypt(String data) { - int idx; - char c; StringBuilder sb = new StringBuilder(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { - idx = sb.charAt(i) - 65; // Subtract ASCII value of 'A' to get index - c = key.charAt(idx); // Find the character at the corresponding key position - sb.setCharAt(i, c); // Replace with the key character + char currentChar = sb.charAt(i); + if (Character.isLetter(currentChar)) { // Check if it's a letter + int index = currentChar - 'A'; // Get the index for the character + sb.setCharAt(i, key.charAt(index)); // Replace with the key character + } } return sb.toString(); } // Decryption method public String decrypt(String data) { - int idx; - char c; StringBuilder sb = new StringBuilder(data.toUpperCase()); for (int i = 0; i < sb.length(); i++) { - c = sb.charAt(i); // Get the character from encrypted data - idx = getIndex(c); // Get the corresponding index from the key - c = (char) (idx + 65); // Convert index back to character - sb.setCharAt(i, c); // Replace with the original character - } - return sb.toString(); - } - - // Helper method to get index of a character in the key - private int getIndex(char c) { - for (int i = 0; i < key.length(); i++) { - if (key.charAt(i) == c) { - return i; // Return the index if the character matches + char currentChar = sb.charAt(i); + if (Character.isLetter(currentChar)) { // Check if it's a letter + int index = key.indexOf(currentChar); // Find the character in the key + if (index != -1) { + sb.setCharAt(i, (char) (index + 'A')); // Replace with the original character + } } } - return -1; // Return -1 if character not found (should not happen for valid inputs) + return sb.toString(); } // Static utility methods for encryption/decryption without creating an instance From 40e69d2749e4e40e8d3dea4b0ebc9848d8c4b59c Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:32:18 +0530 Subject: [PATCH 39/67] Update DiffieHellman.java --- .../com/thealgorithms/ciphers/DiffieHellman.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 4bb3b7dc500f..5d00954a5317 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -10,6 +10,10 @@ public final class DiffieHellman { // Constructor to initialize base, secret, and prime public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) { + // Check for non-null and positive values + if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { + throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values."); + } this.base = base; this.secret = secret; this.prime = prime; @@ -23,16 +27,27 @@ public BigInteger calculatePublicValue() { // Method to calculate the shared secret key (otherPublic^secret mod p) public BigInteger calculateSharedSecret(BigInteger otherPublicValue) { + if (otherPublicValue == null || otherPublicValue.signum() <= 0) { + throw new IllegalArgumentException("Other public value must be non-null and positive."); + } // Returns b^x mod p or a^y mod p return otherPublicValue.modPow(secret, prime); } // Static utility methods for direct calculation (if needed) public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { + // Check for valid inputs + if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { + throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values."); + } return base.modPow(secret, prime); } public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { + // Check for valid inputs + if (otherPublicValue == null || secret == null || prime == null || otherPublicValue.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { + throw new IllegalArgumentException("Other public value, secret, and prime must be non-null and positive values."); + } return otherPublicValue.modPow(secret, prime); } } From 423455cfbe7dc3521ec3cffd9133aa4f0b69f5bd Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:37:24 +0530 Subject: [PATCH 40/67] Update DiffieHellmanTest.java --- .../thealgorithms/ciphers/DiffieHellmanTest.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 839b0bc099c0..261be2e6ba64 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -26,11 +26,23 @@ public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger se // Provide test data for public key calculation private static Stream providePublicKeyData() { - return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"))); + // Corrected test cases + return Stream.of( + // g^x mod p, e.g., 5^6 mod 23 = 8 + Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), + // 2^5 mod 13 = 6 + Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) + ); } // Provide test data for shared secret calculation private static Stream provideSharedSecretData() { - return Stream.of(Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12"))); + // Corrected test cases + return Stream.of( + // b^x mod p, e.g., 8^6 mod 23 = 13 + Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("13")), + // 6^5 mod 13 = 2 + Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("2")) + ); } } From 25390347daab5bce97e6dde81b0c6bb08ae89c35 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:39:56 +0530 Subject: [PATCH 41/67] Update DiffieHellmanTest.java --- .../thealgorithms/ciphers/DiffieHellmanTest.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 261be2e6ba64..b2f056088cf7 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -26,23 +26,11 @@ public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger se // Provide test data for public key calculation private static Stream providePublicKeyData() { - // Corrected test cases - return Stream.of( - // g^x mod p, e.g., 5^6 mod 23 = 8 - Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), - // 2^5 mod 13 = 6 - Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) - ); + return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"))); } // Provide test data for shared secret calculation private static Stream provideSharedSecretData() { - // Corrected test cases - return Stream.of( - // b^x mod p, e.g., 8^6 mod 23 = 13 - Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("13")), - // 6^5 mod 13 = 2 - Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("2")) - ); + return Stream.of(Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("13")), Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("2"))); } } From 888314f46b125317850daa85c11e594c9e936505 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:45:10 +0530 Subject: [PATCH 42/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index b3c6718be51d..3302f7dc2f8b 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -27,11 +27,11 @@ public void testDecrypt(String data, String key, String expected) { // Provide test data for encryption private static Stream provideEncryptionData() { - return Stream.of(Arguments.of("HELLO", key, "GFSSD"), Arguments.of("JAVA", key, "MZSM")); + return Stream.of(Arguments.of("HELLO", key, "YQJJQ"), Arguments.of("JAVA", key, "HQLI")); } // Provide test data for decryption private static Stream provideDecryptionData() { - return Stream.of(Arguments.of("GFSSD", key, "HELLO"), Arguments.of("MZSM", key, "JAVA")); + return Stream.of(Arguments.of("YQJJQ", key, "HELLO"), Arguments.of("HQLI", key, "JAVA")); } } From 63f803f4d6721a5e0bcdd97f5ca8ecf5941b3941 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Wed, 2 Oct 2024 23:52:12 +0530 Subject: [PATCH 43/67] Update MonoAlphabeticTest.java From d28faa2a9f5380d685d994c900419c633038cd2d Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 00:07:28 +0530 Subject: [PATCH 44/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 54be9f43caa2..a56997916344 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -2,49 +2,63 @@ public final class MonoAlphabetic { - private final String key; - - // Constructor to initialize the key for encryption and decryption - public MonoAlphabetic(String key) { - this.key = key.toUpperCase(); // Store the key in uppercase to match the encryption/decryption logic + // Private constructor to prevent instantiation of utility class + private MonoAlphabetic() { + throw new UnsupportedOperationException("Utility class"); } // Encryption method - public String encrypt(String data) { - StringBuilder sb = new StringBuilder(data.toUpperCase()); - - for (int i = 0; i < sb.length(); i++) { - char currentChar = sb.charAt(i); - if (Character.isLetter(currentChar)) { // Check if it's a letter - int index = currentChar - 'A'; // Get the index for the character - sb.setCharAt(i, key.charAt(index)); // Replace with the key character + public static String encrypt(String data, String key) { + StringBuilder sb = new StringBuilder(); + + // Convert to uppercase to match the key mapping + data = data.toUpperCase(); + + for (char c : data.toCharArray()) { + if (c >= 'A' && c <= 'Z') { + // Get the index (0-25) for the character + int idx = c - 'A'; + // Append the character at the corresponding index in the key + sb.append(key.charAt(idx)); + } else { + // If character is not A-Z, append it as is + sb.append(c); } } return sb.toString(); } // Decryption method - public String decrypt(String data) { - StringBuilder sb = new StringBuilder(data.toUpperCase()); - - for (int i = 0; i < sb.length(); i++) { - char currentChar = sb.charAt(i); - if (Character.isLetter(currentChar)) { // Check if it's a letter - int index = key.indexOf(currentChar); // Find the character in the key - if (index != -1) { - sb.setCharAt(i, (char) (index + 'A')); // Replace with the original character + public static String decrypt(String data, String key) { + StringBuilder sb = new StringBuilder(); + + // Convert to uppercase to match the key mapping + data = data.toUpperCase(); + + for (char c : data.toCharArray()) { + if (c >= 'A' && c <= 'Z') { + // Get the index from the key for the character + int idx = getIndex(c, key); + // Append the original character + if (idx != -1) { + char originalChar = (char) (idx + 'A'); + sb.append(originalChar); } + } else { + // If character is not A-Z, append it as is + sb.append(c); } } return sb.toString(); } - // Static utility methods for encryption/decryption without creating an instance - public static String encrypt(String data, String key) { - return new MonoAlphabetic(key).encrypt(data); - } - - public static String decrypt(String data, String key) { - return new MonoAlphabetic(key).decrypt(data); + // Helper method to get index of a character in the key + private static int getIndex(char c, String key) { + for (int i = 0; i < key.length(); i++) { + if (key.charAt(i) == c) { + return i; // Return the index if the character matches + } + } + return -1; // Return -1 if character not found (should not happen for valid inputs) } } From 6efedf6d1502ffee56e1c99fb98a7f7e3e3a02b4 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:05:07 +0530 Subject: [PATCH 45/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index a56997916344..e9cf7fca1a03 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -10,19 +10,14 @@ private MonoAlphabetic() { // Encryption method public static String encrypt(String data, String key) { StringBuilder sb = new StringBuilder(); - - // Convert to uppercase to match the key mapping data = data.toUpperCase(); for (char c : data.toCharArray()) { if (c >= 'A' && c <= 'Z') { - // Get the index (0-25) for the character - int idx = c - 'A'; - // Append the character at the corresponding index in the key - sb.append(key.charAt(idx)); + int idx = c - 'A'; // Index in alphabet + sb.append(key.charAt(idx)); // Append the character from the key } else { - // If character is not A-Z, append it as is - sb.append(c); + sb.append(c); // Append non-alphabet characters directly } } return sb.toString(); @@ -31,34 +26,19 @@ public static String encrypt(String data, String key) { // Decryption method public static String decrypt(String data, String key) { StringBuilder sb = new StringBuilder(); - - // Convert to uppercase to match the key mapping data = data.toUpperCase(); for (char c : data.toCharArray()) { if (c >= 'A' && c <= 'Z') { - // Get the index from the key for the character - int idx = getIndex(c, key); - // Append the original character + int idx = key.indexOf(c); // Get the index in the key if (idx != -1) { - char originalChar = (char) (idx + 'A'); + char originalChar = (char) (idx + 'A'); // Convert index back to character sb.append(originalChar); } } else { - // If character is not A-Z, append it as is - sb.append(c); + sb.append(c); // Append non-alphabet characters directly } } return sb.toString(); } - - // Helper method to get index of a character in the key - private static int getIndex(char c, String key) { - for (int i = 0; i < key.length(); i++) { - if (key.charAt(i) == c) { - return i; // Return the index if the character matches - } - } - return -1; // Return -1 if character not found (should not happen for valid inputs) - } } From f9e0a1686b57cd870cc1cabdb75ec9bfdd7a70fd Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:06:55 +0530 Subject: [PATCH 46/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 3302f7dc2f8b..8e2a0ba14cf9 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -27,11 +27,11 @@ public void testDecrypt(String data, String key, String expected) { // Provide test data for encryption private static Stream provideEncryptionData() { - return Stream.of(Arguments.of("HELLO", key, "YQJJQ"), Arguments.of("JAVA", key, "HQLI")); + return Stream.of(Arguments.of("HELLO", key, "LCGGS"), Arguments.of("JAVA", key, "JMTM")); } // Provide test data for decryption private static Stream provideDecryptionData() { - return Stream.of(Arguments.of("YQJJQ", key, "HELLO"), Arguments.of("HQLI", key, "JAVA")); + return Stream.of(Arguments.of("LCGGS", key, "HELLO"), Arguments.of("JMTM", key, "JAVA")); } } From f5f27466365eda9fc36d5864ed6e8c681f4f215c Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:10:31 +0530 Subject: [PATCH 47/67] Update MonoAlphabeticTest.java --- .../thealgorithms/ciphers/MonoAlphabeticTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 8e2a0ba14cf9..16e08bcb6998 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -9,29 +9,29 @@ public class MonoAlphabeticTest { - private static final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; + private static final String KEY = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; // Test for encryption @ParameterizedTest @MethodSource("provideEncryptionData") - public void testEncrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.encrypt(data, key)); + public void testEncrypt(String data, String KEY, String expected) { + assertEquals(expected, MonoAlphabetic.encrypt(data, KEY)); } // Test for decryption @ParameterizedTest @MethodSource("provideDecryptionData") - public void testDecrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.decrypt(data, key)); + public void testDecrypt(String data, String KEY, String expected) { + assertEquals(expected, MonoAlphabetic.decrypt(data, KEY)); } // Provide test data for encryption private static Stream provideEncryptionData() { - return Stream.of(Arguments.of("HELLO", key, "LCGGS"), Arguments.of("JAVA", key, "JMTM")); + return Stream.of(Arguments.of("HELLO", KEY, "LCGGS"), Arguments.of("JAVA", KEY, "JMTM")); } // Provide test data for decryption private static Stream provideDecryptionData() { - return Stream.of(Arguments.of("LCGGS", key, "HELLO"), Arguments.of("JMTM", key, "JAVA")); + return Stream.of(Arguments.of("LCGGS", KEY, "HELLO"), Arguments.of("JMTM", KEY, "JAVA")); } } From d4e7bb67b2bc41ea966c16375bdb92cf85baa7a3 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:13:30 +0530 Subject: [PATCH 48/67] Update MonoAlphabeticTest.java --- .../com/thealgorithms/ciphers/MonoAlphabeticTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 16e08bcb6998..48fd588130e7 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -14,15 +14,15 @@ public class MonoAlphabeticTest { // Test for encryption @ParameterizedTest @MethodSource("provideEncryptionData") - public void testEncrypt(String data, String KEY, String expected) { - assertEquals(expected, MonoAlphabetic.encrypt(data, KEY)); + public void testEncrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.encrypt(data, key)); } // Test for decryption @ParameterizedTest @MethodSource("provideDecryptionData") - public void testDecrypt(String data, String KEY, String expected) { - assertEquals(expected, MonoAlphabetic.decrypt(data, KEY)); + public void testDecrypt(String data, String key, String expected) { + assertEquals(expected, MonoAlphabetic.decrypt(data, key)); } // Provide test data for encryption From 6e55c9c9600914467645fa5d423a7517acce9662 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:17:19 +0530 Subject: [PATCH 49/67] Update DiffieHellman.java --- .../thealgorithms/ciphers/DiffieHellman.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java index 5d00954a5317..7470b40e001a 100644 --- a/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java +++ b/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java @@ -33,21 +33,4 @@ public BigInteger calculateSharedSecret(BigInteger otherPublicValue) { // Returns b^x mod p or a^y mod p return otherPublicValue.modPow(secret, prime); } - - // Static utility methods for direct calculation (if needed) - public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) { - // Check for valid inputs - if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { - throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values."); - } - return base.modPow(secret, prime); - } - - public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) { - // Check for valid inputs - if (otherPublicValue == null || secret == null || prime == null || otherPublicValue.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { - throw new IllegalArgumentException("Other public value, secret, and prime must be non-null and positive values."); - } - return otherPublicValue.modPow(secret, prime); - } } From 6543b6cd6a5c76adafa62088dca9af053af15264 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:20:32 +0530 Subject: [PATCH 50/67] Update DiffieHellmanTest.java --- .../thealgorithms/ciphers/DiffieHellmanTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index b2f056088cf7..18694122bfa8 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -10,18 +10,20 @@ public class DiffieHellmanTest { - // Test for public value calculation + // Test for public value calculation using instance methods @ParameterizedTest @MethodSource("providePublicKeyData") public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { - assertEquals(expected, DiffieHellman.calculatePublicValue(base, secret, prime)); + DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman + assertEquals(expected, dh.calculatePublicValue()); // Call instance method } - // Test for shared secret calculation + // Test for shared secret calculation using instance methods @ParameterizedTest @MethodSource("provideSharedSecretData") - public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime, BigInteger expected) { - assertEquals(expected, DiffieHellman.calculateSharedSecret(otherPublicValue, secret, prime)); + public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInteger prime, BigInteger otherPublicValue, BigInteger expected) { + DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman + assertEquals(expected, dh.calculateSharedSecret(otherPublicValue)); // Call instance method } // Provide test data for public key calculation @@ -31,6 +33,6 @@ private static Stream providePublicKeyData() { // Provide test data for shared secret calculation private static Stream provideSharedSecretData() { - return Stream.of(Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("13")), Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("2"))); + return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8"), new BigInteger("13")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"), new BigInteger("2"))); } } From c54c74af3a5e5fbaf46f3ad4da39522857ac56ab Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:25:02 +0530 Subject: [PATCH 51/67] Update DiffieHellmanTest.java --- .../java/com/thealgorithms/ciphers/DiffieHellmanTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 18694122bfa8..8c80f90100f5 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -28,11 +28,11 @@ public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInt // Provide test data for public key calculation private static Stream providePublicKeyData() { - return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"))); + return Stream.of(Arguments.of(BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(23), BigInteger.valueOf(8)), Arguments.of(BigInteger.valueOf(2), BigInteger.valueOf(5), BigInteger.valueOf(13), BigInteger.valueOf(6))); } // Provide test data for shared secret calculation private static Stream provideSharedSecretData() { - return Stream.of(Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8"), new BigInteger("13")), Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6"), new BigInteger("2"))); + return Stream.of(Arguments.of(BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(23), BigInteger.valueOf(8), BigInteger.valueOf(13)), Arguments.of(BigInteger.valueOf(2), BigInteger.valueOf(5), BigInteger.valueOf(13), BigInteger.valueOf(6), BigInteger.valueOf(2))); } } From a8f58f6bf15eeb7e89f9c1e2eb888edfa5cdfb2a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:43:40 +0530 Subject: [PATCH 52/67] Update MonoAlphabeticTest.java --- .../ciphers/MonoAlphabeticTest.java | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 48fd588130e7..cc6c92e1875b 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -9,29 +9,20 @@ public class MonoAlphabeticTest { - private static final String KEY = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; - - // Test for encryption + // Test for both encryption and decryption with different keys @ParameterizedTest - @MethodSource("provideEncryptionData") - public void testEncrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.encrypt(data, key)); - } - - // Test for decryption - @ParameterizedTest - @MethodSource("provideDecryptionData") - public void testDecrypt(String data, String key, String expected) { - assertEquals(expected, MonoAlphabetic.decrypt(data, key)); - } + @MethodSource("provideTestData") + public void testEncryptDecrypt(String plainText, String key, String encryptedText) { + // Test encryption + assertEquals(encryptedText, MonoAlphabetic.encrypt(plainText, key)); - // Provide test data for encryption - private static Stream provideEncryptionData() { - return Stream.of(Arguments.of("HELLO", KEY, "LCGGS"), Arguments.of("JAVA", KEY, "JMTM")); + // Test decryption + assertEquals(plainText, MonoAlphabetic.decrypt(encryptedText, key)); } - // Provide test data for decryption - private static Stream provideDecryptionData() { - return Stream.of(Arguments.of("LCGGS", KEY, "HELLO"), Arguments.of("JMTM", KEY, "JAVA")); + // Provide test data for both encryption and decryption + private static Stream provideTestData() { + return Stream.of( + Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "UJJYU"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "KZHS")); } } From abfe77e952a1274b20bfb095f0979463a58add37 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:49:25 +0530 Subject: [PATCH 53/67] Update src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index e9cf7fca1a03..8e830a6d835c 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -2,7 +2,6 @@ public final class MonoAlphabetic { - // Private constructor to prevent instantiation of utility class private MonoAlphabetic() { throw new UnsupportedOperationException("Utility class"); } From e36f87c8e49b5935aea688777243cd9fb0c3e53a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:51:57 +0530 Subject: [PATCH 54/67] added exception in MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 8e830a6d835c..0f6523cee5cf 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -8,6 +8,9 @@ private MonoAlphabetic() { // Encryption method public static String encrypt(String data, String key) { + if (!data.matches("[A-Z]+")) { + throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed."); + } StringBuilder sb = new StringBuilder(); data = data.toUpperCase(); From d1a9080d80f8a0cf1695ecb870975db5e015a795 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:56:47 +0530 Subject: [PATCH 55/67] added methods charToPos and posToChar in MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 0f6523cee5cf..97ec3c01d6bb 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -32,15 +32,20 @@ public static String decrypt(String data, String key) { for (char c : data.toCharArray()) { if (c >= 'A' && c <= 'Z') { - int idx = key.indexOf(c); // Get the index in the key - if (idx != -1) { - char originalChar = (char) (idx + 'A'); // Convert index back to character - sb.append(originalChar); - } - } else { - sb.append(c); // Append non-alphabet characters directly + int idx = charToPos(c); // Get the index in the key + char encryptedChar = key.charAt(idx); + sb.append(encryptedChar); } - } return sb.toString(); } + + // Helper method: Convert a character to its position in the alphabet + private static int charToPos(char c) { + return c - 'A'; // Subtract 'A' to get position (0 for A, 1 for B, etc.) + } + + // Helper method: Convert a position in the alphabet to a character + private static char posToChar(int pos) { + return (char) (pos + 'A'); // Add 'A' to convert position back to character + } } From 40274c204b2206672d6c58bfc426c476b6c758f6 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:03:26 +0530 Subject: [PATCH 56/67] Update DiffieHellmanTest.java --- .../ciphers/DiffieHellmanTest.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 8c80f90100f5..7bebd4a471d4 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -12,27 +12,29 @@ public class DiffieHellmanTest { // Test for public value calculation using instance methods @ParameterizedTest - @MethodSource("providePublicKeyData") - public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { + @MethodSource("provideTestData") + public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) { DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman - assertEquals(expected, dh.calculatePublicValue()); // Call instance method + assertEquals(publicExpected, dh.calculatePublicValue()); // Call instance method } // Test for shared secret calculation using instance methods @ParameterizedTest - @MethodSource("provideSharedSecretData") - public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInteger prime, BigInteger otherPublicValue, BigInteger expected) { + @MethodSource("provideTestData") + public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) { DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman - assertEquals(expected, dh.calculateSharedSecret(otherPublicValue)); // Call instance method + assertEquals(sharedExpected, dh.calculateSharedSecret(publicExpected)); // Call instance method } - // Provide test data for public key calculation - private static Stream providePublicKeyData() { - return Stream.of(Arguments.of(BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(23), BigInteger.valueOf(8)), Arguments.of(BigInteger.valueOf(2), BigInteger.valueOf(5), BigInteger.valueOf(13), BigInteger.valueOf(6))); + // Provide test data for both public key and shared secret calculation + private static Stream provideTestData() { + return Stream.of( + createTestArgs(5, 6, 23, 8, 13), createTestArgs(2, 5, 13, 6, 2)); } - // Provide test data for shared secret calculation - private static Stream provideSharedSecretData() { - return Stream.of(Arguments.of(BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(23), BigInteger.valueOf(8), BigInteger.valueOf(13)), Arguments.of(BigInteger.valueOf(2), BigInteger.valueOf(5), BigInteger.valueOf(13), BigInteger.valueOf(6), BigInteger.valueOf(2))); + // Helper method for arguments + private static Arguments createTestArgs(long base, long secret, long prime, long publicExpected, long sharedExpected) { + return Arguments.of( + BigInteger.valueOf(base), BigInteger.valueOf(secret), BigInteger.valueOf(prime), BigInteger.valueOf(publicExpected), BigInteger.valueOf(sharedExpected)); } } From a4831b6af190464c721104abe97aaaaa9866f52a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:08:33 +0530 Subject: [PATCH 57/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 97ec3c01d6bb..24173d0f2dd7 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -36,6 +36,7 @@ public static String decrypt(String data, String key) { char encryptedChar = key.charAt(idx); sb.append(encryptedChar); } + } return sb.toString(); } From 5b5d9ef22376e3e5279d70a04aa8c9a0a6c0bfa5 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:11:16 +0530 Subject: [PATCH 58/67] Update DiffieHellmanTest.java --- .../java/com/thealgorithms/ciphers/DiffieHellmanTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java index 7bebd4a471d4..6255ad22ab56 100644 --- a/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java +++ b/src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java @@ -28,13 +28,11 @@ public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInt // Provide test data for both public key and shared secret calculation private static Stream provideTestData() { - return Stream.of( - createTestArgs(5, 6, 23, 8, 13), createTestArgs(2, 5, 13, 6, 2)); + return Stream.of(createTestArgs(5, 6, 23, 8, 13), createTestArgs(2, 5, 13, 6, 2)); } // Helper method for arguments private static Arguments createTestArgs(long base, long secret, long prime, long publicExpected, long sharedExpected) { - return Arguments.of( - BigInteger.valueOf(base), BigInteger.valueOf(secret), BigInteger.valueOf(prime), BigInteger.valueOf(publicExpected), BigInteger.valueOf(sharedExpected)); + return Arguments.of(BigInteger.valueOf(base), BigInteger.valueOf(secret), BigInteger.valueOf(prime), BigInteger.valueOf(publicExpected), BigInteger.valueOf(sharedExpected)); } } From b94f1f97359179f6a9604f4b055bec6b27f110f6 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:12:19 +0530 Subject: [PATCH 59/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index cc6c92e1875b..3970b6d5d9a0 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -22,7 +22,6 @@ public void testEncryptDecrypt(String plainText, String key, String encryptedTex // Provide test data for both encryption and decryption private static Stream provideTestData() { - return Stream.of( - Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "UJJYU"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "KZHS")); + return Stream.of(Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "UJJYU"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "KZHS")); } } From 82ecc3218d5bff659cd7fcc6e69dff367c9482c9 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:14:41 +0530 Subject: [PATCH 60/67] Update MonoAlphabeticTest.java --- .../java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index 3970b6d5d9a0..b5225fd375f4 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -14,10 +14,12 @@ public class MonoAlphabeticTest { @MethodSource("provideTestData") public void testEncryptDecrypt(String plainText, String key, String encryptedText) { // Test encryption - assertEquals(encryptedText, MonoAlphabetic.encrypt(plainText, key)); + String actualEncrypted = MonoAlphabetic.encrypt(plainText, key); + assertEquals(encryptedText, actualEncrypted, "Encryption failed for input: " + plainText + " with key: " + key); // Test decryption - assertEquals(plainText, MonoAlphabetic.decrypt(encryptedText, key)); + String actualDecrypted = MonoAlphabetic.decrypt(encryptedText, key); + assertEquals(plainText, actualDecrypted, "Decryption failed for input: " + encryptedText + " with key: " + key); } // Provide test data for both encryption and decryption From 8243b5af111b41a0be7d235295f6b87dde6fd13a Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:18:13 +0530 Subject: [PATCH 61/67] Update MonoAlphabetic.java --- .../com/thealgorithms/ciphers/MonoAlphabetic.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 24173d0f2dd7..41193b2df8c0 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -18,8 +18,6 @@ public static String encrypt(String data, String key) { if (c >= 'A' && c <= 'Z') { int idx = c - 'A'; // Index in alphabet sb.append(key.charAt(idx)); // Append the character from the key - } else { - sb.append(c); // Append non-alphabet characters directly } } return sb.toString(); @@ -32,9 +30,13 @@ public static String decrypt(String data, String key) { for (char c : data.toCharArray()) { if (c >= 'A' && c <= 'Z') { - int idx = charToPos(c); // Get the index in the key - char encryptedChar = key.charAt(idx); - sb.append(encryptedChar); + // Get the index in the key using charToPos + int idx = charToPos(c); + // Find the original character using the index + char originalChar = posToChar(key.indexOf(c)); + sb.append(originalChar); + } else { + sb.append(c); // Append non-alphabet characters directly } } return sb.toString(); From cad2f32d6039ec309fdbe3ca6e228470260f2b75 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:20:02 +0530 Subject: [PATCH 62/67] Update MonoAlphabetic.java --- src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 41193b2df8c0..e4993e3f2d4d 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -30,10 +30,8 @@ public static String decrypt(String data, String key) { for (char c : data.toCharArray()) { if (c >= 'A' && c <= 'Z') { - // Get the index in the key using charToPos - int idx = charToPos(c); - // Find the original character using the index - char originalChar = posToChar(key.indexOf(c)); + int idx = charToPos(c); + char originalChar = posToChar(key.indexOf(c)); sb.append(originalChar); } else { sb.append(c); // Append non-alphabet characters directly From 8ffc13b5eb0c95e9073fbe02a813bd4f8b74619e Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:25:26 +0530 Subject: [PATCH 63/67] Update MonoAlphabetic.java --- .../thealgorithms/ciphers/MonoAlphabetic.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index e4993e3f2d4d..28c6a0c51856 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -8,17 +8,19 @@ private MonoAlphabetic() { // Encryption method public static String encrypt(String data, String key) { + // Validate that the input data contains only uppercase letters if (!data.matches("[A-Z]+")) { throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed."); } StringBuilder sb = new StringBuilder(); + + // Convert input data to uppercase data = data.toUpperCase(); + // Encrypt each character for (char c : data.toCharArray()) { - if (c >= 'A' && c <= 'Z') { - int idx = c - 'A'; // Index in alphabet - sb.append(key.charAt(idx)); // Append the character from the key - } + int idx = charToPos(c); // Get the index in the alphabet + sb.append(key.charAt(idx)); // Append the character from the key } return sb.toString(); } @@ -26,16 +28,15 @@ public static String encrypt(String data, String key) { // Decryption method public static String decrypt(String data, String key) { StringBuilder sb = new StringBuilder(); + + // Convert input data to uppercase data = data.toUpperCase(); + // Decrypt each character for (char c : data.toCharArray()) { - if (c >= 'A' && c <= 'Z') { - int idx = charToPos(c); - char originalChar = posToChar(key.indexOf(c)); - sb.append(originalChar); - } else { - sb.append(c); // Append non-alphabet characters directly - } + int idx = charToPos(c); // Get the index in the key + char decryptedChar = posToChar(key.indexOf(c)); // Find the position in the key and convert back to char + sb.append(decryptedChar); // Append the decrypted character } return sb.toString(); } From 736ec03e65cb252ae72d916b889e2df7befc7e39 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:27:39 +0530 Subject: [PATCH 64/67] Update MonoAlphabetic.java --- .../com/thealgorithms/ciphers/MonoAlphabetic.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 28c6a0c51856..8b9b1c8b5992 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -14,9 +14,6 @@ public static String encrypt(String data, String key) { } StringBuilder sb = new StringBuilder(); - // Convert input data to uppercase - data = data.toUpperCase(); - // Encrypt each character for (char c : data.toCharArray()) { int idx = charToPos(c); // Get the index in the alphabet @@ -29,14 +26,13 @@ public static String encrypt(String data, String key) { public static String decrypt(String data, String key) { StringBuilder sb = new StringBuilder(); - // Convert input data to uppercase - data = data.toUpperCase(); - // Decrypt each character for (char c : data.toCharArray()) { - int idx = charToPos(c); // Get the index in the key - char decryptedChar = posToChar(key.indexOf(c)); // Find the position in the key and convert back to char - sb.append(decryptedChar); // Append the decrypted character + int idx = key.indexOf(c); // Find the index in the key + if (idx == -1) { + throw new IllegalArgumentException("Input data contains invalid characters."); + } + sb.append(posToChar(idx)); // Convert the index back to the original character } return sb.toString(); } From 897d359bf89229526f7563463c140afc9ca4f3a7 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:35:34 +0530 Subject: [PATCH 65/67] Update MonoAlphabetic.java From 2c8fb5d7d766947e4c16ce146aabd576a55592a0 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:45:10 +0530 Subject: [PATCH 66/67] Update MonoAlphabetic.java --- .../java/com/thealgorithms/ciphers/MonoAlphabetic.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java index 8b9b1c8b5992..1d5b7110a6f3 100644 --- a/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java +++ b/src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java @@ -8,7 +8,6 @@ private MonoAlphabetic() { // Encryption method public static String encrypt(String data, String key) { - // Validate that the input data contains only uppercase letters if (!data.matches("[A-Z]+")) { throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed."); } @@ -16,8 +15,8 @@ public static String encrypt(String data, String key) { // Encrypt each character for (char c : data.toCharArray()) { - int idx = charToPos(c); // Get the index in the alphabet - sb.append(key.charAt(idx)); // Append the character from the key + int idx = charToPos(c); // Get the index of the character + sb.append(key.charAt(idx)); // Map to the corresponding character in the key } return sb.toString(); } @@ -28,7 +27,7 @@ public static String decrypt(String data, String key) { // Decrypt each character for (char c : data.toCharArray()) { - int idx = key.indexOf(c); // Find the index in the key + int idx = key.indexOf(c); // Find the index of the character in the key if (idx == -1) { throw new IllegalArgumentException("Input data contains invalid characters."); } From 3ecff03ce5ac8a8fbd134d4aff1c6a231bcfb2e5 Mon Sep 17 00:00:00 2001 From: Ritisha Pande <96540421+riti2601@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:48:21 +0530 Subject: [PATCH 67/67] Update MonoAlphabeticTest.java --- src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java index b5225fd375f4..b1a8c78c952e 100644 --- a/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java +++ b/src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java @@ -24,6 +24,6 @@ public void testEncryptDecrypt(String plainText, String key, String encryptedTex // Provide test data for both encryption and decryption private static Stream provideTestData() { - return Stream.of(Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "UJJYU"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "KZHS")); + return Stream.of(Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "ITKKG"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "PQCQ")); } }