Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(ciphers): Add PermutationCipher implementation(enhance #6697)#6700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
alxkm merged 7 commits intoTheAlgorithms:masterfromdheeraj1010:master
Oct 8, 2025

Conversation

dheeraj1010
Copy link
Contributor

Add PermutationCipher Implementation

Description

This PR adds a newPermutationCipher 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: DescriptiveIllegalArgumentException 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 explanationAlgorithm 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, paddingTests include: basic functionality, different key sizes, error validation,real-world examples, and edge case handling.
@codecov-commenter
Copy link

codecov-commenter commentedOct 7, 2025
edited
Loading

Codecov Report

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

Files with missing linesPatch %Lines
...a/com/thealgorithms/ciphers/PermutationCipher.java94.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.

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

Please review@DenizAltunkapan@yanglbme@alxkm

Copy link
Member

@alxkmalxkm 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.

@alxkmalxkm merged commitc06dc29 intoTheAlgorithms:masterOct 8, 2025
6 checks passed
@dheeraj1010
Copy link
ContributorAuthor

Thanks a lot@alxkm .

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@alxkmalxkmalxkm approved these changes

@DenizAltunkapanDenizAltunkapanAwaiting requested review from DenizAltunkapanDenizAltunkapan is a code owner

@yanglbmeyanglbmeAwaiting requested review from yanglbmeyanglbme is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@dheeraj1010@codecov-commenter@alxkm

[8]ページ先頭

©2009-2025 Movatter.jp