Skip to content

feat: add Gronsfeld cipher implementation#1045

Open
qsrbit wants to merge 1 commit into
TheAlgorithms:masterfrom
qsrbit:feature/gronsfeld_cipher
Open

feat: add Gronsfeld cipher implementation#1045
qsrbit wants to merge 1 commit into
TheAlgorithms:masterfrom
qsrbit:feature/gronsfeld_cipher

Conversation

@qsrbit

@qsrbit qsrbit commented Jun 12, 2026

Copy link
Copy Markdown

Description

This PR adds a Gronsfeld Cipher implementation to the ciphers module.

About Gronsfeld Cipher

The Gronsfeld cipher, a Vigenère variant where the key is a digit sequence (0-9). Includes 20 tests covering key validation, case preservation, alphabet wrap-around, and round-trip correctness.

Implementation Details

The implementation provides two public functions:

  1. gronsfeld_encrypt(text: &str, key: &str) -> Result<String, &'static str>

    • Encrypts plaintext by shifting each letter forward by the corresponding key digit
    • Validates the key: must be non-empty and contain only digits (0–9)
    • Preserves the case of alphabetic characters
    • Passes non-alphabetic characters (spaces, punctuation, Unicode) through unchanged
    • Cycles the key if it is shorter than the plaintext
  2. gronsfeld_decrypt(text: &str, key: &str) -> Result<String, &'static str>

    • Decrypts ciphertext by shifting each letter backward by the corresponding key digit
    • Same key validation and passthrough rules as encryption
    • Inverse of gronsfeld_encrypt: decrypt(encrypt(text, key), key) == text

Algorithm Details

  • Character Set: Works with all ASCII alphabetic characters (A–Z, a–z)
  • Key Format: Digit string, e.g. "31415" or "9876543210"
  • Key Cycling: The key repeats cyclically when shorter than the text; only alphabetic characters consume a key digit
  • Shift Range: Each digit shifts by 0–9 positions (a strict subset of Vigenère's 0–25)
  • Case Preservation: Uppercase input stays uppercase, lowercase stays lowercase
  • Time Complexity: O(n) where n is the length of the input text
  • Space Complexity: O(n) for storing the result

Type of change

  • New cipher implementation
  • Documentation (docstrings and examples)
  • Tests

Checklist:

All tests pass successfully:

cargo test gronsfeld
cargo fmt --check
cargo clippy -- -D warnings
  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines

Implements encrypt and decrypt functions for the Gronsfeld cipher,
a Vigenère variant where the key is a digit sequence (0-9). Includes
20 tests covering key validation, case preservation, alphabet wrap-around,
and round-trip correctness.
@qsrbit qsrbit requested a review from imp2002 as a code owner June 12, 2026 14:58
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.

1 participant