基于DNA和四维超混沌的彩色图像分块加密算法-自己写的

matlab代码 

clc
clear all
x0=[0.1,0.1,0.1,0.1];
[t,x]=ode45('lorenz_diff',[0:0.001:80],x0);
x1=x(:,1);
x2=x(:,2);
x3=x(:,3);
x4=x(:,4);
for j=1:(128*64)
     v11(j)=mod(floor((abs(x1(j))-floor(abs(x1(j))))*(10^15)/(10^8)),256);
     v12(j)=mod(floor((abs(x2(j))-floor(abs(x2(j))))*(10^15)/(10^8)),256);
     v13(j)=mod(floor((abs(x3(j))-floor(abs(x3(j))))*(10^15)/(10^8)),256);
     v14(j)=mod(floor((abs(x4(j))-floor(abs(x4(j))))*(10^15)/(10^8)),256);  
end

for j=1:(128*64)
     v21(j)=mod(floor(mod(((abs(x1(j))-floor(abs(x1(j))))*(10^15)),10^8)),256);
     v22(j)=mod(floor(mod(((abs(x2(j))-floor(abs(x2(j))))*(10^15)),10^8)),256);
     v23(j)=mod(floor(mod(((abs(x3(j))-floor(abs(x3(j))))*(10^15)),10^8)),256);
     v24(j)=mod(floor(mod(((abs(x4(j))-floor(abs(x4(j))))*(10^15)),10^8)),256);
     
end
image=imread('Lean.jpg');
image=double(image);
R=image(:,:,1); 
G=image(:,:,2);
B=image(:,:,3);
[R1,R2,R3,R4,R5,R6,R7,R8]=fenkuai(R);
[G1,G2,G3,G4,G5,G6,G7,G8]=fenkuai(G);
[B1,B2,B3,B4,B5,B6,B7,B8]=fenkuai(B);
cluter1=cat(3,R1,G1,B1);
cluter2=cat(3,R2,G2,B2);
cluter3=cat(3,R3,G3,B3);
cluter4=cat(3,R4,G4,B4);
cluter5=cat(3,R5,G5,B5);
cluter6=cat(3,R6,G6,B6);
cluter7=cat(3,R7,G7,B7);
cluter8=cat(3,R8,G8,B8);

shufflingcluter1=cluter8;
shufflingcluter2=cluter3;
shufflingcluter3=cluter2;
shufflingcluter4=cluter1;

shufflingcluter5=cluter4;
shufflingcluter6=cluter5;
shufflingcluter7=cluter6;
shufflingcluter8=cluter7;


[A2,index11]=sort(v11);
[A2,index12]=sort(v12);
[A2,index13]=sort(v13);
[A2,index14]=sort(v14);
[A2,index21]=sort(v21);
[A2,index22]=sort(v22);
[A2,index23]=sort(v23);
[A2,index24]=sort(v24);

for i=1:(128*64)
  R1(i)=R1(index11(i));
  G1(i)=G1(index11(i));
  B1(i)=B1(index11(i));
end
for i=1:(128*64)
  R2(i)=R2(index12(i));
  G2(i)=G2(index12(i));
  B2(i)=B2(index12(i));
end
for i=1:(128*64)
  R3(i)=R3(index13(i));
  G3(i)=G3(index13(i));
  B3(i)=B3(index13(i));
end

for i=1:(128*64)
  R4(i)=R4(index14(i));
  G4(i)=G4(index14(i));
  B4(i)=B4(index14(i));
end

for i=1:(128*64)
  R5(i)=R5(index21(i));
  G5(i)=G5(index21(i));
  B5(i)=B5(index21(i));
end
for i=1:(128*64)
  R6(i)=R6(index22(i));
  G6(i)=G6(index22(i));
  B6(i)=B6(index22(i));
end

for i=1:(128*64)
  R7(i)=R7(index23(i));
  G7(i)=G7(index23(i));
  B7(i)=B7(index23(i));
end
for i=1:(128*64)
  R8(i)=R8(index24(i));
  G8(i)=G8(index24(i));
  B8(i)=B8(index24(i));
end
[H,T]=rule1(1);
A = H;
px = fix(length(A)/8);
Bx = A(:,1:px*8);
remain = A(px*8+1:end);
Rx = reshape(Bx,px,8);               % 分成8个元素一组的数据
R2 = remain; 
Rx= num2str(Rx);
Rx=bin2dec(Rx);
H=reshape(Rx,1,8192);
for i=1:4096
rdDNA(i)=H(i);
end

