Skip to content

Commit b48f2f4

Browse files
committed
Fix: Checkstyle compliance for ElGamal Encryption Algorithm
1 parent 138cb61 commit b48f2f4

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

clang-format

Whitespace-only changes.

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1-
package com.thealgorithms.cryptography;
1+
/*
2+
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
3+
* Author: Shewale41
4+
* This file is licensed under the MIT License.
5+
*/
6+
7+
package com.thealgorithms.ciphers;
28

39
import java.math.BigInteger;
410
import java.security.SecureRandom;
511

612
/**
713
* Implementation of the ElGamal Encryption Algorithm.
814
*
9-
* <p>This algorithm is based on the Diffie–Hellman key exchange and uses modular arithmetic
10-
* for secure public-key encryption and decryption.
15+
* <p>This algorithm is based on the Diffie–Hellman key exchange and provides secure
16+
* public-key encryption and decryption using modular arithmetic.
1117
*
1218
* <p>Reference:
13-
* https://www.geeksforgeeks.org/elgamal-encryption-algorithm/
19+
* https://en.wikipedia.org/wiki/ElGamal_encryption
1420
*/
15-
public class ElGamalEncryption {
21+
public final class ElGamalEncryption {
1622

17-
private static final SecureRandom random = new SecureRandom();
23+
private static final SecureRandom RANDOM = new SecureRandom();
24+
25+
/** Private constructor to prevent instantiation of utility class. */
26+
private ElGamalEncryption() {
27+
throw new UnsupportedOperationException("Utility class");
28+
}
1829

1930
/**
20-
* Encrypts and decrypts a given message using ElGamal encryption.
31+
* Demonstrates ElGamal encryption and decryption for a given message.
2132
*
22-
* @param message The message to encrypt.
23-
* @param bitLength The bit length of the prime modulus.
33+
* @param message the message to encrypt
34+
* @param bitLength the bit length for the prime number used
2435
*/
2536
public static void runElGamal(String message, int bitLength) {
26-
BigInteger p = BigInteger.probablePrime(bitLength, random); // large prime
37+
BigInteger p = BigInteger.probablePrime(bitLength, RANDOM); // prime modulus
2738
BigInteger g = new BigInteger("2"); // primitive root
28-
BigInteger x = new BigInteger(bitLength - 2, random); // private key
39+
BigInteger x = new BigInteger(bitLength - 2, RANDOM); // private key
2940
BigInteger y = g.modPow(x, p); // public key
3041

3142
// Encryption
32-
BigInteger k = new BigInteger(bitLength - 2, random);
43+
BigInteger k = new BigInteger(bitLength - 2, RANDOM);
3344
BigInteger a = g.modPow(k, p);
34-
BigInteger M = new BigInteger(message.getBytes());
35-
BigInteger b = (y.modPow(k, p).multiply(M)).mod(p);
45+
BigInteger m = new BigInteger(message.getBytes());
46+
BigInteger b = (y.modPow(k, p).multiply(m)).mod(p);
3647

3748
// Decryption
3849
BigInteger aInverse = a.modPow(p.subtract(BigInteger.ONE).subtract(x), p);

src/test/java/com/thealgorithms/ciphers/ElGamalEncryptionTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
package com.thealgorithms.cryptography;
1+
/*
2+
* TheAlgorithms (https://github.com/TheAlgorithms/Java)
3+
* Author: Shewale41
4+
* This file is licensed under the MIT License.
5+
*/
6+
7+
package com.thealgorithms.ciphers;
28

3-
import static org.junit.jupiter.api.Assertions.*;
9+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
410

511
import org.junit.jupiter.api.Test;
612

@@ -11,14 +17,12 @@ public class ElGamalEncryptionTest {
1117

1218
@Test
1319
void testEncryptionDecryption() {
14-
// This test ensures encryption-decryption consistency
1520
String message = "Hello";
16-
ElGamalEncryption.runElGamal(message, 32);
17-
assertTrue(true); // Basic run test - manual verification for output
21+
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal(message, 32));
1822
}
1923

2024
@Test
21-
void testDifferentBitLengths() {
25+
void testWithDifferentBitLengths() {
2226
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal("Test", 16));
2327
assertDoesNotThrow(() -> ElGamalEncryption.runElGamal("Secure", 64));
2428
}

0 commit comments

Comments
 (0)