How shrimp2845-tw/project_des works
1. Repository purpose
- This is a small, educational Python package that implements the DES block cipher from low-level bit operations up to a user-facing DES class.
- The architecture is intentionally modular so each step of the Feistel-based algorithm can be studied independently.
2. Public package boundary
- The main entry point is project_DES/project_DES.py, which exposes the DES class imported from project_DES.
- This is the boundary an application uses for string/byte encryption, decryption, file encryption, key changes, and mode changes.
3. Core cryptography layer
- project_DES/des_core/crypto.py is the central orchestration layer for DES encryption/decryption of a single block.
- It likely coordinates the full round structure, initial/final permutations, round key application, and the Feistel swap structure.
- This module sits above the lower-level helpers and below the public API.
4. Feistel round subsystem
- project_DES/des_core/feistel.py and project_DES/des_core/mangler.py represent the round-function side of DES.
- These modules encapsulate the “right half in, transformed output out” logic that drives each DES round.
- In architecture terms, they are the heart of the algorithm, with the rest of the code supporting key schedule and bit manipulation.
5. Key schedule subsystem
- project_DES/des_core/key.py handles key normalization and round-key generation.
- The README indicates keys can be provided as raw 8 bytes or as a file path whose first 8 bytes are used.
- This subsystem bridges user input and the per-round cryptographic core.
6. Permutation and bit-utility boundary
- project_DES/des_core/permute.py and project_DES/des_core/bit_utils.py provide the mechanical DES transformations: bit selection, rearrangement, splitting, shifting, and similar operations.
- These are foundational utilities used by both the round function and the key schedule.
- They form the lowest-level internal layer and do not appear to own application logic.
7. Mode of operation support
- The package supports at least ECB and CBC modes, as described in the README and examples.
- The mode layer is part of the public DES interface, so architecture-wise it sits between user data/file input and the block cipher core.
- ECB processes blocks independently; CBC introduces chaining between blocks, which affects both encryption and decryption flow.
8. Data flow for bytes/string encryption
- User data enters through DES.encrypt(data: bytes) and DES.decrypt(data: bytes).
- The input is split into DES-sized blocks, passed through the selected mode handler, and each block is processed by the DES core.
- The output is returned as bytes, making the library suitable for embedding into other Python code.
9. Data flow for file encryption
- encrypt_file(name, new_name) and decrypt_file(name, new_name) wrap the same byte-level pipeline around file I/O.
- The examples show this being used for .jpg images and .bin encrypted outputs.
- File handling is therefore an application-layer concern, while the cryptographic core remains block-oriented and reusable.
10. Examples as integration boundary
- examples/string_cipher.py demonstrates in-memory usage of the API.
- examples/file_cipher.py demonstrates file-based encryption/decryption and shows the difference between ECB and CBC in practice.
- The examples/ecb_sample and examples/cbc_sample directories are reference artifacts, not core logic.
11. Testing boundary
- test/des_core_test.py focuses on the internal DES core rather than only the public wrapper.
- This suggests the repo values verifying the correctness of bit-level and round-level transformations.
- For an architecture diagram, this test layer should be shown as validating the core modules and their composition.
12. Operational characteristics
- The README explicitly warns that the implementation is for learning, not real-world security, and notes it is very slow.
- That performance profile is consistent with a design optimized for readability and modularity over throughput.
- So the architecture should be drawn as a didactic layered system, not an optimized cryptography engine.
Main entry points: project_DES.py DES class · crypto.py Crypto core · key.py Key schedule · file_cipher.py File demo
Components 14
Public API
User-facing package boundary and entrypoint.
- DES class · User-facing encryption, decryption, mode, key, and file interface.
project_DES/project_DES.py - Package init · Exports the public DES entry point.
project_DES/__init__.py
DES Core
Block cipher orchestration, rounds, keys, and bit-level transforms.
- Crypto core · Coordinates single-block DES processing and overall round structure.
project_DES/des_core/crypto.py - Feistel · Drives the DES round flow around the left and right halves.
project_DES/des_core/feistel.py - Mangler · Implements the DES F-function style transformation used inside rounds.
project_DES/des_core/mangler.py - Key schedule · Normalizes key input and derives per-round keys.
project_DES/des_core/key.py - Permutations · Performs DES bit rearrangement, expansion, and permutation steps.
project_DES/des_core/permute.py - Bit utils · Low-level bit splitting, shifting, and packing helpers.
project_DES/des_core/bit_utils.py - Core init · Package boundary for the DES core modules.
project_DES/des_core/__init__.py
Usage
Examples and test coverage around the library.
- String demo · Shows in-memory byte/string encryption and decryption.
examples/string_cipher.py - File demo · Shows file-based encryption and decryption with mode selection.
examples/file_cipher.py - ECB sample · Reference output data for ECB example runs.
examples/ecb_sample/mumei.bin - CBC sample · Reference output data for CBC example runs.
examples/cbc_sample/ina.bin - Core tests · Verifies the DES core transformations and composition.
test/des_core_test.py
Connections 22
- Package init → DES class: exports · Package import surface exposes the public DES class.
- DES class → Crypto core: uses · Public API delegates block processing to the core orchestrator.
- DES class → Key schedule: configures · Accepts and updates key material through the key schedule layer.
- DES class → String demo: demonstrated by · In-memory API usage is shown in the string example.
- DES class → File demo: demonstrated by · File-oriented API usage is shown in the file example.
- Crypto core → Feistel: orchestrates · Coordinates round-by-round Feistel processing.
- Crypto core → Key schedule: applies · Consumes derived round keys during encryption and decryption.
- Crypto core → Permutations: permutes · Uses initial, final, and expansion-style permutations.
- Feistel → Mangler: calls · Feistel rounds invoke the round-function transformation.
- Mangler → Permutations: uses · Round function relies on DES permutations and expansions.
- Mangler → Bit utils: uses · Round function depends on bit-level packing and splitting helpers.
- Key schedule → Permutations: uses · Key scheduling uses fixed DES permutations and selections.
- Key schedule → Bit utils: uses · Key normalization and shifts rely on bit utilities.
- Permutations → Bit utils: built on · Permutation helpers are implemented over low-level bit primitives.
- Core init → Crypto core: re-exports · Core package boundary surfaces the cryptographic orchestrator.
- String demo → DES class: uses · Example scripts consume the public DES API.
- File demo → DES class: uses · File workflows are built on the public DES API.
- File demo → ECB sample: writes · ECB example output is represented by sample artifacts.
- File demo → CBC sample: writes · CBC example output is represented by sample artifacts.
- Core tests → Crypto core: validates · Tests focus on the core block-cipher pipeline.
- Core tests → Feistel: validates · Tests cover round-level transformation behavior.
- Core tests → Key schedule: validates · Tests cover key schedule and round-key generation.