Skip to content

fix: normalize Playfair key before building the matrix - #7779

Merged
alxkm merged 3 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-playfair-key-normalization
Nov 24, 2026
Merged

alxkm merged 3 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-playfair-key-normalization

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Bug: PlayfairCipher normalizes the plaintext inside encrypt() (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:

  • The classic textbook key "monarchy" (lowercase, as in every textbook) encrypts INSTRUMENTS to HOooNrKGmcoo instead of GATLMZCLRQXA.
  • A key containing J (e.g. "JUMP") drops Z from the matrix entirely, so decrypt(encrypt("ZOO")) returns JOXO.
  • Keys with digits or punctuation (e.g. "Mon@rchy2026") leak garbage characters into the matrix.

Fix: normalize the key in the constructor the same way encrypt() normalizes the plaintext:

this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");

After the fix, the lowercase textbook key produces the textbook answer (GATLMZCLRQXA), a J-key still covers Z in 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.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

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-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (54d056d) to head (a5cfb03).

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.
📢 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.

@Aditya2375
Aditya2375 marked this pull request as ready for review November 24, 2026 10:36

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 9870de2 into TheAlgorithms:master Nov 24, 2026
7 checks passed
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.

3 participants