基于DNA编码的图像加密

clc
clear all
X=imread('2-2.jpg');
algs = {'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'};
h=hash(X,algs{4});
disp([algs{4} '(' num2str(length(h)*4) ' bits):']);
%disp(h);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:64
     K(i)=hex2dec(h(i));
end
H=dec2bin(K);
H=reshape(H,1,256);

for i=1:(length(H)/8)
     k{i}=H((i*8-7):(i*8));   
     k{i}=bin2dec(k{i});   
end
init1=(bitxor((bitxor((bitxor((bitxor((bitxor((bitxor((bitxor(k{1},k{2})),k{3})),k{4})),k{5})),k{6})),k{7})),k{8}))/256;
init2=(bitxor((bitxor((bitxor((bitxor((bitxor((bitxor((bitxor(k{9},k{10})),k{11})),k{12})),k{13})),k{14})),k{15})),k{16}))/256;
init3=(bitxor((bitxor((bitxor((bitxor((bitxor((bitxor((bitxor(k{17},k{18})),k{19})),k{20})),k{21})),k{22})),k{23})),k{24}))/256;
init4=(bitxor((bitxor((bitxor((bitxor((bitxor((bitxor((bitxor(k{25},k{26})),k{27})),k{28})),k{29})),k{30})),k{31})),k{32}))/256;
init5=(bitxor((bitxor((bitxor((bitxor((bitxor((bitxor((bitxor(k{25},k{26})),k{27})),k{28})),k{29})),k{30})),k{31})),k{32}));
a(1)=init4;
x(1)=(init1+init5)/256;
y(1)=(init2+init5)/256;
b=(init3+init5)/256;
kk=4;
for i=1:2000000
    a(i+1)=kk*a(i)*(1-a(i));
end
num=1;
for i=100:65635
     A1(num)=a(i);
     num=num+1;
end
num=1;
for i=65635:131170
     A2(num)=a(i);
     num=num+1;
end
[m,n]=size(X);
X=double(X);
[A1,index]=sort(A1);
index=reshape(index,256,256);
[A2,cow]=sort(A2);
cow=reshape(cow,256,256);

for i=1:m
    inde{i}=index(i,:); %取行
end

for i=1:m
    G{i}=X(i,:); %取行
end

for i=1:m
    for j=1:n
     G{i}(j)=X(inde{i}(j));
    end
end
AA=[];
for i=1:256 %
eval(['AA',num2str(i),'=','G{i}']); %A1至A10都是2行五列的矩阵,这个要改成你想要的
eval(['AA=[AA;AA',num2str(i),'];']); %组合A=[A1;A2;A3;...]
end
AA=reshape(AA,[256,256]);

for i=1:n
    inde{i}=cow(:,i); %取列
end

for i=1:n
    G{i}=AA(:,i); %取列
end

for i=1:m
    for j=1:n
     G{i}(j)=AA(inde{i}(j));
    end
end
BB=[];
for i=1:256 %
eval(['BB',num2str(i),'=','G{i}']); %A1至A10都是2行五列的矩阵,这个要改成你想要的
eval(['BB=[BB;BB',num2str(i),'];']); %组合A=[A1;A2;A3;...]
end
BB=reshape(BB,[256,256]);
%figure(1);
%imhist(uint8(BB));
%figure(2);
%imshow(uint8(BB));
for i=1:524288
    x(i+1)=sin(pi*(4*b*x(i)*(1-x(i))+(1-b)*sin(pi*y(i))));
    y(i+1)=sin(pi*(4*b*y(i)*(1-y(i))+(1-b)*sin(pi*x(i))));
    y(i)=mod(floor(255*y(i)),255);
    
end
A = y;
p = fix(length(A)/32768);
B = A(:,1:p*32768);
remain = A(p*32768+1:end);
R = reshape(B,p,32768);              
R2 = remain; 
KEY1=R;

