【图像加解密】基于正交拉丁方置乱算法实现图像加解密matlab代码

158 阅读6分钟

1 简介

为了提高图像加密算法的安全性和减少加密中使用的拉丁方数量,提出了一种新的基于完备拉丁方的图像加密算法。通过拉丁方与图像的灰度值矩阵进行异或运算,以提高安全性;再利用完备拉丁方扩展后的矩阵与正交拉丁方对相似这一性质,进行图像的像素位置置乱,减少加密过程中使用的拉丁方数量。仿真结果和参数分析表明,该加密算法安全性好,加密和解密效果理想,属于一种无损加密算法。同正交拉丁方加密方案相比,算法的适用范围更加广泛,安全性更高。

2 部分代码

function varargout = ladingfang(varargin)
% LADINGFANG M-file for ladingfang.fig
%     LADINGFANG, by itself, creates a new LADINGFANG or raises the existing
%     singleton*.
%
%     H = LADINGFANG returns the handle to a new LADINGFANG or the handle to
%     the existing singleton*.
%
%     LADINGFANG('CALLBACK',hObject,eventData,handles,...) calls the local
%     function named CALLBACK in LADINGFANG.M with the given input arguments.
%
%     LADINGFANG('Property','Value',...) creates a new LADINGFANG or raises the
%     existing singleton*. Starting from the left, property value pairs are
%     applied to the GUI before ladingfang_OpeningFunction gets called. An
%     unrecognized property name or invalid value makes property application
%     stop. All inputs are passed to ladingfang_OpeningFcn via varargin.
%
%     *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%     instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ladingfang

% Last Modified by GUIDE v2.5 05-Jan-2005 22:18:40

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                  'gui_Singleton',  gui_Singleton, ...
                  'gui_OpeningFcn', @ladingfang_OpeningFcn, ...
                  'gui_OutputFcn',  @ladingfang_OutputFcn, ...
                  'gui_LayoutFcn', [] , ...
                  'gui_Callback',   []);
