Krishita17/cryptospectra
GitHub: Krishita17/cryptospectra
基于可视密码学的安全图像共享工具,支持五种 VC 方案将图像拆分为噪声共享分片并无损重构。
Stars: 1 | Forks: 0
# 🔐 CryptoSpectra
### 使用可视密码学的安全图像共享
*将任何二进制、灰度或彩色图像分割成自身不显示任何内容的类噪声密码共享(shares)——并且只有在正确的共享重新组合时才能重构出秘密。*




## 概述
传统加密非常繁重,且一旦密钥泄露就会失效。**可视密码学 (Visual Cryptography, VC)**
采取了另一种途径:将秘密图像分成几个*共享*,每一个都是纯统计
噪声。没有任何共享——或任何不完整的子集——能揭示关于秘密的任何信息。只有
重新组合正确的共享,原始图像才会重现,而解密成本几乎为零。
CryptoSpectra 是对学位论文
**“使用可视密码学的安全图像共享”**(Krishita Choksi,网络安全与
数字取证学院,NFSU,2025年)的清晰、打包的重新实现。它在一个单一的
库、一个命令行工具和一个交互式仪表板背后,捆绑了用于三种图像类型的五种 VC 方案,并使用 PSNR、SSIM、NCORR 和 SHA-256 哈希完整性评估每一次重构。
## ✨ 功能
- **跨越 3 种图像类型的 5 种方案** — 为图像选择合适的工具(见下表)。
- **阈值共享(2–8 个共享)** 分布在模拟的 **1-服务器 / 3-客户端** 网络中。
- **无损重构** 用于 XOR、Modular Arithmetic、Bit-Level Decomposition 和 Pixel Expansion
(PSNR 100 dB, SSIM 1.0, 精确哈希匹配)。
- **内置质量 + 完整性指标**:PSNR, NCORR, SSIM, SHA-256 哈希匹配。
- **性能指标** — 加密/解密时间,峰值内存,CPU 使用率。
- **三种使用方式:** Python API, `cli.py`, 和 Streamlit `dashboard.py`。
- **可重复性** — 可选的 RNG 种子;精确数组 (`.npy`) 共享持久化。
## 🧩 方案
| 图像类型 | 方案 | Key | 共享 | 像素扩展 | 重构 |
|------------|--------|-----|:------:|:---------------:|----------------|
| 灰度 | XOR (N,N) | `xor` | 2–8 | 无 | **无损** |
| 灰度 | Modular Arithmetic (N,N) | `modular` | 2–8 | 无 | **无损** |
| 灰度 | Bit-Level Decomposition | `bld` | 2–8 | 2× | **无损** (提取) |
| 二进制 | XOR (N,N) | `xor` | 2–8 | 无 | **无损** |
| 二进制 | Modular Arithmetic (N,N) | `modular` | 2–8 | 无 | **无损** |
| 二进制 | Pixel Expansion (2,2) | `pe` | 2 | 2× | **无损** (提取) |
| 彩色 | XOR (N,N) | `xor` | 2–8 | 无 | **无损** |
| 彩色 | Modular Arithmetic (N,N) | `modular` | 2–8 | 无 | **无损** |
| 彩色 | CMYK Halftone | `cmyk` | 2 | 无 | 有损 |
## 📁 项目结构
```
cryptospectra/
├── cli.py # unified command-line interface
├── dashboard.py # Streamlit interactive dashboard
├── requirements.txt
├── pyproject.toml
├── cryptospectra/ # the library
│ ├── algorithms/ # one module per scheme + a registry
│ │ ├── xor.py modular.py bld.py pixel_expansion.py cmyk.py
│ │ ├── base.py # common Scheme / SharesBundle interface
│ │ └── registry.py # (type, method) -> scheme lookup
│ ├── metrics/ # PSNR / NCORR / SSIM / hash (unified)
│ ├── network/ # server / client2 / client3 simulation
│ ├── pipeline.py # encrypt -> decrypt -> evaluate (+ resource stats)
│ ├── storage.py # share persistence (.npy + .png)
│ └── visualize.py # composite figures
├── data/
│ ├── input/
│ │ ├── grayscale/ # pebble, girl, dog
│ │ ├── binary/ # rings, flower
│ │ └── colour/ # blast, bomb, pebble, flower
│ └── output/
│ ├── shares/ decrypted/ reports/
└── docs/
└── Secure-Image-Sharing-Using-Visual-Cryptography-Report.pdf
```
输入按图像类型组织;每次运行都会将共享、重构的图像和
可选的报告图表写入匹配的 `data/output/` 文件夹中。
## 🚀 安装
```
git clone https://github.com/Krishita17/cryptospectra.git
cd cryptospectra
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
```
## 🖥️ 用法
### 1. 命令行 (`cli.py`)
```
# 列出所有可用的 (type, method) scheme
python cli.py methods
# 加密 -> 解密 -> 评估单张图像,保存 shares + 报告图表
python cli.py run -i data/input/grayscale/pebble.png -t grayscale -m xor -s 4 --plot --save-shares
# Lossy colour 示例
python cli.py run -i data/input/colour/pebble.png -t colour -m cmyk
# 对捆绑样本进行 dissertation 的 headline cases 的 benchmark
python cli.py demo
```
**客户端-服务器分布**(1 个服务器,3 个客户端 — 阈值访问控制):
```
# Server:加密并拆分为 4 个 shares(1 -> client1,1 -> client2,剩余 -> client3)
python cli.py distribute -i data/input/colour/pebble.png --id pebble -t colour -m xor -s 4 --n1 1 --n2 1
# Client2:将 client1 + client2 的 shares 转发给 client3
python cli.py forward --id pebble
# Client3:收集所有 shares,进行重建,并与原始图像进行对比评估
python cli.py combine --id pebble -t colour -m xor --original data/input/colour/pebble.png
```
### 2. 仪表板 (`dashboard.py`)
```
streamlit run dashboard.py
```
选择一张图像(上传或使用内置样本),选择图像类型、方案和共享数量,然后
观看共享和重构后的图像并排出现,并带有完整的指标、性能图表
和差异热力图。
### 3. Python API
```
from PIL import Image
from cryptospectra import run_pipeline, get_scheme
# 带有 metrics + timing 的 One-shot pipeline
result = run_pipeline(Image.open("data/input/grayscale/pebble.png"),
image_type="grayscale", method="xor", num_shares=4)
print(result.summary())
result.reconstruction.image.save("recovered.png")
# 或直接驱动 scheme
scheme = get_scheme("colour", "modular")
bundle = scheme.encrypt(Image.open("data/input/colour/bomb.jpg"), num_shares=3)
recon = scheme.decrypt(bundle.shares)
```
## 📊 结果
从学位论文在 *Pebble* 测试图像上重现(运行 `python cli.py demo`):
**灰度(表 4.2)**
| 指标 | XOR | Modular Arithmetic | BLD |
|--------|:---:|:------------------:|:---:|
| PSNR | 100 dB | 100 dB | 100 dB |
| SSIM | 1.00 | 1.00 | 1.00 |
| 哈希匹配 | ✅ | ✅ | ✅ |
**二进制(表 4.3)**
| 指标 | XOR | Pixel Expansion |
|--------|:---:|:---------------:|
| PSNR | 100 dB | 100 dB |
| SSIM | 1.00 | 1.00 |
| 哈希匹配 | ✅ | ✅ |
**彩色(表 4.4)**
| 指标 | XOR | Modular Arithmetic | CMYK Halftone |
|--------|:---:|:------------------:|:-------------:|
| PSNR | 100 dB | 100 dB | ~33 dB |
| SSIM | 1.00 | 1.00 | 0.8028 |
| 哈希匹配 | ✅ | ✅ | ❌ (有损) |
**性能亮点(表 4.5–4.7):** XOR 是最快且最轻量的方案
(~0.01 秒,小于 1 MB)。Modular Arithmetic 很均衡。BLD 代价最高(几十秒,
峰值 ~1 GB),因为它扩展了八个位平面 —— 除非需要 2× 扩展,否则请优先使用 XOR/MA。
## 🏗️ 架构
```
┌──────────┐ encrypt + split shares ┌──────────┐
secret ─▶│ SERVER │ ─────────────────────────▶ │ client1 │─┐
└──────────┘ └──────────┘ │ forward
│ ┌──────────┐ │
├────────────────────────────────│ client2 │─┤
│ └──────────┘ │
└────────────────────────────────┐ ▼
▼ ┌─────────────┐
┌──────────┐│ client3 │
│ client3 ││ combined/ │─▶ decrypt + evaluate
└──────────┘└─────────────┘
```
秘密永远不会整体传输。每个客户端仅持有一部分共享;`client3` 能够重构
图像,**只有在** `client2` 转发 `client1` 和 `client2` 持有的共享之后。这反映了
学位论文的阈值访问工作流程,并作为文件系统模拟在
`cryptospectra/network/` 中实现(无需活动的 socket 连接)。
## 🔬 指标
| 指标 | 含义 | 完美值 |
|--------|---------|:-------------:|
| **PSNR** | 峰值信噪比 (dB) — 重构保真度 | 100 dB (完全一致) |
| **NCORR** | 原始输出之间的归一化互相关 | 1.0 |
| **SSIM** | 结构相似性(亮度/对比度/结构) | 1.0 |
| **哈希** | SHA-256 相等性 — 完整性/篡改检测 | 匹配 |
## 🛠️ 与原项目的不同之处
这是对原始 `Visual-Cryptography-ongoing` 脚本的重组和清理。密码学
逻辑得到保留,因此结果与学位论文相匹配,并进行了以下修正:
- **约 55 个分散的脚本 → 一个库 + 一个 CLI + 一个仪表板。** 27 个几乎重复的
`server*/client2*/client3*` 文件合并为一个单一的参数化模拟;独立的
`*enc/*dec` 脚本成为注册表背后的可重用方案模块。
- **分类的 I/O。** 输入被拆分为 `grayscale/`, `binary/`, `colour/`;输出被拆分为
`shares/`, `decrypted/`, `reports/`。
- **精确的共享持久化。** 共享存储为 `.npy`(精确)并附带 `.png` 预览,因此
重构永远不会因为 PNG 重新量化而失去保真度。
- **一致的 NCORR。** 早期的 `normxcorr2D` 将完美相关的区域置零,将
完全相同的图像评分在 ~0.03 左右;现在统一的指标为精确匹配
正确地返回 **1.0**。
- **向量化的 CMYK。** 每像素的 `getpixel`/`putpixel` 循环替换为 NumPy(结果相同,速度快得多)。
- **可重复性 + 安全性。** 可选的 RNG 种子;现代的 `numpy.random.default_rng`;没有泄露
可预测共享的固定默认种子。
- **浏览器仪表板** (Streamlit) 取代了 Tkinter 的文件对话框 GUI。
## 👤 作者
**Krishita Choksi** — B.Tech–M.Tech 计算机科学工程(网络安全),NFSU,甘地讷格尔。
由 **Dr. Mukti Padhya** 指导。
完整的学位论文位于 [`docs/`](docs/)。
## 📄 许可证
在 [MIT 许可证](LICENSE)下发布。标签:Kubernetes, Python, Streamlit, 信息隐藏, 图像加密, 密码学, 手动系统调用, 无后门, 视觉密码学, 访问控制, 逆向工具