|
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; |
2 | 8 |
|
3 | 9 | import java.math.BigInteger; |
4 | 10 | import java.security.SecureRandom; |
5 | 11 |
|
6 | 12 | /** |
7 | 13 | * Implementation of the ElGamal Encryption Algorithm. |
8 | 14 | * |
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. |
11 | 17 | * |
12 | 18 | * <p>Reference: |
13 | | - * https://www.geeksforgeeks.org/elgamal-encryption-algorithm/ |
| 19 | + * https://en.wikipedia.org/wiki/ElGamal_encryption |
14 | 20 | */ |
15 | | -public class ElGamalEncryption { |
| 21 | +public final class ElGamalEncryption { |
16 | 22 |
|
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 | + } |
18 | 29 |
|
19 | 30 | /** |
20 | | - * Encrypts and decrypts a given message using ElGamal encryption. |
| 31 | + * Demonstrates ElGamal encryption and decryption for a given message. |
21 | 32 | * |
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 |
24 | 35 | */ |
25 | 36 | 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 |
27 | 38 | 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 |
29 | 40 | BigInteger y = g.modPow(x, p); // public key |
30 | 41 |
|
31 | 42 | // Encryption |
32 | | - BigInteger k = new BigInteger(bitLength - 2, random); |
| 43 | + BigInteger k = new BigInteger(bitLength - 2, RANDOM); |
33 | 44 | 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); |
36 | 47 |
|
37 | 48 | // Decryption |
38 | 49 | BigInteger aInverse = a.modPow(p.subtract(BigInteger.ONE).subtract(x), p); |
|
0 commit comments