if nargin & isstr(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
   gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before ladingfang is made visible.
function ladingfang_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject   handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ladingfang (see VARARGIN)

% Choose default command line output for ladingfang
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using ladingfang.
if strcmp(get(hObject,'Visible'),'off')
%   plot(rand(5));
end
global h1 h2
h1=handles.axes1;
h2=handles.axes2;
%-----------------初始化界面属性
set(handles.edit1,'string','1');
set(handles.edit2,'string','2');
set(handles.edit3,'string','125');
set(handles.pushbutton2,'Visible','off');
G=imread('LENA256.BMP');
set(gcf,'CurrentAxes',h1);
imshow(G);
title('原图像');
set(h2,'Visible','off');

% UIWAIT makes ladingfang wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ladingfang_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject   handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
e GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
axes(handles.axes1);
cla;
global  h1 h2 
Times = str2num(get(handles.edit1, 'string'));
X= str2num(get(handles.edit2, 'string'));
Y= str2num(get(handles.edit3, 'string'));
Times = str2num(get(handles.edit1, 'string'));
UNCODE=imread('code.bmp');
set(gcf,'CurrentAxes',h1);
imshow(UNCODE);
title('解密前');
%-----验证输入数据的有效性
  N = str2double(get(handles.edit1,'String'));
  Errstr = '';
  if isnan(N) | ~(floor(N) == N) | (N <= 0)
     Errstr = '变换次数必须为正整数!';
  end
  if ~isempty(Errstr)
     errordlg(Errstr);
     return
  end
  D(1) =str2double(get(handles.edit2,'String'));
  D(2) =str2double(get(handles.edit3,'String'));
  Errstr = '';
  if isnan(D(1)) | ~(floor(D(1)) == D(1)) | (D(1) < 1)| (D(1)>255)
     Errstr = '参数1必须为1至255正整数!';
  end
  if ~isempty(Errstr)
     errordlg(Errstr);
     return
  end
  Errstr = '';
  if isnan(D(2)) | ~(floor(D(2)) == D(2)) | (D(2) < 1)| (D(1)>255)
     Errstr = '参数2必须为1至255正整数!';
  end
  if ~isempty(Errstr)
     errordlg(Errstr);
     return
  end
  
  if D(1) == D(2)
     Errstr = '参数1与参数2必须不同!';
  end
  if ~isempty(Errstr)
     errordlg(Errstr);
     return
  end
%-----------验证结束   
       %-----------计算期间,禁用部分控件
       mousefrm(0,'watch'); 
       set(handles.popupmenu1,'enable','off');
       set(handles.popupmenu2,'enable','off');
       set(handles.edit1,'enable','off');
       set(handles.edit2,'enable','off');
       set(handles.edit3,'enable','off');
       set(handles.pushbutton1,'enable','off');
       set(handles.pushbutton2,'enable','off');
       %----------------------------------------
set(gcf,'CurrentAxes',h2);
[u,v]=zhengjiaodui(X,Y);
zhiluan(UNCODE,Times,u,v,2);
title('解密后');
%----------恢复控件的可用性
set(handles.popupmenu1,'enable','on');
set(handles.popupmenu2,'enable','on');
set(handles.edit1,'enable','on');
set(handles.edit2,'enable','on');
set(handles.edit3,'enable','on');
set(handles.pushbutton1,'enable','on');
set(handles.pushbutton2,'enable','on');
mousefrm(0,'arrow'); 
%--------------------------------------


% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject   handle to FileMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject   handle to OpenMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
   open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject   handle to PrintMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject   handle to CloseMenuItem (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name''?'],...
                    ['Close ' get(handles.figure1,'Name''...'],...
                    'Yes','No','Yes');
if strcmp(selection,'No')
   return;
end

delete(handles.figure1)


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject   handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
   set(hObject,'BackgroundColor','white');
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

set(hObject, 'String', {'正交拉丁方置乱''二维Arnold置乱'});


%%%%%%%%%%%%%%%%%%% 用以选择加密算法后的界面更新 %%%%%%%%%%%%%%%%%%%%%%%%%
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject   handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%       contents{get(hObject,'Value')} returns selected item from popupmenu3
global  h1 h2
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
   case 1
       G=imread('LENA256.BMP');
       set(gcf,'CurrentAxes',h1);
       imshow(G);
       title('原图像');
       set(handles.popupmenu2,'Visible','on');
       set(handles.popupmenu2,'Value',1);
       set(handles.pushbutton2,'Visible','off');
       set(handles.pushbutton1,'Visible','on');
       set(gcf,'CurrentAxes',h2);
       title('');
       cla;
   case 2
       G=imread('LENA256.BMP');
       set(gcf,'CurrentAxes',h1);
       imshow(G);
       title('原图像');
       set(handles.popupmenu2,'Visible','off');
       set(handles.pushbutton2,'Visible','off');
       set(handles.pushbutton1,'Visible','on');
       set(gcf,'CurrentAxes',h2);
       title('');
       cla;
end


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject   handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
   set(hObject,'BackgroundColor','white');
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
set(hObject, 'String', {'加密''解密'});


%%%%%%%%%%%%%%%%%%% 用以选择加密解密操作后的界面更新 %%%%%%%%%%%%%%%%%%%%%%%%%
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject   handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
%       contents{get(hObject,'Value')} returns selected item from popupmenu2
global  h1 h2
popup_sel_index = get(handles.popupmenu1, 'Value');
popup_sel_index2 = get(handles.popupmenu2, 'Value');
switch popup_sel_index2
   case 1
       set(handles.pushbutton2,'Visible','off');
       set(handles.pushbutton1,'Visible','on');
       G=imread('lena256.bmp');
       set(gcf,'CurrentAxes',h1);
       imshow(G);
       title('加密前');
       set(gcf,'CurrentAxes',h2);
       title('');
       cla;
   case 2
       set(handles.pushbutton2,'Visible','on');
       set(handles.pushbutton1,'Visible','off');
       G=imread('code.bmp');
       set(gcf,'CurrentAxes',h1);
       imshow(G);
       title('解密前');
       set(gcf,'CurrentAxes',h2);
       title('');
       cla;
end


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
   set(hObject,'BackgroundColor','white');
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit1_Callback(hObject, eventdata, handles)
% hObject   handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%       str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
   set(hObject,'BackgroundColor','white');
else
   set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

3 仿真结果

4 参考文献

[1]杨刘洋, 吕翔. 一种基于完备拉丁方的图像加密算法[J]. 计算机应用研究, 2015(11):3435-3438.

部分理论引用网络文献,若有侵权联系博主删除。

5 MATLAB代码与数据下载地址

见博客主页