密碼学上机实验二报告(Matlab).doc
文本预览下载声明
信息安全与密码学实验报告
序号 班级 姓名 学号 日期 时间 地点 1 信计1002 王志新 2012.12.10 18:00-21:00 实验楼102 指导教师:候书会 实验名称:
1、上机实验二 实验内容:
实验四 现代对称密码学实验(验证性实验,4学时)
AES
A、实验内容:熟悉AES算法,并实现AES;
B、测试数据:任意,并验证加/解密结果。
程序清单:
程序代码如下所示:
function plaintext = inv_cipher (ciphertext, w, inv_s_box, inv_poly_mat, vargin)
if nargin 0
% Switch the verbose mode flag on
verbose_mode = 1;
% If there is no optional verbose mode argument
else
% Switch the verbose mode flag off
verbose_mode = 0;
end
% If the input vector is a cell array or does not have 16 elements
if iscell (ciphertext) | prod (size (ciphertext)) ~= 16
% Inform user and abort
error (Ciphertext has to be a vector (not a cell array) with 16 elements.)
end
% If any element of the input vector cannot be represented by 8 bits
if any (ciphertext 0 | ciphertext 255)
% Inform user and abort
error (Elements of ciphertext vector have to be bytes (0 = ciphertext(i) = 255).)
end
% If the expanded key array is a cell arrray or does not have the correct size
if iscell (w) | any (size (w) ~= [44, 4])
% Inform user and abort
error (w has to be an array (not a cell array) with [44 x 4] elements.)
end
if any (w 0 | w 255)
% Inform user and abort
error (Elements of key array w have to be bytes (0 = w(i,j) = 255).)
end
state = reshape (ciphertext, 4, 4);
% Display intermediate result if requested
if verbose_mode
disp_hex (Initial state : , state)
end
round_key = (w(41:44, :));
% Display intermediate result if requested
if verbose_mode
disp_hex (Initial round key : , round_key)
end
% Add (xor) the current round key (matrix) to the state (matrix)
state = add_round_key (state, round_key);
% Loop over 9 rounds backwards
for i_round = 9 : -1 : 1
% Display intermediate result if requested
if verbose_mode
disp_hex ([State at start of round , num2str(i_round)
显示全部