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/5] 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/5] 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/5] 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/5] 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/5] 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); } +}