# EnigmaEnhanced
# Python 实现增强版恩尼格玛密码机
Enhanced Enigma Cipher Machine - Python Implementation


## 安装
install

bash
```
pip install EnigmaEnhanced
```


## 使用方式
Usage

### 交互模式
Interactive Mode

bash
```
enigmaenhanced
```

在交互模式下：
In interactive mode:

*输入/genkey生成密钥
 -Enter /genkey to generate a key

*输入/loadkey <filename>加载密钥
 -Enter /loadkey <filename> to load a key

*输入普通文本进行加密
 -Enter regular text for encryption

*输入以ENC:开头的文本进行解密
 -Enter text starting with ENC: for decryption

*输入/exit退出程序
 -Enter /exit to quit the program

### 批处理模式
Batch Mode

加密文本：
Encrypt text:

bash
```
enigmaenhanced --keyfile my_key.json --encrypt "明文plaintext"
```

解密文本：
Decrypt text:

bash
```
enigmaenhanced --keyfile my_key.json --decrypt "ENC:AbCdEfG..."
```


## 项目结构
Project Structure

```
EnigmaEnhanced/
├── EnigmaEnhanced/
│   ├── __init__.py
│   └── core.py
├── cli.py
├── setup.py
└── README.md
```


## 历史背景与现代演绎
Historical Context and Modern Interpretation

在密码学的历史长卷中，恩尼格玛密码机（Enigma）无疑是最具传奇色彩的篇章之一。这台由德国工程师Arthur Scherbius发明的机械加密设备，在二战期间曾被视为"不可破译"的通信保障，造就了人类密码学史上的重要里程碑。艾伦·图灵在布莱切利园领导的破译工作，不仅缩短了战争进程，更开启了现代计算机科学的先河。

In the grand tapestry of cryptography, the Enigma cipher machine stands as one of the most legendary chapters. Invented by German engineer Arthur Scherbius, this mechanical encryption device was considered "unbreakable" during WWII, becoming a crucial milestone in human cryptographic history. Alan Turing's codebreaking work at Bletchley Park not only shortened the war but also paved the way for modern computer science.

本项目是对这一历史经典的现代数字演绎，保留了恩尼格玛机核心的转子机制原理，同时融合了现代密码学技术：

This project is a modern digital interpretation of this historical classic, preserving the core rotor mechanism principle of the Enigma machine while incorporating modern cryptographic techniques:

1.密码学传承：保留经典的三转子+反射器结构，致敬历史原型
  -Cryptographic Heritage: Maintains the classic three-rotor + reflector structure, paying homage to the historical prototype

2.现代增强：引入SHA-256哈希增强密钥派生，大幅提升安全性
  -Modern Enhancement: Incorporates SHA-256 hashing for key derivation, significantly improving security

3.Unicode扩展：突破原始设备26字母限制，支持全字符集加密
  -Unicode Extension: Breaks the 26-letter limitation of the original device, supporting full character set encryption

4.密钥管理：实现完善的密钥生成、存储和加载机制
  -Key Management: Implements comprehensive key generation, storage, and loading mechanisms

**"密码学不仅是保护信息的科学，更是人类智慧与创造力的永恒战场。" —— Whitfield Diffie（公钥密码学先驱）**
**"Cryptography is not just a science of protecting information; it is an eternal battlefield of human ingenuity." — Whitfield Diffie (Pioneer of Public Key Cryptography)**


## 功能亮点
Key Features

python
```
class EnigmaEnhanced:
    def __init__(self):
        # 初始化增强版恩尼格玛机
        # Initialize enhanced Enigma machine
        self.key = None
        self.print_help()
```

1.增强密钥系统
Enhanced Key System

*HKDF式密钥派生算法（基于SHA-256）
 -HKDF-like key derivation algorithm (based on SHA-256)*

*256位加密种子生成（符合现代安全标准）
 -256-bit encryption seed generation (meets modern security standards)*

*三转子配置，每个转子含256个位置（支持全字节范围）
 -Three-rotor configuration, each with 256 positions (supports full byte range)*

2.智能命令界面
Intelligent Command Interface

python
```
# 命令处理示例
# Command processing example
if user_input.startswith("/"):
    # 命令解析逻辑
    # Command parsing logic
    if command == "genkey":
        enigma.generate_key(filename)
```

3.错误防御机制
Error Defense Mechanism

*密钥不匹配检测
 -Key mismatch detection

*密文损坏识别
 -Ciphertext corruption recognition

*异常处理保障程序健壮性
 -Exception handling ensures program robustness

*多语言支持
 -Multilingual Support

