Repository navigation
fix: normalize Playfair key before building the matrix - #7779
Merged
alxkm merged 3 commits intoNov 24, 2026
Merged
Conversation
encrypt() normalizes the plaintext (uppercase, J folded into I, non-letters stripped) but the constructor passed the raw key straight into the matrix builder. A lowercase key like "monarchy" or one containing J or punctuation produced a wrong matrix, so ciphertext was garbage and Z could never round-trip. Normalize the key the same way before building the matrix.
…letters Regression tests for the key-normalization fix: a lowercase key must behave like its uppercase form, a key containing J must still cover Z in the matrix, and punctuation or digits in the key must be ignored.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7779 +/- ##
=========================================
Coverage 82.44% 82.44%
- Complexity 8509 8510 +1
=========================================
Files 847 847
Lines 26291 26291
Branches 5134 5134
=========================================
Hits 21675 21675
Misses 3825 3825
Partials 791 791 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Aditya2375
marked this pull request as ready for review
November 24, 2026 10:36
Aditya2375
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 24, 2026 10:36
alxkm
approved these changes
Nov 24, 2026
alxkm
left a comment
Member
There was a problem hiding this comment.
Looks good. Thank you for the contribution.
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
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.
Bug:
PlayfairCiphernormalizes the plaintext insideencrypt()(uppercase, J folded into I, non-letters stripped) but passes the raw key straight into the matrix builder. Keys that don't happen to be pre-normalized silently produce a wrong matrix.Examples against the current code:
"monarchy"(lowercase, as in every textbook) encryptsINSTRUMENTStoHOooNrKGmcooinstead ofGATLMZCLRQXA.J(e.g."JUMP") dropsZfrom the matrix entirely, sodecrypt(encrypt("ZOO"))returnsJOXO."Mon@rchy2026") leak garbage characters into the matrix.Fix: normalize the key in the constructor the same way
encrypt()normalizes the plaintext:After the fix, the lowercase textbook key produces the textbook answer (
GATLMZCLRQXA), a J-key still coversZin round-trips, and messy keys behave like their cleaned form.Tests: three regression tests added to
PlayfairTest(lowercase key equals uppercase, J-key covers Z, non-letters stripped). All 6 tests in the class pass; the 3 pre-existing tests are unaffected. Verified locally with JUnit 5 on JDK 21.clang-format -i --style=file path/to/your/file.java