Opencv实现图像的加密解密

1、基础:基于异或操作实现图像加密解密

一般情况下,图像的加密和解密过程是通过按位异或运算实现的。将原始图像与密钥图像进行按位异或,可以实现加密,将加密后的图像与密钥图像再进行按位异或可以实现解密过程。
Opencv-python代码实现

import cv2
import numpy as np

demo = cv2.imread("E:\matlab_file\picture\picture.jpg", 0)
r, c = demo.shape
key = np.random.randint(0, 256, size=(r, c), dtype=np.uint8)  # 生成随机的密钥图像
cv2.imwrite("E:\matlab_file\picture\key.jpg", key)   # 保存密匙图像

cv2.imshow("demo", demo)  # 显示原始图像
cv2.imshow("key", key)  # 显示密钥图像

encryption = cv2.bitwise_xor(demo, key)  # 加密
cv2.imwrite("E:\matlab_file\picture\encryption.jpg", encryption)     # 保存加密后的图像
decryption = cv2.bitwise_xor(encryption, key)  # 解密
cv2.imwrite("E:\matlab_file\picture\decryption.jpg", decryption) # 保存解密后的图像

cv2.imshow("encryption", encryption)  # 显示密文图像
cv2.imshow("decryption", decryption)  # 显示解密后的图像

cv2.waitKey(-1)
cv2.destroyAllWindows()

效果展示:
原图:
原图
密匙:
密匙
加密后:
加密后
解密后:
解密

2、进阶:基于混沌序列构成异或模板实现图像加密解密

混沌系统是非线性的系统,表现出非常复杂的伪随机性,符合混淆规则。它对初始条件和控制参数非常敏感,任何微小的初始偏差都会被指数式放大,符合扩散规则。同时,它又是确定性的,可由非线性系统的方程、参数和初始条件完全确定。因此,初始状态和少量参数的变化就可以产生满足密码学基本特征的混沌密码序列,将混沌理论与加密技术相结合,可以形成良好的图像加密系统。目前常用于图像加密的混沌系统有:Logistic混沌映射、Chebychev映射、分段线形混沌映射、Cubic映射、标准映射、Henon映射、Lorenz混沌映射、蔡氏混沌、Rossler混沌系统、二维Sinai映射、Chen’s混沌系统等。在基于混沌的图像加密方法中,有的利用混沌系统产生伪随机序列,进行序列密码形式的加密。有些利用混沌的遍历性,对产生的伪随机序列作处理,得到像素置乱后的位置,然后对像素位置进行置乱。有些利用一些混沌计算表达式可逆的特点,将像素值代入混沌计算式以进行像素的代换和扩散。与传统加密算法相比,混沌图像加密算法密钥空间大,实现简单,加密速度快。但是,基于混沌的图像加密存在以下不足:①计算机的有限精度可能导致混沌序列的周期比较短,随机性不好。 ②现有的混沌加密技术大都基于一维或二维混沌系统,容易受到相空间重构方法攻击。 ③一些混沌加密算法采用了形式比较复杂的混沌系统,速度较慢,无法实现实时的加密。
本文使用复合混沌加密算法对图像金星加密解密,详细原理见:基于复合混沌系统的数字图像加密方法

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

"""
@Author       :  LitraLin
@File         : Cryption.py
@CreateTime   : 2021/10/07
@Description  : Compound chaos Encryption and Decryption of image
"""

import cv2
import math
import numpy as np


def int2bin8(x):                               # 整型转8位二进制
    result="";
    for i in range(8):
        y=x&(1)
        result+=str(y)
        x=x>>1
    return result[::-1]

def int2bin16(x):                              # 整型转8位二进制
    result="";
    for i in range(16):
        y=x&(1)
        result+=str(y)
        x=x>>1
    return result

def Encryption(img,j0,g0,x0,EncryptionImg):
    x = img.shape[0]
    y = img.shape[1]
    c = img.shape[2]
    g0 = int2bin16(g0)
    for s in range(x):
        for n in range(y):
            for z in range(c):
                m = int2bin8(img[s][n][z])                   # 像素值转八位二进制
                ans=""
                # print("ok")
                for i in range(8):
                    ri=int(g0[-1])                           # 取手摇密码机最后一位ri
                    qi=int(m[i])^ri                          # 与像素值异或得qi
                    xi = 1 - math.sqrt(abs(2 * x0 - 1))      # f1(x)混沌迭代
                    if qi==0:                                # 如果qi=0,则运用x0i+x1i=1;
                        xi=1-xi;
                    x0=xi                                    # xi迭代
                    t=int(g0[0])^int(g0[12])^int(g0[15])     # 本源多项式x^15+x^3+1
                    g0=str(t)+g0[0:-1]                       # gi迭代
                    ci=math.floor(xi*(2**j0))%2              # 非线性转换算子
                    ans+=str(ci)
                re=int(ans,2)
                EncryptionImg[s][n][z]=re                    # 写入新图像

def Decryption(EncryptionImg, j0, g0, x0, DecryptionImg):
    x = EncryptionImg.shape[0]
    y = EncryptionImg.shape[1]
    c = EncryptionImg.shape[2]
    g0 = int2bin16(g0)
    for s in range(x):
        for n in range(y):
            for z in range(c):
                cc = int2bin8(img[s][n][z])
                ans = ""
                # print("no")
                for i in range(8):
                    xi = 1 - math.sqrt(abs(2 * x0 - 1))
                    x0 = xi
                    ssi = math.floor(xi * (2 ** j0)) % 2
                    qi=1-(ssi^int(cc[i]))
                    ri = int(g0[-1])
                    mi=ri^qi
                    t = int(g0[0]) ^ int(g0[12]) ^ int(g0[15])
                    g0 = str(t) + g0[0:-1]
                    ans += str(mi)
                re = int(ans, 2)
                DecryptionImg[s][n][z] = re


if __name__ == "__main__":
    img = cv2.imread(r"E:\matlab_file\picture\Correlation_matrix.png", 1)                    # 读取原始图像

    EncryptionImg = np.zeros(img.shape, np.uint8)
    Encryption(img,10,30,0.123345,EncryptionImg)                                       # 加密
    cv2.imwrite(r"E:\matlab_file\picture\Correlation_matrix-EncryptionImg.png",EncryptionImg)  # 保存加密后的图像

    img = cv2.imread(r"E:\matlab_file\picture\Correlation_matrix-EncryptionImg.png", 1)        # 读取加密图像
    DecryptionImg = np.zeros(img.shape, np.uint8)
    Decryption(img, 10, 30, 0.123345, DecryptionImg)                                   # 解密
    cv2.imwrite(r"E:\matlab_file\picture\Correlation_matrix-DecryptionImg.png", DecryptionImg) # 保存解密后的图像

    cv2.waitKey(0)

结果展示:
原图:
原图
加密后:
加密后
解密后:
解密

猜你喜欢

转载自blog.csdn.net/qq_48314528/article/details/120635006