以下是一个使用 Python 实现超混沌 Lorenz 模型对图像进行加密和解密的示例代码:

import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
from PIL import Image

# 定义 Lorenz 系统的参数
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0

# 定义 Lorenz 系统的微分方程
def lorenz(t, s):
    x, y, z = s[0], s[1], s[2]
    dx_dt = sigma * (y - x)
    dy_dt = x * (rho - z) - y
    dz_dt = x * y - beta * z
    return [dx_dt, dy_dt, dz_dt]

# 加载原始图像
image_path = 'C:/Users/DELL/Desktop/1111/lena.png'
image = Image.open(image_path).convert('L')  # 转换为灰度图像
gray_image = np.array(image)

# 调整图像尺寸
height, width = gray_image.shape
new_height = height + (8 - height % 8) if height % 8 != 0 else height
new_width = width + (8 - width % 8) if width % 8 != 0 else width
resized_image = np.zeros((new_height, new_width), dtype=np.uint8)
resized_image[:height, :width] = gray_image

# 生成 Lorenz 系统的初始状态
initial_state = [1.0, 1.0, 1.0]

# 求解 Lorenz 系统的微分方程
sol = solve_ivp(lorenz, [0, 1e3], initial_state, t_eval=np.linspace(0, 1e3, new_width))

# 提取 Lorenz 系统的 z 分量作为密钥
key = sol.y[2, :].reshape((1, new_width))

# 加密图像
encrypted_image = np.bitwise_xor(resized_image, key)

# 解密图像
decrypted_image = np.bitwise_xor(encrypted_image, key)

# 显示图像和加密后的图像
fig, axes = plt.subplots(1, 2)
axes[0].imshow(resized_image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(encrypted_image, cmap='gray')
axes[1].set_title('Encrypted Image')
axes[1].axis('off')
plt.show()

# 保存加密和解密后的图像
encrypted_image_path = 'C:/Users/DELL/Desktop/1111/lena_encrypted.png'
Image.fromarray(encrypted_image).save(encrypted_image_path)

decrypted_image_path = 'C:/Users/DELL/Desktop/1111/lena_decrypted.png'
Image.fromarray(decrypted_image[:height, :width]).save(decrypted_image_path)

在上述代码中,首先加载原始图像,然后调整图像的尺寸以确保宽高都是 8 的倍数。接下来,定义 Lorenz 系统的微分方程并求解,提取 Lorenz 系统的 z 分量作为密钥。然后,使用密钥对图像进行异或运算,实现图像的加密。最后,再次使用密钥对加密图像进行异或运算,还原原始图像。代码中还包括显示加密后的图像和保存加密和解密后的图像的部分。

请注意,上述代码中的图像路径需要根据实际情况进行修改。同时,需要确保安装了必要的库(如 numpy、scipy、matplotlib 和 Pillow)来运行代码。

Python 实现超混沌 Lorenz 模型图像加密解密示例

原文地址: https://www.cveoy.top/t/topic/daaX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录