MATLAB-image encryption

1. Amplify encryption based on pixel RGB value

Each pixel of the image has a corresponding color value. We enlarge the color value on the pixel and overlap and overlay each other to hide the original information of the image to achieve the effect of encryption. In fact, by zooming in on pixel values, color values ​​are naturally zoomed in. The larger the magnification, the harder it is to distinguish the original information from the encrypted image.

%% 基于像素点RGB值放大加密
% 读取加密图像
a = imread('e:\image-code\liuyifei.jpg');

% size(a)函数:获取矩阵大小,返回矩阵行列数
% rand()函数:随机生成和a大小相同的矩阵并且乘以100,可理解为加密倍数
r = rand(size(a))*100;

% 将a转为双精度,因为rand()函数生成的r矩阵精度较高,a矩阵与r矩阵相乘,需要提高a矩阵的的精度
rgbd = im2double(a);

%% 矩阵点乘运算,实现rgb值的放大加密
rgbs = rgbd.*r;

%% 保存加密后的图像
% b = imwrite(rgbs,'E:\image-code\jiami001.jpg');

%% 矩阵点除运算,实现rgb值的缩小解密
rgbe = rgbs./r;

%% 显示图像
subplot(1,3,1);imshow(a);title('原图');
subplot(1,3,2);imshow(rgbs);title('加密后');
subplot(1,3,3);imshow(rgbe);title('解密后');

If you are prompted to use imwirte incorrectly, refer to this blog

insert image description here

2. Encryption based on row and column pixel scrambling

