混沌对图像的置乱

 

加密函数:

 

function myencryption %对图像进行加密,采用置乱的方式 clear; clc; %key为加密密钥 %os原始图像的路径 [filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入需要加密的图像'); os=[pathname filename]; [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后的图像'); ozls=[pathname filename]; % key=0.98; %%%%%%%%%%%%读入混沌初始值 prompt={'混沌初始值 '}; myTitle='initial value'; lines=1; def={'98'}; mya=inputdlg(prompt,myTitle,lines,def,'on'); key11=['0.',mya{1}]; key=str2num(key11); %%%%%%%%%%%% o=imread(os); % figure,imshow(o); %计算原始图像的大小 [m n]=size(o); %l=zeros(m,n); l=linspace(0,0,m*n); l(1)=key; for i=2:m*n l(i)=1-2*l(i-1)*l(i-1); end % %对生成的混沌序列进行排序 % [lsort lindex]=sort(l); % t=linspace(0,0,m*n); % t(1)=0.98; % for i=1:m*n % t(i+1)=1-2*t(i)*t(i); % end % for i=1:m*n % if (l(i)>=0) % l(i)=1; % else % l(i)=0; % end % end % l=~l; % subplot(2,2,2),imshow(l),title('混沌图像'); [lsort,lindex]=sort(l); %ozl =original zhi luan 表示置乱后的原始图像 ozl=zeros(m,n); for i=1:m*n ozl(i)=o(lindex(i)); end % ozl=reshape(ozl,m,n); %%oo =original fu yuan % ofy=zeros(m,n); % for i=1:m*n % ofy(lindex(i))=ozl(i); % end % h= waitbar(0,'程序处理中,请耐心等待。。。'); % for i=1:100, % computation here % % waitbar(i/100) % end % close(h) ; %首先保存需要加密的图像 ozl=uint8(ozl); ozl=double(ozl); imwrite(ozl,ozls); subplot(1,2,1) imshow(o) % title('原始图像') set(gca,'Title',text('String','原始图像')) subplot(1,2,2),imshow(ozl,[]); set(gca,'Title',text('String','加密后图像')) % title('加密后图像'); % [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后图像'); % os=[pathname filename]; % % [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存图像'); % % os=[pathname filename]; % imwrite(ozl,os); % subplot(1,3,3),imshow(ofy,[]),title('复原图像'); % [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后的图像'); % os=[pathname filename]; % %进度条 % % h= waitbar(0,'程序处理中,请耐心等待。。。'); % % for i=1:100, % computation here % % % waitbar(i/100); % % end % % close(h) ; % ozl=uint8(ozl); % imwrite(ozl,os); % figure,imshow(ozl,[]); % test=imread(os); % figure,imshow(test,[]); 

 

解密函数:

 

function mydecryption %对加密后的图像进行解密 clear; clc; % key=0.98 %key为解密密钥 %os原始图像的路径 [filename pathname]=uigetfile('*.bmp;*.tiff;*.tif', '读入需要解密的图像'); os=[pathname filename]; [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存解密后的图像'); ofys=[pathname filename]; ozl=imread(os); % figure,imshow(ozl,[]); %%%%%%%%%%%%%设置混沌初始值 prompt={'混沌初始值 '}; myTitle='initial value'; lines=1; def={'98'}; mya=inputdlg(prompt,myTitle,lines,def,'on'); key11=['0.',mya{1}]; key=str2num(key11); %%%%%%%%%%%%设置混沌初始值 %计算原始图像的大小 [m n]=size(ozl); %l=zeros(m,n); l=linspace(0,0,m*n); l(1)=key; for i=2:m*n l(i)=1-2*l(i-1)*l(i-1); end % %对生成的混沌序列进行排序 % [lsort lindex]=sort(l); % t=linspace(0,0,m*n); % t(1)=0.98; % for i=1:m*n % t(i+1)=1-2*t(i)*t(i); % end % for i=1:m*n % if (l(i)>=0) % l(i)=1; % else % l(i)=0; % end % end % l=~l; % subplot(2,2,2),imshow(l),title('混沌图像'); [lsort,lindex]=sort(l); %ozl =original zhi luan 表示置乱后的原始图像 % ozl=zeros(m,n); % for i=1:m*n % ozl(i)=o(lindex(i)); % end % ozl=reshape(ozl,m,n); %%oo =original fu yuan ofy=zeros(m,n); for i=1:m*n ofy(lindex(i))=ozl(i); end % ozl=uint8(ozl); ozl=double(ozl); imwrite(ofy,ofys); h= waitbar(0,'程序处理中,请耐心等待。。。'); for i=1:100, % computation here % waitbar(i/100) end close(h) ; subplot(1,2,1),imshow(ozl,[]); %,title('需要解密的图像'); set(gca,'Title',text('String','需要解密的图像')) subplot(1,2,2),imshow(ofy,[]); %,title('解密后图像'); set(gca,'Title',text('String','解密后图像')) % [filename pathname]=uiputfile({'*.bmp';'*.tiff';'*.tif'}, '保存加密后图像'); % os=[pathname filename]; % imwrite(ozl,os); % subplot(1,3,3),imshow(ofy,[]),title('复原图像');  

你可能感兴趣的:(加密,String,function,解密,OS,OO)