sahasra-006/StegoCrypt-Secure-Image-Steganography
GitHub: sahasra-006/StegoCrypt-Secure-Image-Steganography
一个基于 Django 的图像隐写 Web 应用,结合 LSB 隐写术与 AES-128-CBC 加密,并通过 OTP 邮箱验证保护解码流程。
Stars: 1 | Forks: 0
# 安全图像隐写 Web 应用
一个生产级的 Django Web 应用程序,使用 **LSB 隐写术**、**Fernet 加密 (AES-128-CBC)** 和 **基于 OTP 的双因素认证** 在图像中隐藏加密消息。
## 功能特性
| 功能 | 详情 |
|---|---|
| **LSB 隐写术** | 将加密数据隐藏在图像像素的最低有效位中 —— 视觉上与原图无异 |
| **Fernet 加密** | AES-128-CBC + HMAC-SHA256,通过 PBKDF2(39万次迭代)从密码派生密钥 |
| **OTP 双因素认证** | 在任何解码操作前,向邮箱发送 6 位数字的限时 OTP |
| **容量检查** | 实时 AJAX 检查,防止消息溢出 |
| **可共享链接** | 基于 UUID 的解码 URL —— `/decode//` |
| **用户控制面板** | 显示所有已编码图像及其缩略图、时间戳和下载链接 |
| **JPG → PNG 自动转换** | 所有图像均以无损格式存储,以保护 LSB 数据 |
| **UTF-8 支持** | 支持处理消息中的表情符号、CJK 字符和带有重音的字符 |
## 项目结构
```
steganography_project/
├── manage.py
├── requirements.txt
├── README.md
│
├── steganography_project/ # Django project config
│ ├── __init__.py
│ ├── settings.py # All settings (email, OTP expiry, media)
│ ├── urls.py # Root URL dispatcher
│ └── wsgi.py
│
├── steganography_app/ # Main application
│ ├── __init__.py
│ ├── apps.py
│ ├── admin.py # Admin panel registrations
│ ├── models.py # EncodedImage + OTPRecord models
│ ├── forms.py # All forms (signup, encode, decode, OTP)
│ ├── views.py # All views with full encode/decode flow
│ ├── urls.py # App-level URL patterns
│ ├── utils.py # Core logic: LSB, Fernet, OTP
│ │
│ └── templates/steganography_app/
│ ├── base.html # Dark cipher-themed base layout
│ ├── home.html # Landing page
│ ├── login.html
│ ├── signup.html
│ ├── dashboard.html # Image history + shareable links
│ ├── encode.html # Encode form with live capacity meter
│ ├── decode.html # Decode step 1 (upload + password)
│ ├── decode_link.html # Decode via shareable UUID link
│ ├── otp_verify.html # OTP entry with 5-min countdown
│ └── decode_result.html # Revealed message display
│
└── media/ # User-uploaded & encoded images
├── original_images/
└── encoded_images/
```
## 第 1 步:设置与安装
### 1.1 创建虚拟环境
```
python3 -m venv venv
source venv/bin/activate # Linux/Mac
# venv\Scripts\activate # Windows
```
### 1.2 安装依赖
```
pip install -r requirements.txt
```
**requirements.txt:**
```
Django>=4.2,<5.0
Pillow>=10.0.0
cryptography>=41.0.0
```
### 1.3 应用数据库迁移
```
python manage.py makemigrations
python manage.py migrate
```
### 1.4 创建超级用户(可选,用于管理后台)
```
python manage.py createsuperuser
```
### 1.5 运行开发服务器
```
python manage.py runserver
```
打开浏览器访问:**http://127.0.0.1:8000/**
## 第 2 步:邮箱 / OTP 配置
### 开发环境(默认 —— OTP 打印到终端)
在 `settings.py` 中,默认后端为:
```
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
```
当你触发解码操作时,**请查看终端** —— OTP 会显示在那里。
### 生产环境(以 Gmail SMTP 为例)
编辑 `settings.py`:
```
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your@gmail.com' # or use os.environ.get(...)
EMAIL_HOST_PASSWORD = 'your-app-password' # Gmail App Password (not login password)
DEFAULT_FROM_EMAIL = 'your@gmail.com'
```
## 各流程工作原理
### 编码流程
```
User uploads image
↓
Enter secret message + password + optional label
↓
[Server] encrypt_message(message, password) → Fernet ciphertext
↓
[Server] encode_image(image, ciphertext)
→ payload bytes = ciphertext.encode('utf-8') + b'###END###'
→ convert to bit stream
→ replace LSB of each R,G,B channel pixel by pixel
→ save as lossless PNG
↓
Record saved to DB (EncodedImage model) with UUID
↓
User downloads PNG from Dashboard
```
### 解码流程(双因素认证)
```
User uploads encoded PNG + enters password
↓
[Server] generate_otp() → 6-digit code
save OTPRecord to DB (expires in 5 min)
send_otp_email() → console (dev) or SMTP (prod)
↓
User enters OTP on verification page
↓
[Server] check: not expired + verify_otp(input, stored)
↓
[Server] decode_image(png) → extract ciphertext from LSBs
decrypt_message(ciphertext, password) → original plaintext
↓
Message displayed to user
```
## 安全架构
### 加密
- **算法:** Fernet = AES-128-CBC + HMAC-SHA256
- **密钥派生:** PBKDF2-HMAC-SHA256,390,000 次迭代(OWASP 最低标准),固定盐值
- **结果:** 没有确切密码,解密将产生乱码或引发 `InvalidToken` 异常
### LSB 隐写术
- 每个 R、G、B 通道的 **最低有效位** 被替换为 payload 的一位
- 每个通道的视觉变化:最大 ±1 强度(人眼无法察觉)
- 所有图像均保存为 **PNG**(无损)格式 —— JPEG 压缩会破坏 LSB 数据
- 分隔符 `###END###` 用于标记 payload 的结尾;支持完整的 UTF-8(表情符号、CJK 字符等)
### OTP 双因素认证
- `random.SystemRandom()` 使用操作系统级别的熵(加密安全)
- 存储在 `OTPRecord` 数据库表中(而不仅仅是会话中)
- 在 **5 分钟** 后过期(可通过配置中的 `OTP_EXPIRY_MINUTES` 进行设置)
- 使用 `hmac.compare_digest()` 进行验证(恒定时间比较,防止时序攻击)
- 在生成新 OTP 之前,会删除旧的未验证 OTP
### 容量计算公式
```
max_bytes = (image_width × image_height × 3) // 8
```
一张 800×600 的图像 → 180,000 字节容量(约 175 KB 消息量)。
## URL 参考
| URL | 视图 | 需要认证 |
|---|---|---|
| `/` | 主页 / 落地页 | 否 |
| `/signup/` | 用户注册 | 否 |
| `/login/` | 登录 | 否 |
| `/logout/` | 登出 | 否 |
| `/dashboard/` | 图像历史记录 | ✅ 是 |
| `/encode/` | 编码消息 | ✅ 是 |
| `/decode/` | 解码(上传) | ✅ 是 |
| `/decode//` | 通过链接解码 | ✅ 是 |
| `/decode/verify-otp/` | OTP 验证 | ✅ 是 |
| `/decode/resend-otp/` | 重新发送 OTP | ✅ 是 |
| `/api/capacity-check/` | AJAX 容量检查 | ✅ 是 |
| `/admin/` | Django 管理后台 | ✅ 超级用户 |
## 生产部署指南
### 选项 A:Render(免费套餐)
1. 推送至 GitHub
2. 在 render.com 上创建一个新的 **Web Service**
3. 设置构建命令:`pip install -r requirements.txt && python manage.py migrate`
4. 设置启动命令:`gunicorn steganography_project.wsgi`
5. 添加环境变量:`SECRET_KEY`、`EMAIL_HOST_USER`、`EMAIL_HOST_PASSWORD`
### 选项 B:AWS EC2 + Nginx + Gunicorn
```
pip install gunicorn
gunicorn steganography_project.wsgi:application --bind 0.0.0.0:8000
```
### S3 媒体存储
```
pip install boto3 django-storages
```
添加到 `settings.py`:
```
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_STORAGE_BUCKET_NAME = 'your-bucket'
AWS_S3_REGION_NAME = 'us-east-1'
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
```
### 用于 OTP/会话的 Redis(生产环境)
```
pip install django-redis
```
```
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
```
### 生产安全清单
```
DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com']
SECRET_KEY = os.environ.get('SECRET_KEY') # Never hardcode!
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
SECURE_HSTS_SECONDS = 31536000
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
```
## 快速冒烟测试(无需浏览器)
```
# 1. 启动服务器
python manage.py runserver
# 2. 在 http://127.0.0.1:8000/signup/ 注册
# 3. Encode 消息:
# - 前往 /encode/
# - 上传任意图片
# - 输入消息
# - 设置密码(例如 "test123")
# - 点击 Encode
# 4. Decode 消息:
# - 前往 /decode/ 或点击仪表板中的图片
# - 上传已下载的 PNG
# - 输入相同的密码
# - CHECK THE TERMINAL 获取 OTP 代码
# - 输入 OTP → 查看原始消息
```
## 📚 技术参考
- **Fernet 规范:** https://github.com/fernet/spec/
- **PBKDF2 OWASP:** https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
- **LSB 隐写术:** 空间域中的最低有效位替换
- **Django 认证:** https://docs.djangoproject.com/en/4.2/topics/auth/
标签:2FA, AES-128-CBC, Django, DNS枚举, Fernet加密, HMAC-SHA256, LSB算法, meg, PBKDF2, Python, UTF-8, UUID, 二次验证, 信息加密, 信息安全, 加密通信, 响应式UI, 图像隐写, 图片加密, 密码派生, 数字水印, 数据隐藏, 无后门, 深色主题, 网络安全, 网络安全工具, 逆向工具, 邮箱OTP, 隐写术, 隐私保护, 隐秘通信