From 61db75aaa3afb6cb285cd00f1fa6b422c03806bf Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 12 Nov 2025 22:37:46 +0530 Subject: [PATCH 1/7] Add ElGamalCipher implementing ElGamal encryption and decryption algorithm --- .../thealgorithms/ciphers/ElGamalCipher.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java diff --git a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java new file mode 100644 index 000000000000..0f7ce48c4e2c --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java @@ -0,0 +1,47 @@ +package com.thealgorithms.ciphers; + +import java.math.BigInteger; +import java.security.SecureRandom; + +public class ElGamalCipher { + private BigInteger p, g, x, y; + private SecureRandom random = new SecureRandom(); + + // Key generation + public void generateKeys(int bitLength) { + p = BigInteger.probablePrime(bitLength, random); + g = new BigInteger(bitLength - 1, random).mod(p); + x = new BigInteger(bitLength - 2, random); // Private key + y = g.modPow(x, p); // Public key + } + + // Encryption: returns [c1, c2] + public BigInteger[] encrypt(BigInteger message) { + BigInteger k = new BigInteger(p.bitLength() - 1, random); + BigInteger c1 = g.modPow(k, p); + BigInteger s = y.modPow(k, p); + BigInteger c2 = s.multiply(message).mod(p); + return new BigInteger[]{c1, c2}; + } + + // Decryption: m = c2 * (c1^x)^-1 mod p + public BigInteger decrypt(BigInteger c1, BigInteger c2) { + BigInteger s = c1.modPow(x, p); + BigInteger sInv = s.modInverse(p); + return c2.multiply(sInv).mod(p); + } + + // Example usage + public static void main(String[] args) { + ElGamalCipher elgamal = new ElGamalCipher(); + elgamal.generateKeys(256); + + BigInteger message = new BigInteger("12345"); + BigInteger[] cipher = elgamal.encrypt(message); + BigInteger decrypted = elgamal.decrypt(cipher[0], cipher[1]); + + System.out.println("Original: " + message); + System.out.println("Encrypted: c1=" + cipher[0] + ", c2=" + cipher[1]); + System.out.println("Decrypted: " + decrypted); + } +} From 84c769d8c7ddc182c9b890a42289cbb57b064794 Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 12 Nov 2025 22:54:58 +0530 Subject: [PATCH 2/7] style: fix code format for ElGamalCipher --- src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java index 0f7ce48c4e2c..411a131d35d6 100644 --- a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java @@ -21,7 +21,7 @@ public BigInteger[] encrypt(BigInteger message) { BigInteger c1 = g.modPow(k, p); BigInteger s = y.modPow(k, p); BigInteger c2 = s.multiply(message).mod(p); - return new BigInteger[]{c1, c2}; + return new BigInteger[] {c1, c2}; } // Decryption: m = c2 * (c1^x)^-1 mod p From c5435881584d91c2aff1222302b908242a46f5f3 Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 12 Nov 2025 23:15:08 +0530 Subject: [PATCH 3/7] fix: separate BigInteger declarations to satisfy Checkstyle --- src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java index 411a131d35d6..1d7f15dd480c 100644 --- a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java @@ -4,7 +4,11 @@ import java.security.SecureRandom; public class ElGamalCipher { - private BigInteger p, g, x, y; + private BigInteger p; + private BigInteger g; + private BigInteger x; + private BigInteger y; + private SecureRandom random = new SecureRandom(); // Key generation From 2b3f8f1dba92f5c6e6916823473141ce1aceabe9 Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 12 Nov 2025 23:30:56 +0530 Subject: [PATCH 4/7] fix: remove redundant main method for PMD compliance --- .../com/thealgorithms/ciphers/ElGamalCipher.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java index 1d7f15dd480c..2ae579cd1466 100644 --- a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java @@ -34,18 +34,3 @@ public BigInteger decrypt(BigInteger c1, BigInteger c2) { BigInteger sInv = s.modInverse(p); return c2.multiply(sInv).mod(p); } - - // Example usage - public static void main(String[] args) { - ElGamalCipher elgamal = new ElGamalCipher(); - elgamal.generateKeys(256); - - BigInteger message = new BigInteger("12345"); - BigInteger[] cipher = elgamal.encrypt(message); - BigInteger decrypted = elgamal.decrypt(cipher[0], cipher[1]); - - System.out.println("Original: " + message); - System.out.println("Encrypted: c1=" + cipher[0] + ", c2=" + cipher[1]); - System.out.println("Decrypted: " + decrypted); - } -} From 90b8a9da08dad2904f7fcd1cfc4eb1edeef441dc Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 12 Nov 2025 23:38:20 +0530 Subject: [PATCH 5/7] fix: remove redundant main method to resolve PMD violation --- src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java index 2ae579cd1466..08ac245612c2 100644 --- a/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java @@ -34,3 +34,4 @@ public BigInteger decrypt(BigInteger c1, BigInteger c2) { BigInteger sInv = s.modInverse(p); return c2.multiply(sInv).mod(p); } +} From 95ce5e6b30cd133cbd2a12d05f441a20aabcd4db Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Sun, 23 Nov 2025 15:12:25 +0530 Subject: [PATCH 6/7] Add ElGamalCipher unit tests --- .../ciphers/ElGamalCipherTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java new file mode 100644 index 000000000000..3c1d3a4304b0 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java @@ -0,0 +1,41 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class ElGamalCipherTest { + + @Test + void testEncryptThenDecrypt_returnsOriginal() { + ElGamalCipher elg = new ElGamalCipher(); + + String original = "HelloElGamal123"; + String encrypted = elg.encrypt(original); + assertNotNull(encrypted); + + String decrypted = elg.decrypt(encrypted); + assertEquals(original, decrypted); + } + + @Test + void testEncryptedIsDifferentFromOriginal() { + ElGamalCipher elg = new ElGamalCipher(); + String encrypted = elg.encrypt("TestData"); + assertNotEquals("TestData", encrypted); + } + + @Test + void testEmptyString() { + ElGamalCipher elg = new ElGamalCipher(); + String encrypted = elg.encrypt(""); + String decrypted = elg.decrypt(encrypted); + assertEquals("", decrypted); + } + + @Test + void testNullInputs() { + ElGamalCipher elg = new ElGamalCipher(); + assertThrows(IllegalArgumentException.class, () -> elg.encrypt(null)); + assertThrows(IllegalArgumentException.class, () -> elg.decrypt(null)); + } +} From c71a44085386b71ccebc45dd00f27faf451798db Mon Sep 17 00:00:00 2001 From: polasisubash <2400031833@kluniversity.in> Date: Wed, 26 Nov 2025 15:49:52 +0530 Subject: [PATCH 7/7] Delete src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java --- .../ciphers/ElGamalCipherTest.java | 41 ------------------- 1 file changed, 41 deletions(-) delete mode 100644 src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java diff --git a/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java deleted file mode 100644 index 3c1d3a4304b0..000000000000 --- a/src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.thealgorithms.ciphers; - -import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; - -public class ElGamalCipherTest { - - @Test - void testEncryptThenDecrypt_returnsOriginal() { - ElGamalCipher elg = new ElGamalCipher(); - - String original = "HelloElGamal123"; - String encrypted = elg.encrypt(original); - assertNotNull(encrypted); - - String decrypted = elg.decrypt(encrypted); - assertEquals(original, decrypted); - } - - @Test - void testEncryptedIsDifferentFromOriginal() { - ElGamalCipher elg = new ElGamalCipher(); - String encrypted = elg.encrypt("TestData"); - assertNotEquals("TestData", encrypted); - } - - @Test - void testEmptyString() { - ElGamalCipher elg = new ElGamalCipher(); - String encrypted = elg.encrypt(""); - String decrypted = elg.decrypt(encrypted); - assertEquals("", decrypted); - } - - @Test - void testNullInputs() { - ElGamalCipher elg = new ElGamalCipher(); - assertThrows(IllegalArgumentException.class, () -> elg.encrypt(null)); - assertThrows(IllegalArgumentException.class, () -> elg.decrypt(null)); - } -}