天天看點

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

1.軟體版本

matlab2013b

2.本算法理論知識

算法的基本流程如下所示:

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

SubBytes

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

 S-Box

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

ShiftRows

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

Mix Columns

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

AddRoundKey

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

 3.核心代碼

function varargout = main(varargin)
% MAIN MATLAB code for main.fig
%      MAIN, by itself, creates a new MAIN or raises the existing
%      singleton*.
%
%      H = MAIN returns the handle to a new MAIN or the handle to
%      the existing singleton*.
%
%      MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MAIN.M with the given input arguments.
%
%      MAIN('Property','Value',...) creates a new MAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before main_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to main_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 main

% Last Modified by GUIDE v2.5 28-May-2012 22:27:15

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @main_OpeningFcn, ...
                   'gui_OutputFcn',  @main_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 main is made visible.
function main_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 main (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = main_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;


% --- Executes on button press in pushbutton1.
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)
global Image_RGB;

[FileName,PathName] = uigetfile('*.*','選擇圖像');
FullPathName        = [PathName,'\',FileName];
Image_RGB           = rgb2gray(imread(FullPathName));
Image_RGB           = imresize(Image_RGB,[128,128]);
axes(handles.axes1);
imshow(Image_RGB);
% --- Executes on button press in pushbutton2.
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)
global Image_RGB;
global SBOX;
global invSBOX;
global w;
global polys;
global invpolys;
global images_AES;

[rr,cc] = size(Image_RGB);
%對RGB三個通道分别進行加密處理
for i = 1:rr/16
    for j = 1:cc
        M_data2{i,j} = Image_RGB(16*(i-1)+1:16*i , j,1);
    end
end

%設定密鑰
key_hex = {'00' '01' '02' '03' '04' '05' '06' '07' '08' '09' '0a' '0b' '0c' '0d' '0e' '0f'};
%AES初始化

[SBOX,invSBOX,w,polys,invpolys] = func_AES_parameter(key_hex); 

%加密處理
for i = 1:rr/16
    for j = 1:cc
        images_AES{i,j} = func_AES(double(M_data2{i,j}),w,SBOX,polys,1);
    end
end

%顯示加密後的圖像
for i = 1:rr/16
    for j = 1:cc
        tmp                           = (images_AES{i,j})';
        iamges_aes(16*(i-1)+1:16*i,j) = double(tmp);
    end
end
axes(handles.axes2);
imshow(iamges_aes,[]);
 

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Image_RGB;
global SBOX;
global invSBOX;
global w;
global polys;
global invpolys;
global images_AES;

[rr,cc] = size(Image_RGB);
%解密處理
for i = 1:rr/16
    for j = 1:cc
        images_deAES{i,j} = func_invAES(images_AES{i,j},w,invSBOX,invpolys,1);
    end
end 
%顯示解密後的圖像
for i = 1:rr/16
    for j = 1:cc
        tmp                             = (images_deAES{i,j})';
        iamges_deaes(16*(i-1)+1:16*i,j) = double(tmp);
    end
end

axes(handles.axes3);
imshow(iamges_deaes,[]);








% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc;
clear;
close all;      

4.操作步驟與仿真結論

AES界面如下所示:

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

AES加密如下所示:

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

AES解密如下所示:

【AES圖像加解密】基于AES圖像加解密算法的MATLAB仿真

5.參考文獻

AES的理論主要依據參考文獻,