复合混沌加解密算法

复合混沌加解密算法——Python实现

最近网络安全老师布置的一个作业,就是编码实现复合混沌加解密算法。其实这个也是我们网络安全老师的一篇论文【基于复合混沌系统的数字图像加密研究】,这个算法被运用在有线电视机顶盒中给视频加密,我虽然之前都没了解过加解密算法,不过感觉这个还是很厉害的。实验课上给出的是MATLAB代码,这里我使用Python实现。

参考:

https://blog.csdn.net/qq_29941869/article/details/78792245

https://blog.csdn.net/qq_44711612/article/details/114858059

1. 配置OpenCV环境

  1. 确保已经安装Python环境,并且查看Python版本;使用cmd,输入“python -v”查看版本信息。image-20210327205528739

  2. 安装OpenCV,这是它的官网:http://opencv.org/,推荐使用迅雷下载,直接用浏览器下载的话太慢了。。。

  3. 安装python中需要的OpenCV库文件,http://www.lfd.uci.edu/~gohlke/pythonlibs/,进到这个网站,使用快捷键“Ctrl+F”搜索“opencv”,下载这个库文件。

  4. 点击后,进入到版本选择,可以看到,这里有多个版本。刚才第一步的时候,我查看到我的版本是64位,3.8.5,所以我这里选择红框中的第一个链接,这里也是推荐用迅雷下,感觉比浏览器下得要快。一定要选择正确的版本,才能装好,不要问为啥,因为我就是在这里弄错了。

    image-20210327205850643

2. 编码

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
"""
@encoding : utf-8
@Author : Wuguo Tang
@School : CSUFT
@E-mail : 1009592703@qq.com
@File : fhhd.py
@CreateTime : 2021/3/25 14:00
"""
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): # 整型转16位二进制
result = "";
for i in range(16):
y = x & (1)
result += str(y)
x = x >> 1
return result


def fhhd(img, x0, g0, j0, 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 = ""
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 fhhdjm(encryptionImg, x0, g0, j0, 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 = ""
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("E:/9_25.jpg", 1) # 读取原始图像
cv2.imshow("Origin Img", img)

encryptionImg = np.zeros(img.shape, np.uint8)
fhhd(img, 0.15624562, 164, 10, encryptionImg) # 加密
cv2.imwrite("E:/a1.bmp", encryptionImg)
cv2.imshow("Encryption Img", encryptionImg)

img = cv2.imread("E:/a1.bmp", 1) # 读取加密图像
decryptionImg = np.zeros(img.shape, np.uint8)
fhhdjm(img, 0.15624562, 164, 10, decryptionImg) # 解密
cv2.imwrite("E:/a2.bmp", decryptionImg)
cv2.imshow("Decryption Img ", decryptionImg)
cv2.waitKey(-1)

3.运行结果

image-20210809221254315

image-20210809221314844


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!