*完整UTF-8编码支持
 -Full UTF-8 encoding support

*中英双语用户界面
 -Bilingual Chinese-English user interface

*全球化错误提示
 -Globalized error messages


## 应用场景
Application Scenarios

1. 密码学教育
Cryptography Education

*生动演示转子密码机工作原理
 -Vivid demonstration of rotor cipher machine working principles

*对比古典密码与现代加密技术差异
 -Comparison of classical and modern encryption techniques

*密码分析实战平台（可扩展为CTF挑战）
 -Practical platform for cryptanalysis (extendable to CTF challenges)

2. 安全通信
Secure Communication

python
```
# 加密过程核心
# Core encryption process
def _process_byte(self, byte_val: int, rotors: List[List[int]], reflector: List[int]) -> int:
    # 转子加密流程
    # Rotor encryption process
    for rotor in rotors:
        byte_val = rotor[byte_val]
    byte_val = reflector[byte_val]
    for rotor in reversed(rotors):
        byte_val = rotor.index(byte_val)
    return byte_val
```

*安全聊天应用的基础加密层
 -Basic encryption layer for secure chat applications

*敏感信息临时保护（配合安全信道交换密钥）
 -Temporary protection of sensitive information (with secure key exchange)

*数字版权管理（轻量级内容保护）
 -Digital rights management (lightweight content protection)

3. 文化遗产保护
Cultural Heritage Preservation

*博物馆互动展示系统
 -Museum interactive display systems

*密码学历史数字体验
 -Digital experience of cryptographic history

*二战密码战教学工具
 -Teaching tool for WWII cryptography warfare

4. 创意应用开发
Creative Application Development

*神秘主题游戏加密引擎
 -Encryption engine for mystery-themed games

*数字艺术作品的隐藏信息层
 -Hidden information layer for digital artworks

*诗歌/文学作品的加密创作
 -Encrypted creation of poetry/literary works


## 技术优势
Technical Advantages

1.密码增强设计
Cryptographic Enhancement Design

python
```
def _derive_key(self, seed: bytes, length: int) -> bytes:
    # HKDF式密钥派生
    # HKDF-like key derivation
    key = b''
    salt = b'enigma_salt'
    counter = 0
    while len(key) < length:
        key += hashlib.sha256(salt + seed + counter.to_bytes(4, 'big')).digest()
        counter += 1
    return key[:length]
```

2.密码混淆技术
Cryptographic Obfuscation Technique

python
```
# 加密后混淆处理
# Post-encryption obfuscation
key_material = self._derive_key(seed, len(processed_bytes))
for i, byte_val in enumerate(processed_bytes):
    encrypted.append(byte_val ^ key_material[i])
```

3.完整Unicode支持
Full Unicode Support

python
```
# UTF-8编码处理
# UTF-8 encoding processing
text_bytes = text.encode('utf-8')
# ...
return bytes(result_bytes).decode('utf-8')
```


## 使用示例
Usage Example

bash
```
> /genkey secret_key.json
密钥已生成并保存到 secret_key.json
Key has been generated and saved to secret_key.json

> 你好，世界！
加密结果: ENC:7Hj8F3kL9aB2cX5z...
Encryption result: ENC:7Hj8F3kL9aB2cX5z...

> ENC:7Hj8F3kL9aB2cX5z...
解密结果: 你好，世界！
Decryption result: 你好，世界！
```

## 项目意义
Project Significance

本实现架起了密码学历史与现代技术的桥梁，在保留恩尼格玛机精妙机械设计思想的同时，赋予其适应数字时代的新生命。正如密码学家Bruce Schneier所言："**安全是一个过程，而非产品。**" 本项目旨在延续这一过程，让历史智慧在现代技术中焕发新生。

This implementation bridges cryptographic history with modern technology, preserving the ingenious mechanical design philosophy of the Enigma machine while giving it new life adapted to the digital age. As cryptographer Bruce Schneier said: "**Security is a process, not a product.**" This project aims to continue this process, revitalizing historical wisdom with modern technology.

**免责声明：** 本工具为教育目的设计，不应用于实际安全通信。现代加密应使用AES、RSA等标准化算法。
**Disclaimer:** This tool is designed for educational purposes and should not be used for actual secure communication. Modern encryption should use standardized algorithms such as AES or RSA.

![ ](https://github.com/PaulLiszt/EnigmaEnhanced/blob/c0bb58f992df4c11a79091ab4c4bd1614973710c/mermaid.png)

欢迎贡献代码！让我们共同守护数字世界的安全与隐私。
Welcome to contribute code! Let's protect the security and privacy of the digital world together.