for i=1:16
    KEY{i}=KEY1(i,:); %取行
    KEY{i}=reshape(KEY{1},256,128);
   

end

LX(1:m,1:n/2)=double(X(1:m,1:n/2));            %左半快
RX(1:m,1:n/2)=double(X(1:m,n/2+1:n));          %右半块
P0=LX;
Q0=RX;
for j=1:8                                    %加密部分
      
      Q1=bitxor(P0,bitxor(Q0,KEY{j}));
      P1=Q0;
      P0=P1;
      Q0=Q1;
end 
%PPPC=[P0,Q0];

%PPPC=reshape(PPPC,256,256);

%figure(1)
%imshow(uint8(PPPC));
%figure(2);
%imhist(uint8(PPPC));
L8=P0;
R8=Q0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:8
     Array{i} =bitget(L8,i);
end 
A8=Array{1};
B8=Array{2};
C8=Array{3};
D8=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E8=Array{5};
F8=Array{6};
G8=Array{7};
H8=Array{8};
[m1,n1]=size(A8);
num=1;
for i=1:m1
 for j=1:n1
      dna91(num)= chan(A8(i,j),B8(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dna92(num)= chan(C8(i,j),D8(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dna93(num)= chan(E8(i,j),F8(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dna94(num)= chan(G8(i,j),H8(i,j));
      num=num+1;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:8
     Array{i} =bitget(R8,i);
end 
A8=Array{1};
B8=Array{2};
C8=Array{3};
D8=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E8=Array{5};
F8=Array{6};
G8=Array{7};
H8=Array{8};
[m1,n1]=size(A8);
num=1;
for i=1:m1
 for j=1:n1
      dnar91(num)= chan(A8(i,j),B8(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnar92(num)= chan(C8(i,j),D8(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnar93(num)= chan(E8(i,j),F8(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnar94(num)= chan(G8(i,j),H8(i,j));
      num=num+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:8
     Array{i} =bitget(KEY{9},i);
end 
KEY91=Array{1};
KEY92=Array{2};
KEY93=Array{3};
KEY94=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY95=Array{5};
KEY96=Array{6};
KEY97=Array{7};
KEY98=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak91(num)= chan(KEY91(i,j),KEY92(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak92(num)= chan(KEY93(i,j),KEY94(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak93(num)= chan(KEY95(i,j),KEY96(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak94(num)= chan(KEY97(i,j),KEY98(i,j));
      num=num+1;
end
end

for i=1:(m1*n1)
          dnar101(i)=dnasubt(dna91(i),(dnaaddtion(dnar91(i),dnak91(i))));
          dna101(i)=dnar91(i);

          dnar102(i)=dnasubt(dna92(i),(dnaaddtion(dnar92(i),dnak92(i))));
          dna102(i)=dnar92(i);

          dnar103(i)=dnasubt(dna93(i),(dnaaddtion(dnar93(i),dnak93(i))));
          dna103(i)=dnar93(i);
         
          dnar104(i)=dnasubt(dna94(i),(dnaaddtion(dnar94(i),dnak94(i))));
          dna104(i)=dnar94(i);


end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第10轮
for i=1:8
     Array{i} =bitget(KEY{10},i);
end 
KEY101=Array{1};
KEY102=Array{2};
KEY103=Array{3};
KEY104=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY105=Array{5};
KEY106=Array{6};
KEY107=Array{7};
KEY108=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak101(num)= chan(KEY101(i,j),KEY102(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak102(num)= chan(KEY103(i,j),KEY104(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak103(num)= chan(KEY105(i,j),KEY106(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak104(num)= chan(KEY107(i,j),KEY108(i,j));
      num=num+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(m1*n1)
          dnar111(i)=dnasubt(dna101(i),(dnaaddtion(dnar101(i),dnak101(i))));
          dna111(i)=dnar101(i);

          dnar112(i)=dnasubt(dna102(i),(dnaaddtion(dnar102(i),dnak102(i))));
          dna112(i)=dnar102(i);

          dnar113(i)=dnasubt(dna103(i),(dnaaddtion(dnar103(i),dnak103(i))));
          dna113(i)=dnar103(i);
         
          dnar114(i)=dnasubt(dna104(i),(dnaaddtion(dnar104(i),dnak104(i))));
          dna114(i)=dnar104(i);


end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%第11轮
for i=1:8
     Array{i} =bitget(KEY{11},i);
end 
KEY111=Array{1};
KEY112=Array{2};
KEY113=Array{3};
KEY114=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY115=Array{5};
KEY116=Array{6};
KEY117=Array{7};
KEY118=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak111(num)= chan(KEY111(i,j),KEY112(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak112(num)= chan(KEY113(i,j),KEY114(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak113(num)= chan(KEY115(i,j),KEY116(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak114(num)= chan(KEY117(i,j),KEY118(i,j));
      num=num+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(m1*n1)
          dnar121(i)=dnasubt(dna111(i),(dnaaddtion(dnar111(i),dnak111(i))));
          dna121(i)=dnar111(i);

          dnar122(i)=dnasubt(dna112(i),(dnaaddtion(dnar112(i),dnak112(i))));
          dna122(i)=dnar112(i);

          dnar123(i)=dnasubt(dna113(i),(dnaaddtion(dnar113(i),dnak113(i))));
          dna123(i)=dnar113(i);
         
          dnar124(i)=dnasubt(dna114(i),(dnaaddtion(dnar114(i),dnak114(i))));
          dna124(i)=dnar114(i);


end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第12轮
for i=1:8
     Array{i} =bitget(KEY{12},i);
end 
KEY121=Array{1};
KEY122=Array{2};
KEY123=Array{3};
KEY124=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY125=Array{5};
KEY126=Array{6};
KEY127=Array{7};
KEY128=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak121(num)= chan(KEY121(i,j),KEY122(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak122(num)= chan(KEY123(i,j),KEY124(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak123(num)= chan(KEY125(i,j),KEY126(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak124(num)= chan(KEY127(i,j),KEY128(i,j));
      num=num+1;
end
end

for i=1:(m1*n1)
          dnar131(i)=dnasubt(dna121(i),(dnaaddtion(dnar121(i),dnak121(i))));
          dna131(i)=dnar121(i);

          dnar132(i)=dnasubt(dna122(i),(dnaaddtion(dnar122(i),dnak122(i))));
          dna132(i)=dnar122(i);

          dnar133(i)=dnasubt(dna123(i),(dnaaddtion(dnar123(i),dnak123(i))));
          dna133(i)=dnar123(i);
         
          dnar134(i)=dnasubt(dna124(i),(dnaaddtion(dnar124(i),dnak124(i))));
          dna134(i)=dnar124(i);


end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第13轮

for i=1:8
     Array{i} =bitget(KEY{13},i);
end 
KEY131=Array{1};
KEY132=Array{2};
KEY133=Array{3};
KEY134=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY135=Array{5};
KEY136=Array{6};
KEY137=Array{7};
KEY138=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak131(num)= chan(KEY131(i,j),KEY132(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak132(num)= chan(KEY133(i,j),KEY134(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak133(num)= chan(KEY135(i,j),KEY136(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak134(num)= chan(KEY137(i,j),KEY138(i,j));
      num=num+1;
end
end


for i=1:(m1*n1)
          dnar141(i)=dnasubt(dna131(i),(dnaaddtion(dnar131(i),dnak131(i))));
          dna141(i)=dnar131(i);

          dnar142(i)=dnasubt(dna132(i),(dnaaddtion(dnar132(i),dnak132(i))));
          dna142(i)=dnar132(i);

          dnar143(i)=dnasubt(dna133(i),(dnaaddtion(dnar133(i),dnak133(i))));
          dna143(i)=dnar133(i);
         
          dnar144(i)=dnasubt(dna134(i),(dnaaddtion(dnar134(i),dnak134(i))));
          dna144(i)=dnar134(i);


end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第14轮

for i=1:8
     Array{i} =bitget(KEY{14},i);
end 
KEY141=Array{1};
KEY142=Array{2};
KEY143=Array{3};
KEY144=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY145=Array{5};
KEY146=Array{6};
KEY147=Array{7};
KEY148=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak141(num)= chan(KEY141(i,j),KEY142(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak142(num)= chan(KEY143(i,j),KEY144(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak143(num)= chan(KEY145(i,j),KEY146(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak144(num)= chan(KEY147(i,j),KEY148(i,j));
      num=num+1;
end
end


for i=1:(m1*n1)
          dnar151(i)=dnasubt(dna141(i),(dnaaddtion(dnar141(i),dnak141(i))));
          dna151(i)=dnar141(i);

          dnar152(i)=dnasubt(dna142(i),(dnaaddtion(dnar142(i),dnak142(i))));
          dna152(i)=dnar142(i);

          dnar153(i)=dnasubt(dna143(i),(dnaaddtion(dnar143(i),dnak143(i))));
          dna153(i)=dnar143(i);
         
          dnar154(i)=dnasubt(dna144(i),(dnaaddtion(dnar144(i),dnak144(i))));
          dna154(i)=dnar144(i);


end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第15轮

for i=1:8
     Array{i} =bitget(KEY{15},i);
end 
KEY151=Array{1};
KEY152=Array{2};
KEY153=Array{3};
KEY154=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY155=Array{5};
KEY156=Array{6};
KEY157=Array{7};
KEY158=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak151(num)= chan(KEY151(i,j),KEY152(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak152(num)= chan(KEY153(i,j),KEY154(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak153(num)= chan(KEY155(i,j),KEY156(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak154(num)= chan(KEY157(i,j),KEY158(i,j));
      num=num+1;
end
end


for i=1:(m1*n1)
          dnar161(i)=dnasubt(dna151(i),(dnaaddtion(dnar151(i),dnak151(i))));
          dna161(i)=dnar151(i);

          dnar162(i)=dnasubt(dna152(i),(dnaaddtion(dnar152(i),dnak152(i))));
          dna162(i)=dnar152(i);

          dnar163(i)=dnasubt(dna153(i),(dnaaddtion(dnar153(i),dnak153(i))));
          dna163(i)=dnar153(i);
         
          dnar164(i)=dnasubt(dna154(i),(dnaaddtion(dnar154(i),dnak154(i))));
          dna164(i)=dnar154(i);


end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%第16轮
for i=1:8
     Array{i} =bitget(KEY{16},i);
end 
KEY161=Array{1};
KEY162=Array{2};
KEY163=Array{3};
KEY164=Array{4};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KEY165=Array{5};
KEY166=Array{6};
KEY167=Array{7};
KEY168=Array{8};

num=1;
for i=1:m1
 for j=1:n1
      dnak161(num)= chan(KEY161(i,j),KEY162(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak162(num)= chan(KEY163(i,j),KEY164(i,j));
      num=num+1;
end
end

num=1;
for i=1:m1
 for j=1:n1
      dnak163(num)= chan(KEY165(i,j),KEY166(i,j));
      num=num+1;
end
end
num=1;
for i=1:m1
 for j=1:n1
      dnak164(num)= chan(KEY167(i,j),KEY168(i,j));
      num=num+1;
end
end


for i=1:(m1*n1)
          dnar171(i)=dnasubt(dna161(i),(dnaaddtion(dnar161(i),dnak161(i))));
          dna171(i)=dnar161(i);

          dnar172(i)=dnasubt(dna162(i),(dnaaddtion(dnar162(i),dnak162(i))));
          dna172(i)=dnar162(i);

          dnar173(i)=dnasubt(dna163(i),(dnaaddtion(dnar163(i),dnak163(i))));
          dna173(i)=dnar163(i);
         
          dnar174(i)=dnasubt(dna164(i),(dnaaddtion(dnar164(i),dnak164(i))));
          dna174(i)=dnar164(i);


end

for i=1:(m1*n1)
     [R181(i),R182(i)]= dechan(dnar171(i));
     [R183(i),R184(i)]= dechan(dnar172(i));
     [R185(i),R186(i)]= dechan(dnar173(i));
     [R187(i),R188(i)]= dechan(dnar174(i));

     [L181(i),L182(i)]= dechan(dna171(i));
     [L183(i),L184(i)]= dechan(dna172(i));
     [L185(i),L186(i)]= dechan(dna173(i));
     [L187(i),L188(i)]= dechan(dna174(i));

end

Array{1}=reshape(R181,256,128);
Array{2}=reshape(R182,256,128);
Array{3}=reshape(R183,256,128);
Array{4}=reshape(R184,256,128);
Array{5}=reshape(R185,256,128);
Array{6}=reshape(R186,256,128);
Array{7}=reshape(R187,256,128);
Array{8}=reshape(R188,256,128);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[M,N]=size(Array{1});
RC=zeros(M,N);

for i=1:8
  RC=bitset(RC,i,Array{i});
end


Array1{1}=reshape(L181,256,128);
Array1{2}=reshape(L182,256,128);
Array1{3}=reshape(L183,256,128);
Array1{4}=reshape(L184,256,128);
Array1{5}=reshape(L185,256,128);
Array1{6}=reshape(L186,256,128);
Array1{7}=reshape(L187,256,128);
Array1{8}=reshape(R188,256,128);
[M,N]=size(Array1{1});
LC=zeros(M,N);

for i=1:8
  LC=bitset(LC,i,Array1{i});
end


res=[LC,RC];
[m,n]=size(res);
PPP=[];
PPPC=[];
num=1;
for i=1:m
for j=1:n
    PPP(num)=res(i,j);
    num=num+1;
end
end
PPPC(1)=PPP(1);
for i=2:(m*n)
    PPPC(i)=bitxor(PPP(i),PPPC(i-1));
end
PPPC=reshape(PPPC,256,256);

figure(3)
imshow(uint8(PPPC));
figure(4);
imhist(uint8(PPPC));

 

function U=chan(h,k)
re=0;
if (h==0)&&(k==0)
    re='A';
end
 if (h==0)&&(k==1)
    re='G';
end
 if (h==1)&&(k==0)
    re='C';
end
 if (h==1)&&(k==1)
    re='T';
end

U=re;
return;

function [h,k]=dechan(P)
h=0;k=0;
if  P=='A'
    h=0;k=0;
end
if  P=='G'
    h=0;k=1;
end
if  P=='C'
    h=1;k=0;
end
 if  P=='T'
     h=1;k=1;
end


return;

 

 

function U=dnaaddtion(h,k)
U=0;
if (h=='A')&&(k=='A')
     U='A';
end

if (h=='A')&&(k=='G')
     U='G';
end

if (h=='A')&&(k=='C')
     U='C';
end

if (h=='A')&&(k=='T')
     U='T';
end

if (h=='G')&&(k=='A')
     U='G';
end

if (h=='G')&&(k=='G')
     U='C';
end


if (h=='G')&&(k=='C')
     U='T';
end


if (h=='G')&&(k=='T')
     U='A';
end

if (h=='C')&&(k=='A')
     U='C';
end


if (h=='C')&&(k=='G')
     U='T';
end

if (h=='C')&&(k=='C')
     U='A';
end

if (h=='C')&&(k=='T')
     U='G';
end

if (h=='T')&&(k=='A')
     U='T';
end

if (h=='T')&&(k=='G')
     U='A';
end

if (h=='T')&&(k=='C')
     U='G';
end

if (h=='T')&&(k=='T')
     U='C';
end
return;

 

function U=dnasubt(h,k)
U=0;
if (h=='A')&&(k=='A')
     U='A';
end

if (h=='A')&&(k=='G')
     U='T';
end

if (h=='A')&&(k=='C')
     U='C';
end

if (h=='A')&&(k=='T')
     U='G';
end

if (h=='G')&&(k=='A')
     U='G';
end

if (h=='G')&&(k=='G')
     U='A';
end


if (h=='G')&&(k=='C')
     U='T';
end


if (h=='G')&&(k=='T')
     U='C';
end

if (h=='C')&&(k=='A')
     U='C';
end


if (h=='C')&&(k=='G')
     U='G';
end

if (h=='C')&&(k=='C')
     U='A';
end

if (h=='C')&&(k=='T')
     U='T';
end

if (h=='T')&&(k=='A')
     U='T';
end

if (h=='T')&&(k=='G')
     U='C';
end

if (h=='T')&&(k=='C')
     U='G';
end

if (h=='T')&&(k=='T')
     U='A';
end
return;
 

 

function h = hash(inp,meth) 
% HASH - Convert an input variable into a message digest using any of 
%        several common hash algorithms 

% USAGE: h = hash(inp,'meth') 

% inp  = input variable, of any of the following classes: 
%        char, uint8, logical, double, single, int8, uint8, 
%        int16, uint16, int32, uint32, int64, uint64 
% h    = hash digest output, in hexadecimal notation 
% meth = hash algorithm, which is one of the following: 
%        MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512  

% NOTES: (1) If the input is a string or uint8 variable, it is hashed 
%            as usual for a byte stream. Other classes are converted into 
%            their byte-stream values. In other words, the hash of the 
%            following will be identical: 
%                     'abc' 
%                     uint8('abc') 
%                     char([97 98 99]) 
%            The hash of the follwing will be different from the above, 
%            because class "double" uses eight byte elements: 
%                     double('abc') 
%                     [97 98 99] 
%            You can avoid this issue by making sure that your inputs 
%            are strings or uint8 arrays. 
%        (2) The name of the hash algorithm may be specified in lowercase 
%            and/or without the hyphen, if desired. For example, 
%            h=hash('my text to hash','sha256'); 
%        (3) Carefully tested, but no warranty. Use at your own risk. 
%        (4) Michael Kleder, Nov 2005 

% EXAMPLE: 

% algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 
% for n=1:6 
%     h=hash('my sample text',algs{n}); 
%     disp([algs{n} ' (' num2str(length(h)*4) ' bits):']) 
%     disp(h) 
% end 

inp=inp(:); 
% convert strings and logicals into uint8 format 
if ischar(inp) || islogical(inp) 
    inp=uint8(inp); 
else % convert everything else into uint8 format without loss of data 
    inp=typecast(inp,'uint8'); 
end 

% verify hash method, with some syntactical forgiveness: 
meth=upper(meth); 
switch meth 
    case 'SHA1' 
        meth='SHA-1'; 
    case 'SHA256' 
        meth='SHA-256'; 
    case 'SHA384' 
        meth='SHA-384'; 
    case 'SHA512' 
        meth='SHA-512'; 
    otherwise 
end 
algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'}; 
if isempty(strmatch(meth,algs,'exact')) 
    error(['Hash algorithm must be ' ... 
        'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']); 
end 

% create hash 
x=java.security.MessageDigest.getInstance(meth); 
x.update(inp); 
h=typecast(x.digest,'uint8'); 
h=dec2hex(h)'; 
if(size(h,1))==1 % remote possibility: all hash bytes  128, so pad: 
    h=[repmat('0',[1 size(h,2)]);h]; 
end 
h=lower(h(:)'); 
clear x 
return
 

你可能感兴趣的:(基于DNA编码的图像加密)