Skip to content

Commit f646e48

Browse files
committed
Fix: SpotBugs violation and improve test for ElGamalEncryption
1 parent fba5058 commit f646e48

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/main/java/com/thealgorithms/ciphers/ElGamalEncryption.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public final class ElGamalEncryption {
1616

1717
private static final SecureRandom RANDOM = new SecureRandom();
1818

19-
// Private constructor to prevent instantiation
2019
private ElGamalEncryption() {
2120
throw new UnsupportedOperationException("Utility class");
2221
}
@@ -27,25 +26,21 @@ private ElGamalEncryption() {
2726
* @param message the plaintext message to encrypt
2827
* @param bitLength the bit length for prime generation
2928
*/
30-
@SuppressWarnings({"PMD.SystemPrintln", "PMD.DataflowAnomalyAnalysis"})
29+
@SuppressWarnings({ "PMD.SystemPrintln", "PMD.DataflowAnomalyAnalysis" })
3130
public static void runElGamal(final String message, final int bitLength) {
32-
// Key generation
3331
final BigInteger p = BigInteger.probablePrime(bitLength, RANDOM);
3432
final BigInteger g = new BigInteger("2");
3533
final BigInteger x = new BigInteger(bitLength - 2, RANDOM);
3634
final BigInteger y = g.modPow(x, p);
3735

38-
// Encryption
3936
final BigInteger k = new BigInteger(bitLength - 2, RANDOM);
4037
final BigInteger a = g.modPow(k, p);
4138
final BigInteger m = new BigInteger(message.getBytes());
4239
final BigInteger b = (y.modPow(k, p).multiply(m)).mod(p);
4340

44-
// Decryption
4541
final BigInteger aInverse = a.modPow(p.subtract(BigInteger.ONE).subtract(x), p);
4642
final BigInteger decrypted = (b.multiply(aInverse)).mod(p);
4743

48-
// Display results
4944
System.out.println("Prime (p): " + p);
5045
System.out.println("Generator (g): " + g);
5146
System.out.println("Private Key (x): " + x);
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.thealgorithms.ciphers;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
53
import org.junit.jupiter.api.Test;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Modifier;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
67

78
/**
89
* Unit tests for {@link ElGamalEncryption}.
@@ -11,8 +12,6 @@ public class ElGamalEncryptionTest {
1112

1213
@Test
1314
void testEncryptionDecryption() {
14-
// Basic functional test (simulated output check)
15-
// Since ElGamal uses randomization, we only verify successful execution
1615
try {
1716
ElGamalEncryption.runElGamal("Hello", 64);
1817
} catch (Exception e) {
@@ -21,14 +20,9 @@ void testEncryptionDecryption() {
2120
}
2221

2322
@Test
24-
void testUtilityConstructor() {
25-
// Ensures the utility class constructor is private
26-
try {
27-
var constructor = ElGamalEncryption.class.getDeclaredConstructor();
28-
constructor.setAccessible(true);
29-
constructor.newInstance();
30-
} catch (Exception e) {
31-
assertEquals("Utility class", e.getCause().getMessage());
32-
}
23+
void testUtilityConstructor() throws NoSuchMethodException {
24+
Constructor<ElGamalEncryption> constructor = ElGamalEncryption.class.getDeclaredConstructor();
25+
assertTrue(Modifier.isPrivate(constructor.getModifiers()),
26+
"Utility class constructor should be private");
3327
}
3428
}

0 commit comments

Comments
 (0)