• 混沌系统在图像加密中的应用(图像加密算法案例)


    前言

    本节内容我们将学习图像加密的基础理论

    一、什么是图像加密

    图像加密是一种保护图像数据的安全方法,通过对图像内容进行转换和操作,将其转换为一种难以理解或解读的形式,从而防止未经授权的人访问或理解图像的内容。加密的目的是确保图像在传输、存储或处理过程中不被未经授权的人获得敏感信息

    图像加密算法使用密码学的概念和技术,通过对图像进行变换、混淆、扩散和扰乱等方法,将原始图像转化为密文图像。这使得只有具备解密密钥或密码的人才能够还原出原始图像。

    二、什么是数字图像

    数字图像是通过采集、处理和存储方式将视觉信息转换为数字形式的图像。它是由一系列的数字值(像素)组成的二维矩阵,每个像素值都在[0,255]之间,0表示黑色,255表示白色。数字图像可以是黑白图像(灰度图像),其中每个像素的值表示亮度,也可以是彩色图像,其中每个像素的值表示颜色或颜色的组合。

    三、混沌系统如何加密数字图像

    混沌系统是一种具有高度敏感依赖于初始条件的非线性系统,它在数字图像加密中可以起到有效的保护和加密作用,一般我们用异或的方法将提取后混沌序列与数字图像的像素值异或,即可得到加密图像,如果要解密,则异或两次就可以解密。

    当然,这只是最简单的加密方法,比较复杂的加密方法还包括置乱图像、旋转图像,以及用不同的混沌系统进行扩散等等

    1.图像置乱

    图像置乱是一种用于对图像数据进行加密或混淆的算法或方法。它通过改变图像中像素的位置或值来扰乱图像的结构,使其难以被理解或还原。图像置乱通常用于保护敏感图像的隐私、增加图像的安全性,或者对图像进行加密传输。这里我们来看一下比较经典的置乱方法。

    Arnold置乱(Arnold’s Cat Map)是一种经典的图像置乱算法,它由美国数学家Vladimir Arnold于1968年提出。该算法通过对输入的二维图像进行一系列映射操作来实现图像的混乱化。基本思想是通过迭代映射周期性地重排图像中的像素,从而达到混淆图像的效果。它被广泛用于图像加密、图像压缩以及图像处理等领域。
    算法的实现步骤如下:

    (1)输入一张二维图像。对于灰度图像,每个像素点可以表示为一个灰度值;对于彩色图像,每个像素点可以由RGB或者其他颜色表示方法表示。

    (2)定义置乱的参数,通常为两个正整数nm,表示图像的尺寸。

    (3)利用Arnold置乱算法对图像进行迭代映射。映射的步骤如下:

    对于每个像素点(x,y),将其映射到位置((x + y) mod n, (x + 2y) mod m)上。这一步相当于在图像中循环移动像素。一般来说m=n,所以上述公式可以写成:
    在这里插入图片描述
    重复上述映射步骤k次,其中k为正整数。重复映射的次数越多,图像置乱的效果越好。

    (4)输出置乱后的图像。

    需要注意的是,Arnold置乱算法是可逆的,也就是说可以通过逆向映射操作将置乱后的图像还原到原始图像,逆置乱公式:
    在这里插入图片描述
    这里我们设置迭代次数为10次,可以看出,加密图像已经失去原有信息,感兴趣的可以自行设定加密次数
    在这里插入图片描述

    import numpy as np
    import cv2
    
    
    def arnold_encrypt(image, iterations):
        height, width = image.shape
        encrypted_image = np.zeros_like(image, dtype=np.uint8)
    
        for i in range(iterations):
            for y in range(height):
                for x in range(width):
                    new_y = (2 * y + x) % height
                    new_x = (y + x) % width
                    encrypted_image[new_y, new_x] = image[y, x]
            image = encrypted_image.copy()
    
        return encrypted_image
    
    
    def arnold_decrypt(encrypted_image, iterations):
        height, width = encrypted_image.shape
        decrypted_image = np.zeros_like(encrypted_image, dtype=np.uint8)
    
        for i in range(iterations):
            for y in range(height):
                for x in range(width):
                    old_y = (y - x) % height
                    old_x = (2 * x - y) % width
                    decrypted_image[old_y, old_x] = encrypted_image[y, x]
            encrypted_image = decrypted_image.copy()
    
        return decrypted_image
    
    
    # 读取图像
    image_path = 'lena.png'
    image = cv2.imread(image_path, 0)
    
    
    # 加密图像
    iterations = 10
    encrypted_image = arnold_encrypt(image, iterations)
    
    # 解密图像
    decrypted_image = arnold_decrypt(encrypted_image, iterations)
    
    # 显示图像
    cv2.imshow("Original Image", image)
    cv2.imshow("Encrypted Image", encrypted_image)
    cv2.imshow("Decrypted Image", decrypted_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    • 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

    2.图像扩散

    图像加密中的扩散算法有助于增强图像加密的安全性和隐蔽性。它们通过改变原始图像的像素值,使其在加密后的图像中更难以被分析、破解或恢复原始内容。也就是说:图像置乱用来打乱像素位置,图像扩散用来改变像素值。

    扩散算法通过异或运算借助于伪随机矩阵 X 将明文图像 P 转换为矩阵 D,公式如下:
    D = P XOR ⁡   X   D=P\operatorname{XOR}\text{ }X\text{ } D=PXOR X 
    其中,XOR 就表示异或,要注意的是,文图像 P 如果分辨率是256x256,那么 X 也要是256x256,所以,X的一维长度就是256*256=65536,也就是混沌系统要迭代至少65536次。解密算法和加密是反向的,公式如下:
    P = D XOR ⁡   X   P=D\operatorname{XOR}\text{ }X\text{ } P=DXOR X 

    下面是扩散算法的结果,可以看出加密图像已经失去原有信息,感兴趣的可以自行设定混沌系统参数
    在这里插入图片描述

    import numpy as np
    import cv2
    
    
    def logistic_map(x, r, num_iterations):
        """Logistic Map equation"""
        X = []
        for i in range(num_iterations):
            x1 = r * x * (1 - x)
            x = x1
            X.append(x)
        return X
    
    
    
    def encrypt_image(r):
        """Encrypts an image using logistic map and XOR algorithm"""
        # Generate chaos key
        height, width = image.shape
        key = np.zeros((height, width), dtype=np.float64)
        chaotic_sequence = logistic_map(0.5, r, height * width)
        chaotic_sequence = np.reshape(chaotic_sequence, (height, width))
        key = chaotic_sequence.copy()
    
        # Perform XOR encryption
        encrypted_image = cv2.bitwise_xor(image, np.uint8(key * 255))
        return encrypted_image
    
    
    def decrypt_image(encrypted_image, r):
        """Decrypts an encrypted image using logistic map and XOR algorithm"""
        height, width = encrypted_image.shape
    
        # Generate chaos key
        key = np.zeros((height, width), dtype=np.float64)
        chaotic_sequence = logistic_map(0.5, r, height * width)
        chaotic_sequence = np.reshape(chaotic_sequence, (height, width))
        key = chaotic_sequence.copy()
    
        # Perform XOR decryption
        decrypted_image = cv2.bitwise_xor(encrypted_image, np.uint8(key * 255))
        return decrypted_image
    
    
    # Example usage
    image_path = 'lena.png'
    image = cv2.imread(image_path,0)
    r = 3.9
    num_iterations = 100
    encrypted_image = encrypt_image(r)
    decrypted_image = decrypt_image(encrypted_image, r)
    
    # Display the original, encrypted, and decrypted images
    cv2.imshow('Original Image', image)
    cv2.imshow('Encrypted Image', encrypted_image)
    cv2.imshow('Decrypted Image', decrypted_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    • 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
  • 相关阅读:
    10.正则表达式匹配
    DPDK性能影响因素分析
    Python连本地MySQL接数据库
    第二章:初始Ajax
    AM@两种余项型泰勒公式的对比和总结@常用函数的麦克劳林公式
    dotnet平台Http消息处理者工厂
    QT 自定义信号和槽 学习笔记
    4种Javascript类型检测的方式
    PSI-BLAST位点特异性矩阵PSSM和ProteinMPNN中氨基酸顺序映射
    Spring Boot全面总结(超详细,建议收藏)
  • 原文地址:https://blog.csdn.net/weixin_42207434/article/details/132816525