Skip to content

Conversation

@harsha08-2k6
Copy link

This PR implements the ElGamal Encryption Algorithm as requested in issue #6934. ElGamal is a public-key cryptosystem based on the Discrete Logarithm Problem (DLP) that provides semantic security through randomization.
Fixes #6934
🎯 What Has Been Done

✅ Implemented ElGamal key generation with configurable bit length (512, 1024, 2048 bits)
✅ Implemented encryption method using random ephemeral key for semantic security
✅ Implemented decryption method using modular multiplicative inverse
✅ Added comprehensive documentation explaining the mathematical operations and security properties
✅ Created demonstration examples in main() method showing:

Integer encryption/decryption
Semantic security demonstration (same plaintext → different ciphertexts)
String encryption/decryption with utility methods

✅ Included proper error handling for invalid inputs
✅ Used SecureRandom for cryptographically secure random number generation

📂 File Details

Location: src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java
Package: com.thealgorithms.ciphers
Classes:

ElGamalCipher (main utility class)
KeyPair (inner class for key storage)
Ciphertext (inner class for ciphertext representation)

🔐 Algorithm Overview
Key Generation:

Generate large prime p
Find generator g of multiplicative group mod p
Choose random private key x ∈ [2, p-2]
Compute public key y = g^x mod p

Encryption (message m):

Choose random k ∈ [2, p-2]
Compute c1 = g^k mod p
Compute c2 = m * y^k mod p
Return ciphertext (c1, c2)

Decryption (ciphertext (c1, c2)):

Compute s = c1^x mod p
Compute s^-1 mod p (modular inverse)
Recover m = c2 * s^-1 mod p

🧪 Testing
The implementation includes test cases in the main() method:

Basic encryption/decryption of integer 12345
Semantic security demonstration - encrypting the same message twice produces different ciphertexts
String encryption - demonstrates conversion utilities for text encryption

…t backtracking to solve 9×9 Sudoku puzzles- Includes validation for rows, columns, and 3×3 subgrids- Provides clean, modular implementation with helper methods- Includes comprehensive unit tests for solvable and unsolvable cases- Time Complexity: O(9^(n²)) in worst case- Space Complexity: O(n²) for recursion stack
…t backtracking to solve 9×9 Sudoku puzzles- Includes validation for rows, columns, and 3×3 subgrids- Provides clean, modular implementation with helper methods- Includes comprehensive unit tests for solvable and unsolvable cases- Time Complexity: O(9^(n²)) in worst case- Space Complexity: O(n²) for recursion stack
…st backtracking to solve 9×9 Sudoku puzzles - Includes validation for rows, columns, and 3×3 subgrids - Provides clean, modular implementation with helper methods - Includes comprehensive unit tests for solvable and unsolvable cases - Time Complexity: O(9^(n²)) in worst case - Space Complexity: O(n²) for recursion stack
…eration with configurable bit length - Added encryption and decryption methods - Included semantic security demonstration - Added string conversion utilities - Comprehensive documentation and examples - Resolves #6934
…atting - Added proper newline at end of ElGamalCipher.java - Added proper newline at end of SudokuSolver.java - Removed unused parameters (PMD violations) - Removed useless main() methods - Fixed checkstyle and clang-format violations - All files follow Java naming conventions - Resolves #6934
@codecov-commenter
Copy link

codecov-commenter commented Nov 24, 2025

Codecov Report

❌ Patch coverage is 43.10345% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.39%. Comparing base (fa8ea8e) to head (1153425).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
.../java/com/thealgorithms/ciphers/ElGamalCipher.java 0.00% 31 Missing ⚠️
...a/com/thealgorithms/backtracking/SudokuSolver.java 92.59% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7100      +/-   ##
============================================
- Coverage     78.50%   78.39%   -0.11%     
+ Complexity     6752     6747       -5     
============================================
  Files           759      760       +1     
  Lines         22402    22428      +26     
  Branches       4400     4399       -1     
============================================
- Hits          17587    17583       -4     
- Misses         4109     4139      +30     
  Partials        706      706              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@harsha08-2k6 harsha08-2k6 closed this by deleting the head repository Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] ElGamal Encryption Algorithm

2 participants