@@ -290,4 +290,35 @@ void testIdentityPermutation() {
290290 assertEquals ("IDENTITY" , encrypted ); // Should remain unchanged
291291 assertEquals ("IDENTITY" , decrypted );
292292 }
293- }
293+
294+ @ Test
295+ void testEmptyStringRemovePadding () {
296+ // given - Test to cover line 178 (empty string case in removePadding)
297+ String ciphertext = "" ;
298+ int [] key = {2 , 1 , 3 };
299+
300+ // when
301+ String decrypted = cipher .decrypt (ciphertext , key );
302+
303+ // then
304+ assertEquals ("" , decrypted ); // Should return empty string directly
305+ }
306+
307+ @ Test
308+ void testBlockShorterThanKey () {
309+ // given - Test to cover line 139 (block length != key length case)
310+ // This is a defensive case where permuteBlock might receive a block shorter than key
311+ // We can test this by manually creating a scenario with malformed ciphertext
312+ String malformedCiphertext = "AB" ; // Length 2, but key length is 3
313+ int [] key = {3 , 1 , 2 }; // Key length is 3
314+
315+ // when - This should trigger the padding logic in permuteBlock during decryption
316+ String decrypted = cipher .decrypt (malformedCiphertext , key );
317+
318+ // then - The method should handle the short block gracefully
319+ // "AB" gets padded to "ABX", then permuted with inverse key {2,3,1}
320+ // inverse key {2,3,1} means: pos 2→1st, pos 3→2nd, pos 1→3rd = "BXA"
321+ // Padding removal only removes trailing X's, so "BXA" remains as is
322+ assertEquals ("BXA" , decrypted );
323+ }
324+ }
0 commit comments