-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
88 lines (64 loc) · 3.23 KB
/
analysis.py
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
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from main import get_image_data
from module.cryptography import encrypt_image_3d
from module.analysis_util import calculate_entropy, cross_correlation, spatial_autocorrelation, chi_square_test, measure_key_sensitivity, modify_image_pixels
PIXEL_RANGE = 4096 # 像素值範圍
SEED_LEN = 16
PLAIN_IMAGE_PATH = "./fig/1.png"
# plot image and histogram
def display_analysis_results(plain_image, encrypted_image):
fig, axs = plt.subplots(1, 4, figsize=(15, 5))
fig.suptitle("Image and Histogram Comparison")
# Normalize encrypted image data to [0, 1]
encrypted_image_display = (encrypted_image / encrypted_image.max() * 255).astype(np.uint8)
axs[0].set_title("Plain Image")
axs[0].imshow(plain_image, cmap='gray')
axs[0].axis('off')
axs[1].set_title("Plain Image Histogram")
axs[1].hist(np.array(plain_image).flatten(), bins=4096, range=(0, 4095), color='blue')
axs[2].set_title("Encrypted Image")
axs[2].imshow(encrypted_image_display, cmap='gray')
axs[2].axis('off')
axs[3].set_title("Encrypted Image Histogram")
axs[3].hist(np.array(encrypted_image).flatten(), bins=4096, range=(0, 4095), color='blue')
plt.tight_layout()
print("Displaying results...")
plt.show()
if __name__ == "__main__":
plain_image = Image.open(PLAIN_IMAGE_PATH)
image_array, sbox, chaotic_mask_3d, sbox_2, chaotic_mask_3d_2 = get_image_data(plain_image)
print("Image data loaded successfully.")
# 加密圖像
encrypted_image = encrypt_image_3d(image_array, sbox, chaotic_mask_3d)
encrypted_array = np.array(encrypted_image)
print("Image encrypted successfully.")
print("\n------------- Analysis Result -------------")
# 計算圖像的熵
entropy = calculate_entropy(image_array)
print("Entropy:", entropy)
# 計算圖像的交叉相關性
cross_corr = cross_correlation(image_array, encrypted_array)
# 計算圖像的空間自相關性
autocorr_h, autocorr_v, autocorr_d = spatial_autocorrelation(encrypted_array)
print("Horizontal spatial autocorrelation:", autocorr_h)
print("Vertical spatial autocorrelation:", autocorr_v)
print("Diagonal spatial autocorrelation:", autocorr_d)
# 直方圖和卡方檢驗
chi2, p = chi_square_test(encrypted_array)
print("Chi-square value:", chi2)
print("p-value:", p)
# 計算 Key Sensitivity 的 npcr 以及 uaci
# 測試 sbox sensitivity
npcr_rate, uaci_value = measure_key_sensitivity(encrypted_image, image_array, sbox_2, chaotic_mask_3d)
print("\n----------- Key Sensitivity Analysis Results -----------")
print("NPCR (Normalized Pixel Change Rate):", npcr_rate)
print("UACI (Unified Average Changing Intensity):", uaci_value)
# 測試 chaotic map sensitivity
npcr_rate, uaci_value = measure_key_sensitivity(encrypted_image, image_array, sbox, chaotic_mask_3d_2)
print("NPCR (Normalized Pixel Change Rate):", npcr_rate)
print("UACI (Unified Average Changing Intensity):", uaci_value)
print("----------------------------------------------------------")
# 顯示加密前後的圖片和直方圖
display_analysis_results(image_array, encrypted_image)