Skip to content

Fix Unicode text handling in DES encryption - #15641

Open
mohammedanasnakhuda wants to merge 2 commits into
TheAlgorithms:masterfrom
mohammedanasnakhuda:fix-des-unicode
Open

mohammedanasnakhuda wants to merge 2 commits into
TheAlgorithms:masterfrom
mohammedanasnakhuda:fix-des-unicode

Conversation

@mohammedanasnakhuda

@mohammedanasnakhuda mohammedanasnakhuda commented Oct 25, 2026 •

Copy link
Copy Markdown

Describe your change

  • 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

Summary

Fixes #15634.

This PR fixes Unicode text corruption in DES ECB encryption and decryption by using UTF-8 encoding.

Previously, characters above U+00FF could be corrupted during encryption and decryption because Unicode code points were converted into variable-width bit sequences.

Changes Made

  • Convert input text to UTF-8 bytes before encryption.
  • Decode decrypted bytes using UTF-8.
  • Add eight regression tests covering ASCII, accented characters, the euro symbol, Japanese text, emoji, block boundaries, and embedded null characters.
  • Update the documentation for encryption and decryption.

Testing

  • 14 existing DES tests passed.
  • 8 new regression tests passed.
  • Ruff linting and formatting checks passed.
  • git diff --check passed.

The broader cipher test collection was blocked by a pre-existing syntax error in maths/greatest_common_divisor.py and a missing NumPy dependency in the local environment.

Compatibility Notes

The UTF-8 change may affect compatibility with ciphertext previously generated from non-ASCII text.

The existing zero-padding scheme does not preserve trailing null characters. This is outside the scope of the current fix.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 25, 2026

This branch has not been deployed

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

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ciphers/des_ecb.py: encrypt/decrypt silently corrupts any character above U+00FF (round-trip fails)

1 participant