Row and column pixel scrambling is to scramble the order of each row in the original matrix of the image, and then scramble the order of each column. The specific implementation method is to use the randsample( function to randomly generate an integer sequence whose number of elements is equal to the number of rows, each element is not greater than the number of rows, and each element is different. Rearrange. Rearrange the columns of the matrix in the same way, and the randomly generated integer sequence can be used as the key. For example, if there is a matrix with 4 rows and 6 columns, the initial row order is (1, 2, 3, 4), and the generated row The random number column is (2, 4, 3, 1), then when rearranging, put the first row of the matrix in the fourth row, and so on, to get the image after row encryption, and then use the same method on the basis of row encryption Carry out column encryption. The pixel size of an image is its matrix size. Since the number of matrix rows and columns of a general image is relatively large, this encryption method can be used to hide image information.

%% 基于行列像素点置乱加密
%% 读取图像
a = imread('E:\image-code\liuyifei.jpg');
% 显示原图
subplot(2,3,1);imshow(a);title('原图');

%% 获取原图信息
% 获取原图行列数
[M,N] = size(a);

% 随机生成M个不相同的不大于M的数
Rm = randsample(M,M)';
Mchange = [1:1:M;Rm];

% 随机生成N个不相同的不大于N的数
Rn = randsample(N,N)';
Nchange = [1:1:N;Rn];

%% 加密
% 打乱行顺序
a(Mchange(1,:),:) = a(Mchange(2,:),:); % 按照随机生成的Rm序列中的顺序对原图矩阵的行进行重新排列
subplot(2,3,2);imshow(a);title('行加密');

% 打乱列顺序
a(:,Nchange(1,:)) = a(:,Nchange(2,:)); % 按照随机生成的Rn序列中的顺序对原图矩阵的列进行重新排列
subplot(2,3,3);imshow(a);title('列加密');

%% 解密
% 恢复行顺序
a(Mchange(2,:),:) = a(Mchange(1,:),:); % 逆向还原
subplot(2,3,5);imshow(a);title('行解密');

% 恢复列顺序
a(:,Nchange(2,:)) = a(:,Nchange(1,:)); % 逆向还原
subplot(2,3,6);imshow(a);title('列解密');

insert image description here

3. Encryption based on grayscale scrambling

A grayscale image is regarded as an image composed of many pixels with a value between 0 and 255, 255 represents white, 0 represents black, and there are 256 gray levels between black and white. Grayscale scrambling encryption is to perform certain operations on the grayscale value of the image to scramble and reset, so as to hide the information of the original image. This algorithm is suitable for grayscale images, and can be used in practical applications by converting color images into grayscale images.

clear all;
clc;
%% 基于灰度置乱加密
%% 获取原始图像
L = imread('E:\image-code\jiemi002.bmp')
% 请使用灰度图像,如果是彩色图像,会丢失彩色信息!!!
subplot(1,3,1);imshow(L);title('原图');
[x,y] = size(L);
% 生成与原图大小相同的随机矩阵
gadd = fix(255*rand(x,y));

%% 灰度值置乱加密
for m=1:x
    for n=1:y
        L1(m,n)=0.1*L(m,n)+0.9*gadd(m,n);
    end
end
subplot(1,3,2);imshow(L1);title('加密图片');

%% 逆向解密
%% 灰度值置乱加密
for m=1:x
    for n=1:y
        L2(m,n)=(L1(m,n)-0.9*gadd(m,n))/0.1;
    end
end
subplot(1,3,3);imshow(L2);title('解密图片');

insert image description here

4. Encryption based on chaotic sequence

The principle of chaotic sequence encryption is to superimpose the chaotic sequence rearranged according to the key in a specific way on the original image sequence information, so that when the rearranged information is transmitted, the signal on the public channel will be in a The characteristics of random noise transmission, such signals exist in a large number in the network, the attacker cannot determine whether it is encrypted information or useless information, which reduces the possibility of encrypted images being attacked. After receiving the encrypted information, the receiver rearranges the chaotic sequence according to the pre-determined method and removes it from the encrypted sequence. After a certain degree of calculation, the original image sequence information of the image is restored, thus showing original image information. Its schematic diagram is as follows:

insert image description here

clear all;
clc;
%% 处理图像信息
% 读取原图
l = imread('E:\image-code\liuyifei.jpg')
% 转换为灰度图像
x = rgb2gray(l);
% 显示原始图片
subplot(1,3,1);imshow(x);title('原始图片');
% 获取图像大小
[a,b,c] = size(l);
% 定义变量N并赋值
N = a*b;

%% 加密
%% 创建秘钥
% 用户输入加密密码
m(1) = input('请输入秘钥(0~1之间):');
% 提示信息
disp('加密中...');
% 进行N-1次循环产生序列密码
for i=1:N-1
    m(i+1) = 4*m(i)-4*m(i)^2;
end
m = mod(1000*m,256);
% 转换为无符号整型
m = uint8(m);
n = 1;
for i=1:a
    for j=1:b
        % 将图像每一像素值与序列每一位进行异或运算
        e(i,j)=bitxor(m(n),x(i,j));
        n = n+1;
    end
end
%% 显示加密后的图片
subplot(1,3,2);imshow(e);title('加密后图片');
%% 写入加密后的图片
imwrite(e,'E:\image-code\jiami002.bmp');

%% 解密
[a,b,c] = size(e);
N = a*b;
m(1) = input('请输入秘钥(0~1之间):');
disp('解密中...');
% 进行N-1次循环产生序列密码
for i=1:N-1
    m(i+1) = 4*m(i)-4*m(i)^2;
end
m = mod(1000*m,256);
% 转换为无符号整型
m = uint8(m);
n = 1;
for i=1:a
    for j=1:b
        % 将图像每一像素值与序列每一位进行异或运算
        o(i,j)=bitxor(m(n),x(i,j));
        n = n+1;
    end
end

%% 显示解密后的图片
subplot(1,3,3);imshow(o);title('解密后图片');

%% 写入解密后的图片
imwrite(o,'E:\image-code\jiemi002.bmp');

insert image description here

Source: Hetian Network Security Lab
Code

Guess you like

Origin blog.csdn.net/qq_40843903/article/details/126915066