知道ICA原理后(不知道的,可以自行查找相关资料),我们就可以利用ICA还原信号的正定要求,进行图像加密,因为欠定下是不能还原信号的。
下面我用流程图说明下这个过程。
假设我们有p-1幅图像为密钥图像,一幅为密文图像,那么我们通过混合后,生成一幅混合图像(可以看后面例子的图像),此过程可以算是加密。解密就是通过,P-1幅秘钥图像和一幅混合图像利用ICA算法进行解密。
下面我演示一个简单的示例:
matlab 程序:(注:这里的图像都是220*220的图像)
clear all;
close all;
img1 = imread('lena.jpg');
img2 = imread('密文3.png');
img3 = imread('密文4.png');
img4 = imread('ciphertext.png');
[src1 ,I1] = imgPro(img1);
[src2 ,I2] = imgPro(img2);
[src3 ,I3] = imgPro(img3);
[src4 ,I4] = imgPro(img4);
src4 = abs(0.4-src4);
S=[src1;src2;src3;src4];
Sweight=abs(rand(size(S,1))*0.5);
MixedS=Sweight*S; % 将混合矩阵重新排列并输出
%% 方法二:
% MixedS(2,:) = src1;
% MixedS(3,:) = src2;
% MixedS(4,:) = src3;
%% show
dst1 = reshape(MixedS(1,:),220,220);
dst2 = reshape(MixedS(2,:),220,220);
dst3 = reshape(MixedS(3,:),220,220);
dst4 = reshape(MixedS(4,:),220,220);
dst1 = mapminmax(dst1, 0, 1);
dst2 = mapminmax(dst2, 0, 1);
dst3 = mapminmax(dst3, 0, 1);
dst4 = mapminmax(dst4, 0, 1);
subplot(2,4,1),imshow(dst1),title('输入图像1');
subplot(2,4,2),imshow(dst2),title('输入图像2');
subplot(2,4,3),imshow(dst3),title('输入图像3');
subplot(2,4,4),imshow(dst4),title('输入图像4');
% figure,imshow(dst1);
% figure,imshow(dst2);
% figure,imshow(dst3);
% figure,imshow(dst4);
MixedS_bak=MixedS;
len =4;
%% %%%%%%%%%%%%%%%%%%%%%%%%%% 标准化 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MixedS_mean=zeros(len,1);
for i=1:len
MixedS_mean(i)=mean(MixedS(i,:));
end % 计算MixedS的均值
for i=1:len
for j=1:size(MixedS,2)
MixedS(i,j)=MixedS(i,j)-MixedS_mean(i);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%% 白化 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MixedS_cov=cov(MixedS'); % cov为求协方差的函数
[E,D]=eig(MixedS_cov); % 对信号矩阵的协方差函数进行特征值分解
Q=inv(sqrt(D))*(E)'; % Q为白化矩阵
MixedS_white=Q*MixedS; % MixedS_white为白化后的信号矩阵
IsI=cov(MixedS_white'); % IsI应为单位阵
%%%%%%%%%%%%%%%%%%%%%%%% FASTICA算法 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X=MixedS_white; % 以下算法将对X进行操作
[VariableNum,SampleNum]=size(X);
numofIC=VariableNum; % 在此应用中,独立元个数等于变量个数
B=zeros(numofIC,VariableNum); % 初始化列向量w的寄存矩阵,B=[b1 b2 ... bd]
for r=1:numofIC
i=1;maxIterationsNum=100; % 设置最大迭代次数(即对于每个独立分量而言迭代均不超过此次数)
IterationsNum=0;
b=rand(numofIC,1)-.5; % 随机设置b初值
b=b/norm(b); % 对b标准化 norm(b):向量元素平方和开根号
while i<=maxIterationsNum+1
if i == maxIterationsNum % 循环结束处理
fprintf('\n第%d分量在%d次迭代内并不收敛。', r,maxIterationsNum);
break;
end
bOld=b;
a2=1;
u=1;
t=X'*b;
g=t.*exp(-a2*t.^2/2);
dg=(1-a2*t.^2).*exp(-a2*t.^2/2);
b=((1-u)*t'*g*b+u*X*g)/SampleNum-mean(dg)*b;
% 核心公式
b=b-B*B'*b; % 对b正交化
b=b/norm(b);
if abs(abs(b'*bOld)-1)<1e-9 % 如果收敛,则
B(:,r)=b; % 保存所得向量b
break;
end
i=i+1;
end
% B(:,r)=b; % 保存所得向量b
end
%%%%%%%%%%%%%%%%%%%%%%%%%% ICA计算的数据复原并构图 %%%%%%%%%%%%%%%%%%%%%%%%%
ICAedS=B'*Q*MixedS_bak; % 计算ICA后的矩阵
ICAedS=abs(ICAedS)/max(max(abs(ICAedS)));
dst1 = imadjust(reshape(ICAedS(1,:),220,220));
dst2 = imadjust(reshape(ICAedS(2,:),220,220));
dst3 = imadjust(reshape(ICAedS(3,:),220,220));
dst4 = imadjust(reshape(ICAedS(4,:),220,220));
subplot(2,4,5),imshow(dst1),title('解密图像1');
subplot(2,4,6),imshow(dst2),title('解密图像2');
subplot(2,4,7),imshow(dst3),title('解密图像3');
subplot(2,4,8),imshow(dst4),title('解密图像4');
imgPro.m
function [ out ,I] = imgPro( IN )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
I = rgb2gray(IN);
M = size(I,1)*size(I,2);
out = double(reshape(I,1,M))/255;
end
程序的结果为:
可以看到,通过ICA算法能够还原出混合的图像。
高级版本:为了更难让别人破解,再加入混沌加密(具体算法可以百度)
也就是在发送前,进行混沌加密,ICA处理前,进行混沌解密。
程序代码如下:
clear all;
close all;
%% 读取数据
img1 = imread('lena.jpg');
img2 = imread('密文3.png');
img3 = imread('密文4.png');
img4 = imread('ciphertext.png');
subplot(3,4,1),imshow(rgb2gray(img1)),title('输入图像1');
subplot(3,4,2),imshow(rgb2gray(img2)),title('输入图像2');
subplot(3,4,3),imshow(rgb2gray(img3)),title('输入图像3');
subplot(3,4,4),imshow(rgb2gray(img4)),title('输入图像4');
%% 预处理
[src1 ,I1] = imgPro(img1);
[src2 ,I2] = imgPro(img2);
[src3 ,I3] = imgPro(img3);
[src4 ,I4] = imgPro(img4);
src4 = abs(0.4-src4);%%密文
M = size(I1,1)*size(I1,2);
S=[src1;src2;src3;src4];
Sweight=abs(rand(size(S,1))*0.5);
MixedS=Sweight*S; % 将混合矩阵重新排列并输出
%% 方法二:给定混合图像和3幅不相关的原始图像
MixedS(2,:) = src1;
MixedS(3,:) = src2;
MixedS(4,:) = src3;
%% show
dst1 = reshape(MixedS(1,:),220,220);
dst2 = reshape(MixedS(2,:),220,220);
dst3 = reshape(MixedS(3,:),220,220);
dst4 = reshape(MixedS(4,:),220,220);
dst1 = mapminmax(dst1, 0, 1);
dst2 = mapminmax(dst2, 0, 1);
dst3 = mapminmax(dst3, 0, 1);
dst4 = mapminmax(dst4, 0, 1);
subplot(3,4,5),imshow(dst1),title('混合图像1');
% subplot(3,4,6),imshow(dst2),title('混合图像2');
% subplot(3,4,7),imshow(dst3),title('混合图像3');
% subplot(3,4,8),imshow(dst4),title('混合图像4');
%% 加密
r=[0.343 0.432 0.63 3.769 3.82 3.85 0.1 1];
dst1 = uint8(dst1*255);
dst2 = uint8(dst2*255);
dst3 = uint8(dst3*255);
dst4 = uint8(dst4*255);
e1=jiami(dst1,r);
% e2=jiami(dst2,r);
% e3=jiami(dst3,r);
% e4=jiami(dst4,r);
subplot(3,4,9),imshow(uint8(e1)),title('加密图像1');
% subplot(3,4,10),imshow(uint8(e2)),title('加密图像2');
% subplot(3,4,11),imshow(uint8(e3)),title('加密图像3');
% subplot(3,4,12),imshow(uint8(e4)),title('加密图像4');
suptitle('加密过程');
%%
%% 解密
r = [0.343 0.432 0.63 3.769 3.82 3.85 0.1 1];
dst1=jiemi1(e1,r);
% dst2=jiemi1(e2,r);
% dst3=jiemi1(e3,r);
% dst4=jiemi1(e4,r);
figure,
subplot(3,4,1),imshow(uint8(e1)),title('加密图像1');
% subplot(3,4,2),imshow(uint8(e2)),title('加密图像2');
% subplot(3,4,3),imshow(uint8(e3)),title('加密图像3');
% subplot(3,4,4),imshow(uint8(e4)),title('加密图像4');
subplot(3,4,5),imshow(uint8(dst1)),title('解密图像1');
subplot(3,4,6),imshow(uint8(dst2)),title('解密图像2');
subplot(3,4,7),imshow(uint8(dst3)),title('解密图像3');
subplot(3,4,8),imshow(uint8(dst4)),title('解密图像4');
%%
MixedS_bak=MixedS;
len =4;
%% %%%%%%%%%%%%%%%%%%%%%%%%%% 标准化 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MixedS_mean=zeros(len,1);
for i=1:len
MixedS_mean(i)=mean(MixedS(i,:));
end % 计算MixedS的均值
for i=1:len
for j=1:size(MixedS,2)
MixedS(i,j)=MixedS(i,j)-MixedS_mean(i);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%% 白化 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MixedS_cov=cov(MixedS'); % cov为求协方差的函数
[E,D]=eig(MixedS_cov); % 对信号矩阵的协方差函数进行特征值分解
Q=inv(sqrt(D))*(E)'; % Q为白化矩阵
MixedS_white=Q*MixedS; % MixedS_white为白化后的信号矩阵
IsI=cov(MixedS_white'); % IsI应为单位阵
%%%%%%%%%%%%%%%%%%%%%%%% FASTICA算法 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X=MixedS_white; % 以下算法将对X进行操作
[VariableNum,SampleNum]=size(X);
numofIC=VariableNum; % 在此应用中,独立元个数等于变量个数
B=zeros(numofIC,VariableNum); % 初始化列向量w的寄存矩阵,B=[b1 b2 ... bd]
for r=1:numofIC
i=1;maxIterationsNum=100; % 设置最大迭代次数(即对于每个独立分量而言迭代均不超过此次数)
IterationsNum=0;
b=rand(numofIC,1)-.5; % 随机设置b初值
b=b/norm(b); % 对b标准化 norm(b):向量元素平方和开根号
while i<=maxIterationsNum+1
if i == maxIterationsNum % 循环结束处理
fprintf('\n第%d分量在%d次迭代内并不收敛。', r,maxIterationsNum);
break;
end
bOld=b;
a2=1;
u=1;
t=X'*b;
g=t.*exp(-a2*t.^2/2);
dg=(1-a2*t.^2).*exp(-a2*t.^2/2);
b=((1-u)*t'*g*b+u*X*g)/SampleNum-mean(dg)*b;
% 核心公式
b=b-B*B'*b; % 对b正交化
b=b/norm(b);
if abs(abs(b'*bOld)-1)<1e-9 % 如果收敛,则
B(:,r)=b; % 保存所得向量b
break;
end
i=i+1;
end
% B(:,r)=b; % 保存所得向量b
end
%%%%%%%%%%%%%%%%%%%%%%%%%% ICA计算的数据复原并构图 %%%%%%%%%%%%%%%%%%%%%%%%%
ICAedS=B'*Q*MixedS_bak; % 计算ICA后的矩阵
ICAedS=abs(ICAedS)/max(max(abs(ICAedS)));
dst1 = imadjust(reshape(ICAedS(1,:),220,220));
dst2 = imadjust(reshape(ICAedS(2,:),220,220));
dst3 = imadjust(reshape(ICAedS(3,:),220,220));
dst4 = imadjust(reshape(ICAedS(4,:),220,220));
suptitle('解密过程');
d1 = double(sum((ICAedS(1,:)>0.5)))/M;
d2 = double(sum((ICAedS(2,:)>0.5)))/M;
d3 = double(sum((ICAedS(3,:)>0.5)))/M;
d4 = double(sum((ICAedS(4,:)>0.5)))/M;
u = [d1,d2,d3,d4];
d = find(max(u));
if d==1
subplot(3,4,9),imshow(dst1),title('密文图像1');
figure,imshow(dst1);
suptitle('密文');
elseif d==2
subplot(3,4,10),imshow(dst2),title('密文图像2');
figure,imshow(dst2);
suptitle('密文');
elseif d==3
subplot(3,4,11),imshow(dst3),title('密文图像3');
figure,imshow(dst3);
suptitle('密文');
elseif d==4
subplot(3,4,12),imshow(dst4),title('密文图像4');
figure,imshow(dst4);
suptitle('密文');
end
效果图如下:
混沌加密解密程序如下:
jiami.m
function e=jiami(x,data)
m(1)=data(1);
m1(1)=data(2);
m2(1)=data(3);
[a,b]=size(x);
N=a*b;
u1=data(4);
%u=4;
for i=1:N-1
m(i+1)=u1*m(i)*(1-m(i));
end
m=mode(255*m,256);
m=uint8(m);
u2=data(5);
for i=1:N-1
m1(i+1)=u2*m1(i)*(1-m1(i));
end
m1=mode(255*m1,256);
m1=uint8(m1);
u3=data(6);
for i=1:N-1
m2(i+1)=u3*m2(i)*(1-m2(i));
end
m2=mode(255*m2,256);
m2=uint8(m2);
%n=1;
n=data(8);
x=double(x);
m=double(m);
m1=double(m1);
m2=double(m2);
e = zeros(a,b);
for i=1:a
for j=1:b
e(i,j)=m(n)+m1(n);
e(i,j)=bitxor(e(i,j),m2(n));
e(i,j)=e(i,j)+x(i,j);
e(i,j)=mod(e(i,j),255);
n=n+1;
end
end
end
jiemi1.m
function kk=jiemi1(e,data)
e=double(e);
[a,b]=size(e);
e=floor(e);
m3(1)=data(1);
m4(1)=data(2);
m5(1)=data(3);
u1=data(4);
N=a*b;
for i=1:N-1
m3(i+1)=u1*m3(i)*(1-m3(i));
end
m3=mode(255*m3,256);
m3=uint8(m3);
u2=data(5);
for i=1:N-1
m4(i+1)=u2*m4(i)*(1-m4(i));
end
m4=mode(255*m4,256);
m4=uint8(m4); u3=data(6);
for i=1:N-1
m5(i+1)=u3*m5(i)*(1-m5(i));
end
m5=mode(255*m5,256);
m5=uint8(m5);
sigma=data(7);
n=data(8);
m3=double(m3);
m4=double(m4);
m5=double(m5);
for i=1:a
for j=1:b
kk(i,j)=m3(n)+m4(n);
kk(i,j)=bitxor(kk(i,j),m5(n));
kk(i,j)=e(i,j)-kk(i,j);
kk(i,j)=mod(kk(i,j),255);
n=n+1;
end
end
end
简单的演示到此为止。