Skip to content

fix: make ADFGVX decrypt handle partial last rows and repeated key letters - #7783

Open
Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-adfgvx-decrypt
Open

Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-adfgvx-decrypt

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Bug: ADFGVXCipher.decrypt corrupts 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:

  1. reverseColumnarTransposition assumed 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 with numRows characters misaligns the whole grid:
decrypt(encrypt("HELLO", "PRIVACY"), "PRIVACY")        // "S35LL"
decrypt(encrypt("ATTACKATDAWN", "LEMON"), "LEMON")     // "A8TA2KNTDAWA"
  1. The column order was computed with key.indexOf(keyChar), which maps repeated key letters to the same column. With key "BADCAB" even the round trips that survived cause 1 turned into ATnullACnullATnullAWnull - both because two columns alias and because invalid fraction pairs then made POLYBIUS_MAP.get(pair) return null, which StringBuilder rendered 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:

  • Compute each column's length from the ciphertext length (fullRows + 1 for the first length % keyLength columns) and fill exactly that many characters per column.
  • Order columns by (key letter, first occurrence) so repeated letters select their own column; encryption uses the same order, so ciphertext for distinct-letter keys is unchanged.
  • decrypt now throws IllegalArgumentException on 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.

  • 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

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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.10345% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.48%. Comparing base (9e216aa) to head (47a6656).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...n/java/com/thealgorithms/ciphers/ADFGVXCipher.java 93.10% 1 Missing and 1 partial ⚠️
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.
📢 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 25, 2026 00:35
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.

2 participants