1 简介
组合数学中的许多课题都与有趣的数学游戏息息相关,比如科克曼女生问题,哥尼斯堡七桥问题,Fibonacci数列,幻方问题等.对这些数学问题,人们经过了几十年甚至几百年的努力,已取得一些成果,并逐步应用于人们的实际生活中.组合数学中一个重要的成员"拉丁方",源自于"三十六军官问题".由瑞士著名的数学大师欧拉开始研究.从1779年算起已有200多年的历史,人们在这方面取得了大量的研究成果.近几十年来,拉丁方的相关理论研究和各种应用研究成为热点.尤其是在通信编码和信息加密这两个大的方面.比如,纠错码构造,等重码构造,跳频网络通信,光正交码构造,图像加密,消息认证码构造等.
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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%% 加密函数 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see 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'));
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
case 1
G=imread('lena256.bmp');
set(gcf,'CurrentAxes',h1);
imshow(G);
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);
I=G;
[u,v]=zhengjiaodui(X,Y);
zhiluan(I,Times,u,v,1);
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');
%--------------------------------------
case 2
G=imread('LENA256.BMP');
set(gcf,'CurrentAxes',h1);
imshow(G);
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))
Errstr = '参数1必须为整数!';
end
if ~isempty(Errstr)
errordlg(Errstr);
return
end
Errstr = '';
if isnan(D(2)) | ~(floor(D(2)) == D(2))
Errstr = '参数2必须为整数!';
end
if ~isempty(Errstr)
errordlg(Errstr);
return
end
%-----------验证结束
mousefrm(0,'watch');
set(gcf,'CurrentAxes',h2);
positiontrans(G,Times,X,Y);
title('加密后');
mousefrm(0,'arrow');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%% 解密函数 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see 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
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
%%%%%%%%%%%%%%%%%%%%%十进制整数转换为二进制矩阵%%%%%%%%%%%%%%%%%%%%%%
function r=dectobin(t)
r=zeros(1,8);
for i=1:8;
r(9-i)=mod(t,2);
t=fix(t/2);
end
%%%%%%%%%%%%%%%%%%%%%%%%有限域加法处理程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Add=modAdd(x,y)
p=dectobin(x);
q=dectobin(y);
r=mod(p+q,2);
for k=1:7
r(k)=r(k)*2^(8-k);
end
Add=sum(r);
%%%%%%%%%%%%%%%%%%%%%%%%有限域乘法处理程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Mul=modMul(x,y)
mx=[1 0 0 0 0 0 0 1 1];
p=dectobin(x);
q=dectobin(y);
z=zeros(15,8);
for i=8:-1:1
z(9-i,i:(i+7))=q(i)*p;
end
result1=mod(sum(z),2);
for i=1:7
if result1(i)==1
result1(i:i+8)=result1(i:(i+8))+mx;
result1(i:i+8)=mod(result1(i:i+8),2);
end
end
result2=result1(8:15);
for k=1:7
result2(k)=result2(k)*2^(8-k);
end
Mul=sum(result2);
%%%%%%%%%%%%%%%%%%%%%%%%正交拉丁方计算程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [u,v]=zhengjiaodui(a,b)
u=zeros(256);
v=zeros(256);
for i=1:256
for j=1:256
u(i,j)=modAdd(modMul(a,i),j);
v(i,j)=modAdd(modMul(b,i),j);
end
end
%%%%%%%%%%%%%%%%%%正交拉丁方加密、解密程序%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--I为待处理的图像数据;t为变换次数;u、v分别为拉丁方;
%--flag加密解密标志符(1为加密,其它为解密)
function zhiluan(I,t,u,v,flag)
if(flag==1)
for d=1:t
for x=1:256
for y=1:256
B(x,y)=I(u(x,y)+1,v(x,y)+1);
end
end
I=B;
end
imwrite(I,'code.bmp');
else
for d=1:t
for x=1:256
for y=1:256
B(u(x,y)+1,v(x,y)+1)=I(x,y);
end
end
I=B;
end
end
imshow(B);
%%%%%%%%%%%%%%%%%%%%%%二维Arnold变换函数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function positiontrans(G,t,x0,y0)
N=256;
for i=1:t;
for x=1:N
for y=1:N
B(mod(x+y+x0,N)+1,mod(x+2*y+y0,N)+1)=G(x,y);
end
end
G=B;
end
imshow(G);
3 仿真结果
4 参考文献
[1]杨刘洋. 拉丁方在二维光正交码和图像加密中的应用研究[D]. 浙江师范大学, 2015.