-
Notifications
You must be signed in to change notification settings - Fork 20.9k
Feature/elgamal cipher #7100
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
harsha08-2k6
wants to merge
12
commits into
TheAlgorithms:master
from
harsha08-2k6:feature/elgamal-cipher
Closed
Feature/elgamal cipher #7100
harsha08-2k6
wants to merge
12
commits into
TheAlgorithms:master
from
harsha08-2k6:feature/elgamal-cipher
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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