forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElGamalEncryption.java
More file actions
51 lines (43 loc) · 1.99 KB
/
ElGamalEncryption.java
File metadata and controls
51 lines (43 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.thealgorithms.ciphers;
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* Implementation of the ElGamal Encryption Algorithm.
*
* <p>ElGamal is an asymmetric key encryption algorithm based on
* the Diffie–Hellman key exchange. It uses randomization
* for security and is widely used in cryptographic systems.</p>
*
* <p>Reference: Menezes, van Oorschot, and Vanstone, "Handbook of Applied Cryptography"</p>
*/
public final class ElGamalEncryption {
private static final SecureRandom RANDOM = new SecureRandom();
private ElGamalEncryption() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Runs the ElGamal encryption and decryption demonstration.
*
* @param message the plaintext message to encrypt
* @param bitLength the bit length for prime generation
*/
@SuppressWarnings({"PMD.SystemPrintln", "PMD.DataflowAnomalyAnalysis"})
public static void runElGamal(final String message, final int bitLength) {
final BigInteger p = BigInteger.probablePrime(bitLength, RANDOM);
final BigInteger g = new BigInteger("2");
final BigInteger x = new BigInteger(bitLength - 2, RANDOM);
final BigInteger y = g.modPow(x, p);
final BigInteger k = new BigInteger(bitLength - 2, RANDOM);
final BigInteger a = g.modPow(k, p);
final BigInteger m = new BigInteger(message.getBytes());
final BigInteger b = (y.modPow(k, p).multiply(m)).mod(p);
final BigInteger aInverse = a.modPow(p.subtract(BigInteger.ONE).subtract(x), p);
final BigInteger decrypted = (b.multiply(aInverse)).mod(p);
System.out.println("Prime (p): " + p);
System.out.println("Generator (g): " + g);
System.out.println("Private Key (x): " + x);
System.out.println("Public Key (y): " + y);
System.out.println("Ciphertext: (" + a + ", " + b + ")");
System.out.println("Decrypted Message: " + new String(decrypted.toByteArray()));
}
}