Skip to content

Conversation

dheeraj1010
Copy link
Contributor

Add PermutationCipher Implementation

Description

This PR adds a new PermutationCipher class to the ciphers package, implementing a transposition cipher that encrypts text by rearranging characters according to a fixed permutation key.

Algorithm Overview

The Permutation Cipher is a classic transposition cipher that:

  1. Divides plaintext into blocks of key length
  2. Rearranges characters within each block according to key positions
  3. Pads incomplete blocks with 'X' characters
  4. Supports bidirectional encryption/decryption

Example: With key {3, 1, 2} and plaintext "HELLO":

  • Block 1: "HEL" → positions {3,1,2} → "LHE"
  • Block 2: "LOX" → positions {3,1,2} → "XLO"
  • Result: "LHEXLO"

Implementation Features

✅ Core Functionality

  • encrypt(String, int[]) - Encrypts plaintext with permutation key
  • decrypt(String, int[]) - Decrypts ciphertext using inverse permutation
  • Automatic padding for incomplete blocks using 'X' character
  • Text preprocessing (removes spaces, converts to uppercase)

✅ Robust Validation

  • Key validation: Ensures 1-based positions without duplicates
  • Range checking: Validates all positions are within key length
  • Error handling: Descriptive IllegalArgumentException messages
  • Null safety: Handles null inputs gracefully

✅ Comprehensive Testing

  • 20+ JUnit 5 test cases with full coverage
  • Round-trip testing (encrypt → decrypt → verify)
  • Edge cases: empty strings, single char keys, exact block sizes
  • Error scenarios: invalid keys, duplicates, out-of-range values
  • Real-world examples: secret messages, longer texts, reverse permutations

Files Added

  • src/main/java/com/thealgorithms/ciphers/PermutationCipher.java - Main implementation
  • src/test/java/com/thealgorithms/ciphers/PermutationCipherTest.java - Comprehensive test suite

Code Quality

  • JavaDoc documentation with algorithm explanation and examples
  • Follows project conventions matching existing cipher implementations
  • Clean, readable code with meaningful variable names
  • Comprehensive error handling with descriptive messages
  • Efficient implementation with O(n) time complexity

Testing Results

image

…e tests

- Implement PermutationCipher class for transposition encryption/decryption
- Add encrypt() and decrypt() methods with permutation key support
- Include robust key validation (1-based positions, no duplicates)
- Implement automatic padding for incomplete blocks using 'X' character
- Add comprehensive error handling with descriptive exceptions
- Create 20+ JUnit test cases covering encryption, decryption, edge cases
- Support various key sizes and text processing (spaces removal, case handling)
- Include detailed JavaDoc documentation with algorithm explanation

Algorithm Details:
- Divides plaintext into blocks based on key length
- Rearranges characters within each block according to permutation positions
- Supports round-trip encryption/decryption with inverse permutation
- Handles edge cases: empty strings, single character keys, padding

Tests include: basic functionality, different key sizes, error validation,
real-world examples, and edge case handling.
@codecov-commenter
Copy link

codecov-commenter commented Oct 7, 2025

Codecov Report

❌ Patch coverage is 94.54545% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.93%. Comparing base (74647e3) to head (246a449).

Files with missing lines Patch % Lines
...a/com/thealgorithms/ciphers/PermutationCipher.java 94.54% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6700      +/-   ##
============================================
+ Coverage     75.88%   75.93%   +0.04%     
- Complexity     5819     5845      +26     
============================================
  Files           706      707       +1     
  Lines         19910    19965      +55     
  Branches       3859     3871      +12     
============================================
+ Hits          15109    15160      +51     
- Misses         4218     4219       +1     
- Partials        583      586       +3     

☔ View full report in Codecov by Sentry.
📢 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.

@dheeraj1010 dheeraj1010 changed the title feat(ciphers): Add PermutationCipher implementation with comprehensiv… feat(ciphers): Add PermutationCipher implementation(enhance #6697) Oct 7, 2025
@dheeraj1010
Copy link
Contributor Author

Please review @DenizAltunkapan @yanglbme @alxkm

Copy link
Member

@alxkm alxkm left a comment

Choose a reason for hiding this comment

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

Looks good, thanks.

@alxkm alxkm merged commit c06dc29 into TheAlgorithms:master Oct 8, 2025
6 checks passed
@dheeraj1010
Copy link
Contributor Author

Thanks a lot @alxkm .

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