Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add ElGamalCipher implementing ElGamal encryption and decryption#7064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
polasisubash wants to merge12 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
frompolasisubash:add-elgamal-encryption
Open
Changes from1 commit
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
61db75a
Add ElGamalCipher implementing ElGamal encryption and decryption algo…
polasisubashNov 12, 2025
84c769d
style: fix code format for ElGamalCipher
polasisubashNov 12, 2025
c543588
fix: separate BigInteger declarations to satisfy Checkstyle
polasisubashNov 12, 2025
2b3f8f1
fix: remove redundant main method for PMD compliance
polasisubashNov 12, 2025
90b8a9d
fix: remove redundant main method to resolve PMD violation
polasisubashNov 12, 2025
99039c6
Merge branch 'master' into add-elgamal-encryption
polasisubashNov 15, 2025
a7792eb
Merge branch 'master' into add-elgamal-encryption
polasisubashNov 16, 2025
9f744c2
Merge branch 'master' into add-elgamal-encryption
polasisubashNov 22, 2025
95ce5e6
Add ElGamalCipher unit tests
polasisubashNov 23, 2025
9391734
Merge branch 'master' into add-elgamal-encryption
polasisubashNov 24, 2025
c71a440
Delete src/test/java/com/thealgorithms/ciphers/ElGamalCipherTest.java
polasisubashNov 26, 2025
9d421bc
Merge branch 'master' into add-elgamal-encryption
polasisubashNov 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Add ElGamalCipher implementing ElGamal encryption and decryption algo…
…rithm
  • Loading branch information
@polasisubash
polasisubash committedNov 12, 2025
commit61db75aaa3afb6cb285cd00f1fa6b422c03806bf
47 changes: 47 additions & 0 deletionssrc/main/java/com/thealgorithms/ciphers/ElGamalCipher.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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);
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp