# shrimp2845-tw/project_des architecture

> Architecture diagram of the GitHub repository shrimp2845-tw/project_des, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shrimp2845-tw/project_des
- Repository: https://github.com/shrimp2845-tw/project_des

## Overview

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/project_DES.py`](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/project_DES.py) (DES class), [`project_DES/des_core/crypto.py`](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/crypto.py) (Crypto core), [`project_DES/des_core/key.py`](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/key.py) (Key schedule), [`examples/file_cipher.py`](https://github.com/shrimp2845-tw/project_des/blob/main/examples/file_cipher.py) (File demo)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_public["Public API"]
  node_node_des_class["DES class<br/>public API<br/>[project_DES.py]"]
  node_node_pkg_init["Package init<br/>package export<br/>[__init__.py]"]
end

subgraph group_group_core["DES Core"]
  node_node_crypto["Crypto core<br/>block orchestration<br/>[crypto.py]"]
  node_node_feistel["Feistel<br/>round control<br/>[feistel.py]"]
  node_node_mangler["Mangler<br/>round function<br/>[mangler.py]"]
  node_node_key["Key schedule<br/>key handling<br/>[key.py]"]
  node_node_permute["Permutations<br/>bit selection<br/>[permute.py]"]
  node_node_bits["Bit utils<br/>bit primitives<br/>[bit_utils.py]"]
  node_node_core_init["Core init<br/>core export<br/>[__init__.py]"]
end

subgraph group_group_usage["Usage"]
  node_node_string_example["String demo<br/>example<br/>[string_cipher.py]"]
  node_node_file_example["File demo<br/>example<br/>[file_cipher.py]"]
  node_node_ecb_sample["ECB sample<br/>artifact<br/>[mumei.bin]"]
  node_node_cbc_sample["CBC sample<br/>artifact<br/>[ina.bin]"]
  node_node_core_test["Core tests<br/>validation<br/>[des_core_test.py]"]
end

node_node_pkg_init -->|"exports"| node_node_des_class
node_node_des_class -->|"uses"| node_node_crypto
node_node_des_class -->|"configures"| node_node_key
node_node_des_class -.->|"demonstrated by"| node_node_string_example
node_node_des_class -.->|"demonstrated by"| node_node_file_example
node_node_crypto -->|"orchestrates"| node_node_feistel
node_node_crypto -->|"applies"| node_node_key
node_node_crypto -->|"permutes"| node_node_permute
node_node_feistel -->|"calls"| node_node_mangler
node_node_mangler -->|"uses"| node_node_permute
node_node_mangler -->|"uses"| node_node_bits
node_node_key -->|"uses"| node_node_permute
node_node_key -->|"uses"| node_node_bits
node_node_permute -->|"built on"| node_node_bits
node_node_core_init -.->|"re-exports"| node_node_crypto
node_node_string_example -.->|"uses"| node_node_des_class
node_node_file_example -.->|"uses"| node_node_des_class
node_node_file_example -.->|"writes"| node_node_ecb_sample
node_node_file_example -.->|"writes"| node_node_cbc_sample
node_node_core_test -.->|"validates"| node_node_crypto
node_node_core_test -.->|"validates"| node_node_feistel
node_node_core_test -.->|"validates"| node_node_key

click node_node_des_class "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/project_DES.py"
click node_node_pkg_init "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/__init__.py"
click node_node_crypto "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/crypto.py"
click node_node_feistel "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/feistel.py"
click node_node_mangler "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/mangler.py"
click node_node_key "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/key.py"
click node_node_permute "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/permute.py"
click node_node_bits "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/bit_utils.py"
click node_node_core_init "https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/__init__.py"
click node_node_string_example "https://github.com/shrimp2845-tw/project_des/blob/main/examples/string_cipher.py"
click node_node_file_example "https://github.com/shrimp2845-tw/project_des/blob/main/examples/file_cipher.py"
click node_node_ecb_sample "https://github.com/shrimp2845-tw/project_des/blob/main/examples/ecb_sample/mumei.bin"
click node_node_cbc_sample "https://github.com/shrimp2845-tw/project_des/blob/main/examples/cbc_sample/ina.bin"
click node_node_core_test "https://github.com/shrimp2845-tw/project_des/blob/main/test/des_core_test.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_des_class,node_node_pkg_init toneBlue
class node_node_crypto,node_node_feistel,node_node_mangler,node_node_key,node_node_permute,node_node_bits,node_node_core_init toneAmber
class node_node_string_example,node_node_file_example,node_node_ecb_sample,node_node_cbc_sample,node_node_core_test toneMint
```

## Components

### Public API

User-facing package boundary and entrypoint.

- [DES class](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/project_DES.py): public API · User-facing encryption, decryption, mode, key, and file interface. · `project_DES/project_DES.py`
- [Package init](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/__init__.py): package export · Exports the public DES entry point. · `project_DES/__init__.py`

### DES Core

Block cipher orchestration, rounds, keys, and bit-level transforms.

- [Crypto core](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/crypto.py): block orchestration · Coordinates single-block DES processing and overall round structure. · `project_DES/des_core/crypto.py`
- [Feistel](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/feistel.py): round control · Drives the DES round flow around the left and right halves. · `project_DES/des_core/feistel.py`
- [Mangler](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/mangler.py): round function · Implements the DES F-function style transformation used inside rounds. · `project_DES/des_core/mangler.py`
- [Key schedule](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/key.py): key handling · Normalizes key input and derives per-round keys. · `project_DES/des_core/key.py`
- [Permutations](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/permute.py): bit selection · Performs DES bit rearrangement, expansion, and permutation steps. · `project_DES/des_core/permute.py`
- [Bit utils](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/bit_utils.py): bit primitives · Low-level bit splitting, shifting, and packing helpers. · `project_DES/des_core/bit_utils.py`
- [Core init](https://github.com/shrimp2845-tw/project_des/blob/main/project_DES/des_core/__init__.py): core export · Package boundary for the DES core modules. · `project_DES/des_core/__init__.py`

### Usage

Examples and test coverage around the library.

- [String demo](https://github.com/shrimp2845-tw/project_des/blob/main/examples/string_cipher.py): example · Shows in-memory byte/string encryption and decryption. · `examples/string_cipher.py`
- [File demo](https://github.com/shrimp2845-tw/project_des/blob/main/examples/file_cipher.py): example · Shows file-based encryption and decryption with mode selection. · `examples/file_cipher.py`
- [ECB sample](https://github.com/shrimp2845-tw/project_des/blob/main/examples/ecb_sample/mumei.bin): artifact · Reference output data for ECB example runs. · `examples/ecb_sample/mumei.bin`
- [CBC sample](https://github.com/shrimp2845-tw/project_des/blob/main/examples/cbc_sample/ina.bin): artifact · Reference output data for CBC example runs. · `examples/cbc_sample/ina.bin`
- [Core tests](https://github.com/shrimp2845-tw/project_des/blob/main/test/des_core_test.py): validation · Verifies the DES core transformations and composition. · `test/des_core_test.py`

## Connections

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