image.domain\encryption\image_encryption.mbt
86.2% · 131 / 152 points · 536 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像加密 encryption/image_encryption - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像加密(v12.14.0) - 7
// 基于混沌的图像加密、Logistic 映射、加密质量评估 - 8
- 9
///| - 10
/// 加密配置 - 11
pub(all) struct EncryptionConfig { - 12
method_type : String // "logistic" / "xor" - 13
key : Double // 混沌系统初始值 - 14
mu : Double // Logistic 映射参数(通常 3.5699456 < mu <= 4) - 15
iterations : Int32 // 混沌迭代次数 - 16
} - 17
- 18
///| - 19
/// 创建默认加密配置 - 20
pub fn encryption_default_config() -> EncryptionConfig { - 21●
EncryptionConfig::{ - 22
method_type: "logistic", - 23
key: 0.123456789, - 24
mu: 3.99, - 25
iterations: 1000, - 26
} - 27
} - 28
- 29
///| - 30
/// 简单的伪随机数生成器(线性同余) - 31
fn lcg_next(seed : Int32) -> (Int32, Int32) { - 32●
let a = 1103515245 - 33
let c = 12345 - 34
let m = 1073741824 - 35●
let next = ((a * seed + c) % m).abs() - 36
(next, next) - 37
} - 38
- 39
///| - 40
/// Logistic 混沌映射:x(n+1) = mu * x(n) * (1 - x(n)) - 41
fn logistic_map(x : Double, mu : Double) -> Double { - 42●
mu * x * (1.0 - x) - 43
} - 44
- 45
///| - 46
/// 生成混沌序列 - 47
fn generate_chaotic_sequence( - 48
length : Int32, - 49
key : Double, - 50
mu : Double, - 51
warmup~ : Int32 = 100, - 52
) -> Array[Double] { - 53●
let warm = warmup - 54
let sequence : Array[Double] = [] - 55
let mut x = key - 56
// 预热(丢弃前 warm 个值) - 57
for _ in 0..<warm { - 58●
x = logistic_map(x, mu) - 59
} - 60
// 生成序列 - 61
for _ in 0..<length { - 62●
x = logistic_map(x, mu) - 63●
sequence.push(x) - 64
} - 65
sequence - 66
} - 67
- 68
///| - 69
/// 基于 Logistic 混沌映射的图像加密 - 70
pub fn encrypt_image_logistic( - 71
img : @types.Image, - 72
key~ : Double = 0.123456789, - 73
mu~ : Double = 3.99, - 74
) -> @types.Image raise @types.LoadError { - 75
// 输入验证 - 76●
if img.width <= 0 || img.height <= 0 { - 77●
raise @types.LoadError::DecodeFailed( - 78
"encrypt_image_logistic: 图像尺寸必须大于0", - 79
) - 80
} - 81
if img.channels < 1 || img.channels > 4 { - 82●
raise @types.LoadError::DecodeFailed( - 83
"encrypt_image_logistic: 通道数必须在1-4之间", - 84
) - 85
} - 86
let expected_size = img.width * img.height * img.channels - 87●
if img.data.length() != expected_size { - 88●
raise @types.LoadError::DecodeFailed( - 89
"encrypt_image_logistic: 数据大小不匹配", - 90
) - 91
} - 92
let k = key - 93
let m = mu - 94
let w = img.width - 95
let h = img.height - 96
let channels = img.channels - 97
let total_pixels = w * h * channels - 98
// 生成混沌序列 - 99●
let chaotic = generate_chaotic_sequence(total_pixels, k, m, warmup=100) - 100
// XOR 加密 - 101●
let out_data : Array[Byte] = Array::make(total_pixels, b'\x00') - 102
for i in 0..<total_pixels { - 103
// 将混沌值映射到 0-255 - 104●
let chaotic_byte = (chaotic[i] * 256.0).to_int().min(255).max(0) - 105●
let original = img.data[i].to_int() - 106
let encrypted = original ^ chaotic_byte - 107●
out_data[i] = encrypted.to_byte() - 108
} - 109
@types.Image::{ - 110
width: w, - 111
height: h, - 112
channels, - 113●
data: Bytes::from_array(out_data), - 114
} - 115
} - 116
- 117
///| - 118
/// 基于 Logistic 混沌映射的图像解密(与加密相同,因为 XOR 是对称的) - 119
pub fn decrypt_image_logistic( - 120
img : @types.Image, - 121
key~ : Double = 0.123456789, - 122
mu~ : Double = 3.99, - 123
) -> @types.Image raise @types.LoadError { - 124
// 输入验证 - 125●
if img.width <= 0 || img.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"decrypt_image_logistic: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img.channels < 1 || img.channels > 4 { - 131○
raise @types.LoadError::DecodeFailed( - 132
"decrypt_image_logistic: 通道数必须在1-4之间", - 133
) - 134
} - 135
let expected_size = img.width * img.height * img.channels - 136●
if img.data.length() != expected_size { - 137○
raise @types.LoadError::DecodeFailed( - 138
"decrypt_image_logistic: 数据大小不匹配", - 139
) - 140
} - 141
// XOR 加密是对称的,解密与加密使用相同的操作 - 142●
encrypt_image_logistic(img, key~, mu~) - 143
} - 144
- 145
///| - 146
/// 简单的 XOR 加密(使用密钥字节序列) - 147
pub fn encrypt_image_xor( - 148
img : @types.Image, - 149
key : String, - 150
) -> @types.Image raise @types.LoadError { - 151
// 输入验证 - 152●
if img.width <= 0 || img.height <= 0 { - 153●
raise @types.LoadError::DecodeFailed( - 154
"encrypt_image_xor: 图像尺寸必须大于0", - 155
) - 156
} - 157
if img.channels < 1 || img.channels > 4 { - 158○
raise @types.LoadError::DecodeFailed( - 159
"encrypt_image_xor: 通道数必须在1-4之间", - 160
) - 161
} - 162
let expected_size = img.width * img.height * img.channels - 163●
if img.data.length() != expected_size { - 164○
raise @types.LoadError::DecodeFailed( - 165
"encrypt_image_xor: 数据大小不匹配", - 166
) - 167
} - 168●
if key.length() == 0 { - 169●
raise @types.LoadError::DecodeFailed( - 170
"encrypt_image_xor: 密钥不能为空", - 171
) - 172
} - 173
let w = img.width - 174
let h = img.height - 175
let channels = img.channels - 176
let total_pixels = w * h * channels - 177●
let key_bytes = @encoding/utf8.encode(key) - 178●
let key_len = key_bytes.length() - 179●
let out_data : Array[Byte] = Array::make(total_pixels, b'\x00') - 180
for i in 0..<total_pixels { - 181●
let key_byte = key_bytes[i % key_len].to_int() - 182●
let original = img.data[i].to_int() - 183
let encrypted = original ^ key_byte - 184●
out_data[i] = encrypted.to_byte() - 185
} - 186
@types.Image::{ - 187
width: w, - 188
height: h, - 189
channels, - 190●
data: Bytes::from_array(out_data), - 191
} - 192
} - 193
- 194
///| - 195
/// 简单的 XOR 解密 - 196
pub fn decrypt_image_xor( - 197
img : @types.Image, - 198
key : String, - 199
) -> @types.Image raise @types.LoadError { - 200
// 输入验证 - 201●
if img.width <= 0 || img.height <= 0 { - 202●
raise @types.LoadError::DecodeFailed( - 203
"decrypt_image_xor: 图像尺寸必须大于0", - 204
) - 205
} - 206
if img.channels < 1 || img.channels > 4 { - 207○
raise @types.LoadError::DecodeFailed( - 208
"decrypt_image_xor: 通道数必须在1-4之间", - 209
) - 210
} - 211
let expected_size = img.width * img.height * img.channels - 212●
if img.data.length() != expected_size { - 213○
raise @types.LoadError::DecodeFailed( - 214
"decrypt_image_xor: 数据大小不匹配", - 215
) - 216
} - 217●
if key.length() == 0 { - 218○
raise @types.LoadError::DecodeFailed( - 219
"decrypt_image_xor: 密钥不能为空", - 220
) - 221
} - 222●
encrypt_image_xor(img, key) - 223
} - 224
- 225
///| - 226
/// 计算图像直方图 - 227
fn compute_histogram(img : @types.Image) -> Array[Int32] { - 228●
let histogram : Array[Int32] = [] - 229
for _ in 0..<256 { - 230●
histogram.push(0) - 231
} - 232
let w = img.width - 233
let h = img.height - 234
let channels = img.channels - 235
for y in 0..<h { - 236●
for x in 0..<w { - 237●
let idx = (y * w + x) * channels - 238
for c in 0..<channels { - 239●
let val = img.data[idx + c].to_int() - 240
histogram[val] += 1 - 241
} - 242
} - 243
} - 244
histogram - 245
} - 246
- 247
///| - 248
/// 计算直方图熵(衡量加密质量,熵越高越好) - 249
pub fn histogram_entropy(img : @types.Image) -> Double raise @types.LoadError { - 250
// 输入验证 - 251●
if img.width <= 0 || img.height <= 0 { - 252●
raise @types.LoadError::DecodeFailed( - 253
"histogram_entropy: 图像尺寸必须大于0", - 254
) - 255
} - 256
if img.channels < 1 || img.channels > 4 { - 257○
raise @types.LoadError::DecodeFailed( - 258
"histogram_entropy: 通道数必须在1-4之间", - 259
) - 260
} - 261
let expected_size = img.width * img.height * img.channels - 262●
if img.data.length() != expected_size { - 263●
raise @types.LoadError::DecodeFailed( - 264
"histogram_entropy: 数据大小不匹配", - 265
) - 266
} - 267●
let histogram = compute_histogram(img) - 268
let w = img.width - 269
let h = img.height - 270
let channels = img.channels - 271●
let total = (w * h * channels).to_double() - 272
let mut entropy = 0.0 - 273
for count in histogram { - 274●
if count > 0 { - 275●
let p = count.to_double() / total - 276●
entropy -= p * @math.log2(p) - 277
} - 278
} - 279
entropy - 280
} - 281
- 282
///| - 283
/// 计算相邻像素相关性(加密后应接近 0) - 284
pub fn adjacent_correlation( - 285
img : @types.Image, - 286
direction~ : String = "horizontal", - 287
) -> Double raise @types.LoadError { - 288
// 输入验证 - 289●
if img.width <= 0 || img.height <= 0 { - 290●
raise @types.LoadError::DecodeFailed( - 291
"adjacent_correlation: 图像尺寸必须大于0", - 292
) - 293
} - 294
if img.channels < 1 || img.channels > 4 { - 295○
raise @types.LoadError::DecodeFailed( - 296
"adjacent_correlation: 通道数必须在1-4之间", - 297
) - 298
} - 299
let expected_size = img.width * img.height * img.channels - 300●
if img.data.length() != expected_size { - 301●
raise @types.LoadError::DecodeFailed( - 302
"adjacent_correlation: 数据大小不匹配", - 303
) - 304
} - 305
let dir = direction - 306
let w = img.width - 307
let h = img.height - 308
let channels = img.channels - 309
// 采样点数量 - 310◐
let sample_count = if 1000 < w * h { 1000 } else { w * h } - 311
let mut sum_x = 0.0 - 312
let mut sum_y = 0.0 - 313
let mut sum_xy = 0.0 - 314
let mut sum_x2 = 0.0 - 315
let mut sum_y2 = 0.0 - 316
let mut count = 0 - 317
let mut seed = 12345 - 318
for _ in 0..<sample_count { - 319
// 随机选择像素位置 - 320●
let (next, value) = lcg_next(seed) - 321
seed = next - 322
let x = value % w - 323●
let (next2, value2) = lcg_next(seed) - 324
seed = next2 - 325
let y = value2 % h - 326
let idx = (y * w + x) * channels - 327●
let pixel1 = img.data[idx].to_double() - 328
// 相邻像素 - 329●
let pixel2 = match dir { - 330
"horizontal" => - 331●
if x + 1 < w { - 332●
img.data[(y * w + x + 1) * channels].to_double() - 333
} else { - 334○
img.data[idx].to_double() - 335
} - 336
"vertical" => - 337●
if y + 1 < h { - 338●
img.data[((y + 1) * w + x) * channels].to_double() - 339
} else { - 340●
img.data[idx].to_double() - 341
} - 342
"diagonal" => - 343●
if x + 1 < w && y + 1 < h { - 344●
img.data[((y + 1) * w + x + 1) * channels].to_double() - 345
} else { - 346●
img.data[idx].to_double() - 347
} - 348●
_ => img.data[idx].to_double() - 349
} - 350
sum_x += pixel1 - 351
sum_y += pixel2 - 352
sum_xy += pixel1 * pixel2 - 353
sum_x2 += pixel1 * pixel1 - 354
sum_y2 += pixel2 * pixel2 - 355
count += 1 - 356
} - 357
if count == 0 { - 358○
return 0.0 - 359
} - 360●
let n = count.to_double() - 361
let mean_x = sum_x / n - 362
let mean_y = sum_y / n - 363
let cov_xy = sum_xy / n - mean_x * mean_y - 364
let var_x = sum_x2 / n - mean_x * mean_x - 365
let var_y = sum_y2 / n - mean_y * mean_y - 366●
let denom = (var_x * var_y).sqrt() - 367
if denom < 0.000001 { - 368●
return 0.0 - 369
} - 370
cov_xy / denom - 371
} - 372
- 373
///| - 374
/// 加密质量评估 - 375
pub(all) struct EncryptionQuality { - 376
entropy : Double // 直方图熵(理想值 8) - 377
correlation_h : Double // 水平相关性(理想值 0) - 378
correlation_v : Double // 垂直相关性(理想值 0) - 379
correlation_d : Double // 对角相关性(理想值 0) - 380
npcr : Double // 像素变化率(Number of Pixel Change Rate) - 381
uaci : Double // 统一平均变化强度(Unified Average Changing Intensity) - 382
} - 383
- 384
///| - 385
/// 计算 NPCR(像素变化率) - 386
fn compute_npcr(img1 : @types.Image, img2 : @types.Image) -> Double { - 387●
if img1.width != img2.width || img1.height != img2.height { - 388○
return 0.0 - 389
} - 390
let w = img1.width - 391
let h = img1.height - 392●
let channels = img1.channels.min(img2.channels) - 393
let total = w * h * channels - 394
let mut changed = 0 - 395
for i in 0..<total { - 396●
if img1.data[i] != img2.data[i] { - 397●
changed += 1 - 398
} - 399
} - 400●
changed.to_double() / total.to_double() * 100.0 - 401
} - 402
- 403
///| - 404
/// 计算 UACI(统一平均变化强度) - 405
fn compute_uaci(img1 : @types.Image, img2 : @types.Image) -> Double { - 406●
if img1.width != img2.width || img1.height != img2.height { - 407○
return 0.0 - 408
} - 409
let w = img1.width - 410
let h = img1.height - 411●
let channels = img1.channels.min(img2.channels) - 412
let total = w * h * channels - 413
let mut sum = 0.0 - 414
for i in 0..<total { - 415●
let diff = (img1.data[i].to_int() - img2.data[i].to_int()).abs().to_double() - 416
sum += diff / 255.0 - 417
} - 418●
sum / total.to_double() * 100.0 - 419
} - 420
- 421
///| - 422
/// 评估加密质量 - 423
pub fn evaluate_encryption_quality( - 424
original : @types.Image, - 425
encrypted : @types.Image, - 426
) -> EncryptionQuality raise @types.LoadError { - 427
// 输入验证 - 428●
if original.width <= 0 || original.height <= 0 { - 429●
raise @types.LoadError::DecodeFailed( - 430
"evaluate_encryption_quality: 图像尺寸必须大于0", - 431
) - 432
} - 433
if original.channels < 1 || original.channels > 4 { - 434○
raise @types.LoadError::DecodeFailed( - 435
"evaluate_encryption_quality: 通道数必须在1-4之间", - 436
) - 437
} - 438
let expected_size = original.width * original.height * original.channels - 439●
if original.data.length() != expected_size { - 440●
raise @types.LoadError::DecodeFailed( - 441
"evaluate_encryption_quality: 数据大小不匹配", - 442
) - 443
} - 444
if encrypted.width <= 0 || encrypted.height <= 0 { - 445○
raise @types.LoadError::DecodeFailed( - 446
"evaluate_encryption_quality: 图像尺寸必须大于0", - 447
) - 448
} - 449
if encrypted.channels < 1 || encrypted.channels > 4 { - 450○
raise @types.LoadError::DecodeFailed( - 451
"evaluate_encryption_quality: 通道数必须在1-4之间", - 452
) - 453
} - 454
let expected_size2 = encrypted.width * encrypted.height * encrypted.channels - 455●
if encrypted.data.length() != expected_size2 { - 456●
raise @types.LoadError::DecodeFailed( - 457
"evaluate_encryption_quality: 数据大小不匹配", - 458
) - 459
} - 460●
let entropy = histogram_entropy(encrypted) - 461●
let corr_h = adjacent_correlation(encrypted, direction="horizontal") - 462●
let corr_v = adjacent_correlation(encrypted, direction="vertical") - 463●
let corr_d = adjacent_correlation(encrypted, direction="diagonal") - 464●
let npcr = compute_npcr(original, encrypted) - 465●
let uaci = compute_uaci(original, encrypted) - 466
EncryptionQuality::{ - 467
entropy, - 468
correlation_h: corr_h, - 469
correlation_v: corr_v, - 470
correlation_d: corr_d, - 471
npcr, - 472
uaci, - 473
} - 474
} - 475
- 476
///| - 477
/// 密钥敏感性测试(微小密钥变化导致完全不同的密文) - 478
pub fn key_sensitivity_test( - 479
img : @types.Image, - 480
key : Double, - 481
delta~ : Double = 0.0000001, - 482
) -> Double raise @types.LoadError { - 483
// 输入验证 - 484●
if img.width <= 0 || img.height <= 0 { - 485●
raise @types.LoadError::DecodeFailed( - 486
"key_sensitivity_test: 图像尺寸必须大于0", - 487
) - 488
} - 489
if img.channels < 1 || img.channels > 4 { - 490○
raise @types.LoadError::DecodeFailed( - 491
"key_sensitivity_test: 通道数必须在1-4之间", - 492
) - 493
} - 494
let expected_size = img.width * img.height * img.channels - 495●
if img.data.length() != expected_size { - 496●
raise @types.LoadError::DecodeFailed( - 497
"key_sensitivity_test: 数据大小不匹配", - 498
) - 499
} - 500
let d = delta - 501●
let encrypted1 = encrypt_image_logistic(img, key~, mu=3.99) - 502●
let encrypted2 = encrypt_image_logistic(img, key=key + d, mu=3.99) - 503●
compute_npcr(encrypted1, encrypted2) - 504
} - 505
- 506
///| - 507
/// 批量加密 - 508
pub fn batch_encrypt( - 509
images : Array[@types.Image], - 510
key : Double, - 511
) -> Array[@types.Image] raise @types.LoadError { - 512
// 输入验证 - 513●
for img in images { - 514●
if img.width <= 0 || img.height <= 0 { - 515○
raise @types.LoadError::DecodeFailed( - 516
"batch_encrypt: 图像尺寸必须大于0", - 517
) - 518
} - 519
if img.channels < 1 || img.channels > 4 { - 520○
raise @types.LoadError::DecodeFailed( - 521
"batch_encrypt: 通道数必须在1-4之间", - 522
) - 523
} - 524
let expected_size = img.width * img.height * img.channels - 525●
if img.data.length() != expected_size { - 526●
raise @types.LoadError::DecodeFailed( - 527
"batch_encrypt: 数据大小不匹配", - 528
) - 529
} - 530
} - 531
let result : Array[@types.Image] = [] - 532
for img in images { - 533●
result.push(encrypt_image_logistic(img, key~, mu=3.99)) - 534
} - 535
result - 536
}