Skip to content

Implement modified vigenere cipher for all printable ASCII - #13452

Open
Abheelash-Mishra wants to merge 5 commits into
TheAlgorithms:masterfrom
Abheelash-Mishra:add_printable_ascii_vigenere_cipher
Open

Abheelash-Mishra wants to merge 5 commits into
TheAlgorithms:masterfrom
Abheelash-Mishra:add_printable_ascii_vigenere_cipher

Conversation

@Abheelash-Mishra

Copy link
Copy Markdown

Describe your change:

Added a modifed Vigenere cipher script that encrypts and decrypts messages using only printable ASCII characters (32–126). This maintains the original Vigenere cipher behavior with cyclic key repetition, ensures all output remains readable.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added the require tests Tests [doctest/unittest/pytest] are required label Oct 12, 2025

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread ciphers/printable_ascii_vigenere_cipher.py Outdated
Comment thread ciphers/printable_ascii_vigenere_cipher.py
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed and removed require tests Tests [doctest/unittest/pytest] are required labels Oct 12, 2025
@Abheelash-Mishra
Abheelash-Mishra force-pushed the add_printable_ascii_vigenere_cipher branch from fa57345 to e371c10 Compare October 18, 2025 07:54
@cclauss

cclauss commented Sep 15, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review.
How is this better, faster, or more correct than our existing ciphers/vigenere_cipher.py?

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR awaiting triage Awaiting triage from a maintainer and removed awaiting reviews This PR is ready to be reviewed labels Sep 15, 2026
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Thanks for the ping, @cclauss. I read through it — my honest take:

On "better/faster/more correct" vs ciphers/vigenere_cipher.py: it isn't any of those for the classic cipher. It's the same O(n) translate loop, so no speed win, and for letters-only input the existing file is already correct. What this PR actually is, is a different variant: it operates over the full printable-ASCII range (32–126) instead of A–Z, so it also enciphers digits, punctuation and spaces, and the key is any printable string rather than an alphabetic one. That's a legitimately larger keyspace/message-space, but it's a new cipher, not an improvement to the old one — and the repo generally discourages near-duplicates unless the added value is clear. I'd ask @Abheelash-Mishra to make that case explicitly in the PR description (why the printable-ASCII variant is worth carrying alongside the classic one) so it's a deliberate keep rather than an accidental fork.

If it does stay, two concrete fixes:

  1. Empty key crashes instead of erroring cleanly:

    >>> encrypt_message('', 'hi')
    IndexError: list index out of range

    Guard it up front (if not key: raise ValueError("Key must be non-empty")) and add a doctest for the raise.

  2. mode is validated inside the per-character loop, so the ValueError only fires if the message has a printable char, and re-checks every iteration. Validate mode once before the loop.

Minor: the module-level PRINTABLE_* constants are nice; consider deriving key_bytes outside translate_message is already good. Doctests pass locally (6/6). Overall the code is clean and readable — the real question is scope/duplication, which is your call as maintainer.

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

Labels

awaiting changes A maintainer has requested changes to this PR awaiting triage Awaiting triage from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants