Repository navigation
fix: make ADFGVX decrypt handle partial last rows and repeated key letters - #7783
Open
Aditya2375 wants to merge 2 commits into
Open
Aditya2375 wants to merge 2 commits into
Aditya2375 wants to merge 2 commits into
Conversation
…tters reverseColumnarTransposition assumed every column had the same length, but the last grid row is partial, so decrypt garbled any message whose fractionated length was not a multiple of the key length (HELLO/PRIVACY came back as S35LL). Column order also used key.indexOf, so repeated key letters aliased one column and POLYBIUS_MAP misses leaked the literal text "null" into the output. Compute per-column lengths, order columns by (letter, first occurrence), and throw IllegalArgumentException on ciphertext that is not valid ADFGVX output.
…y letters Regression tests for the decrypt fix: a message whose fractionated length is not a multiple of the key length must round-trip, a key with repeated letters must round-trip, and invalid ciphertext must throw IllegalArgumentException instead of leaking "null".
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7783 +/- ##
============================================
+ Coverage 82.45% 82.48% +0.03%
- Complexity 8512 8524 +12
============================================
Files 847 847
Lines 26291 26303 +12
Branches 5134 5140 +6
============================================
+ Hits 21677 21695 +18
+ Misses 3825 3823 -2
+ Partials 789 785 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Aditya2375
marked this pull request as ready for review
November 25, 2026 00:35
Aditya2375
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 25, 2026 00:35
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:
ADFGVXCipher.decryptcorrupts almost every message whose fractionated length is not a multiple of the key length, and any message encrypted with a key containing repeated letters. Two root causes, both in the columnar transposition helpers:reverseColumnarTranspositionassumed every column of the transposition grid has the same number of characters. The last row of the encryption grid is only partially filled (encryption skips the padding when reading columns), so the columns past the cut point hold one fewer character. Filling every column withnumRowscharacters misaligns the whole grid:key.indexOf(keyChar), which maps repeated key letters to the same column. With key"BADCAB"even the round trips that survived cause 1 turned intoATnullACnullATnullAWnull- both because two columns alias and because invalid fraction pairs then madePOLYBIUS_MAP.get(pair)returnnull, whichStringBuilderrendered as the literal text "null".The existing tests only used messages whose length is an exact multiple of the key length (14 letters with key PRIVACY, 7 with key A), which is why this survived.
Fix:
fullRows + 1for the firstlength % keyLengthcolumns) and fill exactly that many characters per column.decryptnow throwsIllegalArgumentExceptionon ciphertext that is not valid ADFGVX output (odd length or unknown pair) instead of silently appending "null".Tests: three new tests in
ADFGVXCipherTest: partial-last-row round trip (HELLO/PRIVACY), repeated-letter key round trip (ATTACKATDAWN/BADCAB), and rejection of invalid ciphertext (decrypt("ZZ", "KEY")). All 7 tests in the class pass locally (JUnit 5, JDK 21); the three new tests fail on the unpatched code. The four pre-existing tests, including the exact-ciphertext encrypt test, are unaffected.clang-format -i --style=file path/to/your/file.java