rsum=0;
for i=1:256
    for j=1:256
        rsum=rsum+R(i,j);
  
    end
end
gsum=0;
for i=1:256
    for j=1:256
       gsum=gsum+G(i,j);
       
    end
end
bsum=0;

for i=1:256
    for j=1:256
        bsum=bsum+B(i,j);
   
    end
end

Dmat1=mod(1*mod(rsum,256)*rdDNA,256);
Dmat1=reshape(Dmat1,64,64);
Dmat11=Dmat1';
Dmat2=mod(1*mod(gsum,256)*rdDNA,256);
Dmat2=reshape(Dmat2,64,64);
Dmat22=Dmat2';
Dmat3=mod(1*mod(bsum,256)*rdDNA,256);
Dmat3=reshape(Dmat3,64,64);
Dmat33=Dmat3';

IDMat1=[Dmat1 Dmat11; Dmat11 Dmat11;];   
IDMat11=IDMat1';
IDMat2=[Dmat2 Dmat22; Dmat22 Dmat22;];
IDMat22=IDMat2';
IDMat3=[Dmat3 Dmat33; Dmat33 Dmat33;];
IDMat33=IDMat3';

IIDMat1=[IDMat1 IDMat11; IDMat11 IDMat11;];   
IIDMat2=[IDMat2 IDMat22; IDMat22 IDMat22;]; 
IIDMat3=[IDMat3 IDMat33; IDMat33 IDMat33;]; 
R=bitxor(R,IIDMat1);
G=bitxor(G,IIDMat2);
B=bitxor(B,IIDMat3);
cipher=cat(3,R,G,B);

figure(1);
imhist(uint8(R));
figure(2);
imshow(uint8(cipher));

function [H,T]=rule1(logo)
p53nt=fastaread('sequence.fasta');
SQ=getfield(p53nt,'Sequence');
H=[];
T=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:65536
    if SQ(i)=='A'
       H(i)=0;
       T(i)=0;
    elseif SQ(i)=='C'
       H(i)=0;
       T(i)=1;
    elseif SQ(i)=='G'
       H(i)=1; 
       T(i)=0;
    elseif SQ(i)=='T'
       H(i)=1;     
       T(i)=1;    
    end
end
return

function dx = lorenz_diff(t,x)
a=35;b=3;c=35;d=5;s1=1;s2=0.2;s3=0.3;
dx=[a*(x(2)-x(1))+s1*x(4);
    c*x(1)-x(1)*x(3)+s2*x(4);
    -b*x(3)+x(1)*x(2)+s3*x(4);
    -d*x(1);];

function [I6,I7,I8,I9,I10,I11,I12,I13]=fenkuai(SCC);
[m,n]=size(SCC);
I0(1:m,1:n/2)=SCC(1:m,1:n/2);             %左半块
I1(1:m,1:n/2)=SCC(1:m,n/2+1:n);           %右半块
%左半块分块
[m1,n1]=size(I0);
I2(1:m1/2,1:n1)=I0(1:m1/2,1:n1);        %左上半块
I3(1:m1/2,1:n1)=I0(m1/2+1:m1,1:n1);     %左下半块
%右半块分块
I4(1:m1/2,1:n1)=I1(1:m1/2,1:n1);        %右上半块
I5(1:m1/2,1:n1)=I1(m1/2+1:m1,1:n1);     %右下半块

[m2,n2]=size(I2);
I6(1:m2,1:n2/2)=I2(1:m2,1:n2/2);             %左半块
I7(1:m2,1:n2/2)=I2(1:m2,n2/2+1:n2);          %右半块

I8(1:m2,1:n2/2)=I4(1:m2,1:n2/2);             %左半块
I9(1:m2,1:n2/2)=I4(1:m2,n2/2+1:n2);          %右半块

I10(1:m2,1:n2/2)=I3(1:m2,1:n2/2);             %左半块
I11(1:m2,1:n2/2)=I3(1:m2,n2/2+1:n2);          %右半块

I12(1:m2,1:n2/2)=I5(1:m2,1:n2/2);             %左半块
I13(1:m2,1:n2/2)=I5(1:m2,n2/2+1:n2);          %右半块
return 


 

猜你喜欢

转载自blog.csdn.net/bj21002000/article/details/105044363