Demo entry 6624845

加密

   

Submitted by SLY on Jun 18, 2017 at 13:16
Language: Matlab. Code size: 2.7 kB.

function E = img_encrypt_Hopfield( P, X_S10,  b1, M1, X_S20, b2, M2, R)
%使用改进Logistic映射,hopfield混沌映射,超混沌映射加密图像

%P          待加密彩色图像,
%X_S10      置乱阶段正弦映射初始值(0,1)
%b1         置乱阶段正弦映射参数值 (1,正无穷);
%M1         置乱阶段正弦正弦映射迭代次数,迭代产生Hopfield初始值,mh=[1000 2000 3000]
%X_S20      扩散阶段正弦映射初始值
%b2         扩散阶段正弦映射参数值
%M2         扩散阶段正弦正弦映射迭代次数,迭代产生超混沌映射初始值,mh=[1000 2000 3000 4000]
%R          迭代次数

format long;

%获得图像大小M x N x 3
[M, N, ~] = size(P);
%根据图片尺寸中较大的数值确定Hopfield混沌序列长度
XH_len = max([M,N]);
len = M*N;
%置乱阶段Hopfield初值
X_H0 = zeros(R,3);
%扩散阶段超混沌系统初值
X_HC0 = zeros(R,4);
%加密后的图像
E = double(P);

ER = zeros(M,N);
EG = zeros(M,N);
EB = zeros(M,N);

%-------------------------------------加密-------------------------
for r=1:R
    %分离RGB三个通道
    PR = E(:,:,1);
    PG = E(:,:,2);
    PB = E(:,:,3);
    max_M1 = max(M1);
    X_S1 = zeros(1,max_M1);
    X_S1(1) = X_S10;
    %迭代生成Hopfield初始值
    for i=1:max_M1-1
        X_S1(i+1) = (sin(b1*asin(sqrt(X_S1(i)))))^2;
    end
    %Hopfield初始值
    X_H0(r,1) = X_S1(M1(1));
    X_H0(r,2) = X_S1(M1(2));
    X_H0(r,3) = X_S1(M1(3));
    
    %迭代Hopfield产生混沌序列XHop用于置乱
    [~,XHop] = ode45('hopfield_diff',1:1:XH_len,X_H0(r,:));
    %排序混沌序列,使用得到的索引置乱图像
    XHop_Rx = XHop(1:M,1);
    XHop_Ry = XHop(1:N,2);
    XHop_Gx = XHop(1:M,1);
    XHop_Gy = XHop(1:N,3);
    XHop_Bx = XHop(1:M,1);
    XHop_By = XHop(1:N,2);
    [~,indRx] = sort(XHop_Rx);
    [~,indRy] = sort(XHop_Ry);
    [~,indGx] = sort(XHop_Gx);
    [~,indGy] = sort(XHop_Gy);
    [~,indBx] = sort(XHop_Bx);
    [~,indBy] = sort(XHop_By);
    %得到置乱后的序列ER,EG,EB
	ER(indRx,indRy) = PR;
	EG(indGx,indGy) = PG;
	EB(indBx,indBy) = PB; 
    E(:,:,1) = reshape(ER,M,N);
    E(:,:,2) = reshape(EG,M,N);
    E(:,:,3) = reshape(EB,M,N);
    
    max_M2 = max(M2);
    X_S2 = zeros(1,max_M2);
    X_S2(1) = X_S20;
    %迭代生成超混沌系统初始值
    for i=1:max_M2-1
        X_S2(i+1) = (sin(b2*asin(sqrt(X_S2(i)))))^2;
    end
    %超混沌系统初始值
    X_HC0(r,1) = X_S2(M2(1));
    X_HC0(r,2) = X_S2(M2(2));
    X_HC0(r,3) = X_S2(M2(3));
    X_HC0(r,4) = X_S2(M2(4));
    %生成混沌序列
    [~,X_HC] = ode45('hyperchaotic_diff',0.001:0.001:ceil(len/4)/1000,X_HC0(r,:));
    %把E化为1*len大小的数组,
    S = bitxor(bitshift(bitxor(bitshift(E(:,:,1), 8),E(:,:,2)),8),E(:,:,3));
    S = reshape(S, 1,len);
    %把X_HC化为1*len大小的数组,舍弃多余序列
    XS_HC = reshape(X_HC, 1, ceil(len/4)*4);
    XS_HC(len+1:end) = [];
    %把混沌序列值化成正整数
    E(:, :, 3) = reshape(bitand(C,255), M, N);
    E(:, :, 2) = reshape(bitand(bitshift(C,-8),255), M, N);
    E(:, :, 1) = reshape(bitand(bitshift(C,-16),255), M, N);
    E = reshape(E,[M N 3]);
end
    E = uint8(E);
end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).