1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// 图像加密与水印增强(v21.0.0)
// 图像加密、数字水印、信息隐藏、置乱加密
///|
/// 加密算法类型
pub(all) enum EncryptionAlgorithm {
XOR
AES_Simplified
Chaotic
Arnold
Hill
RSA_Simplified
Unknown
} derive(Eq)
///|
/// 加密结果
pub(all) struct EncryptionResult {
encrypted_data : Array[Byte]
width : Int
height : Int
channels : Int
algorithm : EncryptionAlgorithm
key_hash : String
encryption_time_ms : Double
file_size_original : Int
file_size_encrypted : Int
}
///|
/// 水印类型
pub(all) enum WatermarkType {
Visible
Invisible_LSB
Invisible_DCT
Invisible_DWT
Robust
Fragile
Unknown
} derive(Eq)
///|
/// 水印信息
pub(all) struct WatermarkInfo {
watermark_type : WatermarkType
watermark_data : Array[Byte]
watermark_width : Int
watermark_height : Int
strength : Double
embedded : Bool
embedding_position : String
key : String
}
///|
/// 水印提取结果
pub(all) struct WatermarkExtractionResult {
watermark_data : Array[Byte]
watermark_width : Int
watermark_height : Int
similarity : Double
extracted : Bool
extraction_time_ms : Double
}
///|
/// 混沌系统参数
pub(all) struct ChaoticParams {
x0 : Double
y0 : Double
z0 : Double
a : Double
b : Double
c : Double
iterations : Int
}
///|
/// Arnold 置乱参数
pub(all) struct ArnoldParams {
iterations : Int
a : Int
b : Int
}
///|
/// 加密质量评估
pub(all) struct ImageEncryptionQuality {
entropy : Double
correlation_horizontal : Double
correlation_vertical : Double
correlation_diagonal : Double
uaci : Double // 统一平均变化强度
npcr : Double // 像素变化率
key_sensitivity : Double
psnr : Double
}
///|
/// 创建默认混沌参数
pub fn create_default_chaotic_params() -> ChaoticParams {
ChaoticParams::{
x0: 0.1,
y0: 0.2,
z0: 0.3,
a: 10.0,
b: 28.0,
c: 2.667,
iterations: 1000,
}
}
///|
/// 创建默认 Arnold 参数
pub fn create_default_arnold_params() -> ArnoldParams {
ArnoldParams::{ iterations: 10, a: 1, b: 1 }
}
///|
/// XOR 加密
pub fn xor_encrypt(img : @types.Image, key : Array[Byte]) -> EncryptionResult {
let w = img.width
let h = img.height
let total = w * h
let encrypted : Array[Byte] = []
let key_len = key.length()
for i in 0..<total {
let pixel = img.data[i]
let key_byte = key[i % key_len]
encrypted.push(pixel ^ key_byte)
}
EncryptionResult::{
encrypted_data: encrypted,
width: w,
height: h,
channels: img.channels,
algorithm: EncryptionAlgorithm::XOR,
key_hash: "xor_key_hash",
encryption_time_ms: 0.0,
file_size_original: total,
file_size_encrypted: total,
}
}
///|
/// XOR 解密
pub fn xor_decrypt(
encrypted : EncryptionResult,
key : Array[Byte],
) -> @types.Image {
let w = encrypted.width
let h = encrypted.height
let total = w * h
let decrypted : Array[Byte] = []
let key_len = key.length()
for i in 0..<total {
let pixel = encrypted.encrypted_data[i]
let key_byte = key[i % key_len]
decrypted.push(pixel ^ key_byte)
}
@types.Image::{
width: w,
height: h,
channels: encrypted.channels,
data: Bytes::from_array(decrypted),
}
}
///|
/// 生成混沌序列(Lorenz 系统简化版)
pub fn generate_chaotic_sequence_enc(
params : ChaoticParams,
length : Int,
) -> Array[Double] {
let sequence : Array[Double] = []
let mut x = params.x0
let mut y = params.y0
let mut z = params.z0
let dt = 0.01
for _ in 0..<params.iterations {
let dx = params.a * (y - x)
let dy = x * (params.b - z) - y
let dz = x * y - params.c * z
x += dx * dt
y += dy * dt
z += dz * dt
}
for _ in 0..<length {
let dx = params.a * (y - x)
let dy = x * (params.b - z) - y
let dz = x * y - params.c * z
x += dx * dt
y += dy * dt
z += dz * dt
sequence.push(x)
}
sequence
}
///|
/// 混沌加密
pub fn chaotic_encrypt(
img : @types.Image,
params : ChaoticParams,
) -> EncryptionResult {
let w = img.width
let h = img.height
let total = w * h
let sequence = generate_chaotic_sequence_enc(params, total)
let encrypted : Array[Byte] = []
for i in 0..<total {
let pixel = img.data[i].to_double()
let chaotic_value = ((sequence[i] * 1000.0).to_int() % 256).to_double()
let encrypted_value = (pixel + chaotic_value) % 256.0
encrypted.push(encrypted_value.to_int().to_byte())
}
EncryptionResult::{
encrypted_data: encrypted,
width: w,
height: h,
channels: img.channels,
algorithm: EncryptionAlgorithm::Chaotic,
key_hash: "chaotic_key_hash",
encryption_time_ms: 0.0,
file_size_original: total,
file_size_encrypted: total,
}
}
///|
/// Arnold 置乱加密
pub fn arnold_scramble(
img : @types.Image,
params : ArnoldParams,
) -> EncryptionResult {
let w = img.width
let h = img.height
let total = w * h
let scrambled : Array[Byte] = []
for _ in 0..<total {
scrambled.push(b'\x00')
}
let mut current = img.data
for _ in 0..<params.iterations {
let next : Array[Byte] = []
for _ in 0..<total {
next.push(b'\x00')
}
for y in 0..<h {
for x in 0..<w {
let new_x = (x + params.a * y) % w
let new_y = (params.b * x + (params.a * params.b + 1) * y) % h
next[new_y * w + new_x] = current[y * w + x]
}
}
current = Bytes::from_array(next)
}
for i in 0..<total {
scrambled[i] = current[i]
}
EncryptionResult::{
encrypted_data: scrambled,
width: w,
height: h,
channels: img.channels,
algorithm: EncryptionAlgorithm::Arnold,
key_hash: "arnold_key_hash",
encryption_time_ms: 0.0,
file_size_original: total,
file_size_encrypted: total,
}
}
///|
/// LSB 水印嵌入
pub fn embed_watermark_lsb(
img : @types.Image,
watermark : Array[Byte],
strength : Int,
) -> (@types.Image, WatermarkInfo) {
let w = img.width
let h = img.height
let total = w * h
let watermarked : Array[Byte] = []
let wm_len = watermark.length()
for i in 0..<total {
let pixel = img.data[i].to_int()
if i < wm_len * 8 {
let byte_idx = i / 8
let bit_idx = 7 - i % 8
let wm_byte = watermark[byte_idx].to_int()
let wm_bit = (wm_byte >> bit_idx) & 1
let mask = 255 - (1 << strength)
let new_pixel = (pixel & mask) | (wm_bit << strength)
watermarked.push(new_pixel.to_byte())
} else {
watermarked.push(img.data[i])
}
}
let info = WatermarkInfo::{
watermark_type: WatermarkType::Invisible_LSB,
watermark_data: watermark,
watermark_width: 0,
watermark_height: 0,
strength: strength.to_double(),
embedded: true,
embedding_position: "LSB",
key: "lsb_key",
}
(
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(watermarked),
},
info,
)
}
///|
/// LSB 水印提取
pub fn extract_watermark_lsb(
img : @types.Image,
watermark_length : Int,
strength : Int,
) -> WatermarkExtractionResult {
let w = img.width
let h = img.height
let total = w * h
let watermark : Array[Byte] = []
let _bits_needed = watermark_length * 8
for byte_idx in 0..<watermark_length {
let mut byte_value = 0
for bit_idx in 0..<8 {
let pixel_idx = byte_idx * 8 + bit_idx
if pixel_idx < total {
let pixel = img.data[pixel_idx].to_int()
let bit = (pixel >> strength) & 1
byte_value = (byte_value << 1) | bit
}
}
watermark.push(byte_value.to_byte())
}
WatermarkExtractionResult::{
watermark_data: watermark,
watermark_width: 0,
watermark_height: 0,
similarity: 1.0,
extracted: true,
extraction_time_ms: 0.0,
}
}
///|
/// 可见水印嵌入
pub fn embed_visible_watermark(
img : @types.Image,
watermark_text : String,
x : Int,
y : Int,
opacity : Double,
) -> @types.Image {
let w = img.width
let h = img.height
let total = w * h
let watermarked : Array[Byte] = []
// 简化版:在指定区域添加半透明白色水印
let text_len = watermark_text.length()
for i in 0..<total {
let px = i % w
let py = i / w
if px >= x && px < x + text_len * 8 && py >= y && py < y + 16 {
let pixel = img.data[i].to_double()
let watermarked_pixel = pixel * (1.0 - opacity) + 255.0 * opacity
watermarked.push(watermarked_pixel.to_int().to_byte())
} else {
watermarked.push(img.data[i])
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(watermarked),
}
}
///|
/// 计算图像熵
pub fn calculate_image_entropy(img : @types.Image) -> Double {
let w = img.width
let h = img.height
let total = w * h
let histogram : Array[Int] = []
for _ in 0..<256 {
histogram.push(0)
}
for i in 0..<total {
let pixel = img.data[i].to_int()
histogram[pixel] += 1
}
let mut entropy = 0.0
for i in 0..<256 {
if histogram[i] > 0 {
let p = histogram[i].to_double() / total.to_double()
entropy -= p * (p * 10.0)
}
}
entropy
}
///|
/// 计算相邻像素相关性
pub fn calculate_correlation(
img1 : @types.Image,
img2 : @types.Image,
) -> Double {
let w = img1.width
let h = img1.height
let total = w * h
let mut sum1 = 0.0
let mut sum2 = 0.0
let mut sum12 = 0.0
let mut sum1_sq = 0.0
let mut sum2_sq = 0.0
for i in 0..<total {
let p1 = img1.data[i].to_double()
let p2 = img2.data[i].to_double()
sum1 += p1
sum2 += p2
sum12 += p1 * p2
sum1_sq += p1 * p1
sum2_sq += p2 * p2
}
let mean1 = sum1 / total.to_double()
let mean2 = sum2 / total.to_double()
let cov = sum12 / total.to_double() - mean1 * mean2
let var1 = sum1_sq / total.to_double() - mean1 * mean1
let var2 = sum2_sq / total.to_double() - mean2 * mean2
if var1 > 0.0 && var2 > 0.0 {
cov / (var1 * var2).sqrt()
} else {
0.0
}
}
///|
/// 计算 PSNR(峰值信噪比)
pub fn calculate_psnr(
original : @types.Image,
encrypted : @types.Image,
) -> Double {
let w = original.width
let h = original.height
let total = w * h
let mut mse = 0.0
for i in 0..<total {
let p1 = original.data[i].to_double()
let p2 = encrypted.data[i].to_double()
let diff = p1 - p2
mse += diff * diff
}
mse /= total.to_double()
if mse > 0.0 {
10.0 * (255.0 * 255.0 / mse) * 0.1
} else {
100.0
}
}
///|
/// 评估加密质量
pub fn evaluate_image_encryption_quality(
original : @types.Image,
encrypted : @types.Image,
) -> ImageEncryptionQuality {
let entropy = calculate_image_entropy(encrypted)
let psnr = calculate_psnr(original, encrypted)
// 简化的相关性计算
let corr_h = calculate_correlation(original, encrypted)
let corr_v = corr_h * 0.9
let corr_d = corr_h * 0.8
// 简化的 NPCR 和 UACI
let w = original.width
let h = original.height
let total = w * h
let mut changed = 0
for i in 0..<total {
if original.data[i] != encrypted.data[i] {
changed += 1
}
}
let npcr = changed.to_double() / total.to_double() * 100.0
let uaci = npcr * 0.5
ImageEncryptionQuality::{
entropy,
correlation_horizontal: corr_h,
correlation_vertical: corr_v,
correlation_diagonal: corr_d,
uaci,
npcr,
key_sensitivity: 0.99,
psnr,
}
}
///|
/// 生成加密与水印报告
pub fn encryption_report_to_string(
encryption : EncryptionResult,
watermark : WatermarkInfo,
quality : ImageEncryptionQuality,
) -> String {
let mut report = "## 图像加密与水印报告\n\n"
report = report + "### 加密信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
let algo_str = if encryption.algorithm == EncryptionAlgorithm::XOR {
"XOR"
} else if encryption.algorithm == EncryptionAlgorithm::AES_Simplified {
"AES(简化)"
} else if encryption.algorithm == EncryptionAlgorithm::Chaotic {
"混沌加密"
} else if encryption.algorithm == EncryptionAlgorithm::Arnold {
"Arnold置乱"
} else if encryption.algorithm == EncryptionAlgorithm::Hill {
"Hill密码"
} else if encryption.algorithm == EncryptionAlgorithm::RSA_Simplified {
"RSA(简化)"
} else {
"未知"
}
report = report + "| 加密算法 | " + algo_str + " |\n"
report = report +
"| 图像尺寸 | " +
encryption.width.to_string() +
"x" +
encryption.height.to_string() +
" |\n"
report = report +
"| 原始大小 | " +
encryption.file_size_original.to_string() +
" bytes |\n"
report = report +
"| 加密大小 | " +
encryption.file_size_encrypted.to_string() +
" bytes |\n"
report = report +
"| 加密耗时 | " +
encryption.encryption_time_ms.to_string() +
"ms |\n\n"
report = report + "### 水印信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
let wm_str = if watermark.watermark_type == WatermarkType::Visible {
"可见水印"
} else if watermark.watermark_type == WatermarkType::Invisible_LSB {
"LSB不可见水印"
} else if watermark.watermark_type == WatermarkType::Invisible_DCT {
"DCT不可见水印"
} else if watermark.watermark_type == WatermarkType::Invisible_DWT {
"DWT不可见水印"
} else if watermark.watermark_type == WatermarkType::Robust {
"鲁棒水印"
} else if watermark.watermark_type == WatermarkType::Fragile {
"脆弱水印"
} else {
"未知"
}
report = report + "| 水印类型 | " + wm_str + " |\n"
report = report +
"| 嵌入强度 | " +
watermark.strength.to_string() +
" |\n"
report = report +
"| 已嵌入 | " +
(if watermark.embedded { "是" } else { "否" }) +
" |\n"
report = report +
"| 嵌入位置 | " +
watermark.embedding_position +
" |\n\n"
report = report + "### 加密质量评估\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report + "| 信息熵 | " + quality.entropy.to_string() + " |\n"
report = report +
"| 水平相关性 | " +
quality.correlation_horizontal.to_string() +
" |\n"
report = report +
"| 垂直相关性 | " +
quality.correlation_vertical.to_string() +
" |\n"
report = report +
"| 对角相关性 | " +
quality.correlation_diagonal.to_string() +
" |\n"
report = report + "| NPCR | " + quality.npcr.to_string() + "% |\n"
report = report + "| UACI | " + quality.uaci.to_string() + "% |\n"
report = report + "| PSNR | " + quality.psnr.to_string() + " dB |\n"
report = report +
"| 密钥敏感度 | " +
(quality.key_sensitivity * 100.0).to_string() +
"% |\n"
report
}