function plaintext = inv_cipher (ciphertext, w, inv_s_box, inv_poly_mat, vargin)
%INV_CIPHER Convert 16 bytes of ciphertext to 16 bytes of plaintext.
%
% PLAINTEXT = INV_CIPHER (CIPHERTEXT, W, INV_S_BOX, INV_POLY_MAT)
% converts CIPHERTEXT (back) to the plaintext PLAINTEXT,
% using the expanded cipher key W,
% the inverse byte substitution table INV_S_BOX, and
% the inverse transformation matrix INV_POLY_MAT.
%
% PLAINTEXT = INV_CIPHER (CIPHERTEXT, W, INV_S_BOX, INV_POLY_MAT, 1)
% switches verbose mode on, that displays intermediate results.
% CIPHERTEXT has to be a vector of 16 bytes (0 <= CIPHERTEXT(i) <= 255).
% W has to be a [44 x 4]-matrix of bytes (0 <= W(i,j) <= 255).
% Copyright 2001-2005, J. J. Buchholz, Hochschule Bremen, [email protected]
% Version 1.0 30.05.2001
% If there is an optional "verbose mode" argument
if nargin > 4
% 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 element of the expanded key array can not be represented by 8 bits
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
% Display headline if requested
if verbose_mode
disp (' ')
disp ('********************************************')
disp ('* *')
disp ('* I N V E R S E C I P H E R *')
disp ('* *')
disp ('********************************************')
disp (' ')
end
% Copy the 16 elements of the input vector column-wise into the 4 x 4 state matrix
state = reshape (ciphertext, 4, 4);
% Display intermediate result if requested
if verbose_mode
disp_hex ('Initial state : ', state)
end
% Copy the last 4 rows (4 x 4 elements) of the expanded key
% into the current round key.
% Transpose to make this column-wise
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),' : '], state)
end
% Cyclically shift the last three rows of the state matrix
state = inv_shift_rows (state);
% Display intermediate result if requested
if verbose_mode
disp_hex ('After inv_shift_rows : ', state)
end
% Substitute all 16 elements of the state matrix
% by shoving them through the S-box
state = sub_bytes (state, inv_s_box);
% Display intermediate result if requested
if verbose_mode
disp_hex ('After inv_sub_bytes : ', state)
end
% Extract the current round key (4 x 4 matrix) from the expanded key
round_key = (w((1:4) + 4*i_round, :))';
% Display intermediate result if requested
if verbose_mode
disp_hex ('Round key : ', round_key)
end
% Add (XOR) the current round key (matrix) to the state (matrix)
state = add_round_key (state, round_key);
% Display intermediate result if requested
if verbose_mode
disp_hex ('After add_round_key : ', state)
end
% Transform the columns of the state matrix via a four-term polynomial.
% Use the same function (mix_columns) as in cipher,
% but with the inverse polynomial matrix
state = mix_columns (state, inv_poly_mat);
end
% Display intermediate result if requested
if verbose_mode
disp_hex ('State at start of final round : ', state)
end
% Cyclically shift the last three rows of the state matrix
state = inv_shift_rows (state);
% Display intermediate result if requested
if verbose_mode
disp_hex ('After inv_shift_rows : ', state)
end
% Substitute all 16 elements of the state matrix
% by shoving them through the inverse S-box
state = sub_bytes (state, inv_s_box);
% Display intermediate result if requested
if verbose_mode
disp_hex ('After inv_sub_bytes : ', state)
end
% Extract the "first" (final) round key (4 x 4 matrix) from the expanded key
round_key = (w(1:4, :))';
% Display intermediate result if requested
if verbose_mode
disp_hex ('Round key : ', round_key)
end
% Add (xor) the current round key (matrix) to the state (matrix)
state = add_round_key (state, round_key);
% Display intermediate result if requested
if verbose_mode
disp_hex ('Final state : ', state)
end
% reshape the 4 x 4 state matrix into a 16 element row vector
plaintext = reshape (state, 1, 16);
没有合适的资源?快使用搜索试试~ 我知道了~
比较完整的AES加解密仿真输出sbox-逆sbox-key-Rcon-poly矩阵-加解密过程等关键信息-源码
共20个文件
m:20个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 120 浏览量
2021-10-01
23:43:17
上传
评论
收藏 26KB ZIP 举报
温馨提示
比较完整的AES加解密仿真输出sbox_逆sbox_key_Rcon_poly矩阵_加解密过程等关键信息_源码
资源推荐
资源详情
资源评论
收起资源包目录
比较完整的AES加解密仿真输出sbox_逆sbox_key_Rcon_poly矩阵_加解密过程等关键信息_源码.zip (20个子文件)
比较完整的AES加解密仿真输出sbox_逆sbox_key_Rcon_poly矩阵_加解密过程等关键信息_源码
sub_bytes.m 659B
find_inverse.m 1KB
poly_mat_gen.m 2KB
add_round_key.m 539B
mix_columns.m 2KB
cipher.m 6KB
aff_trans.m 1KB
inv_cipher.m 6KB
disp_hex.m 1KB
aes_init.m 1KB
key_expansion.m 4KB
aes_demo.m 2KB
poly_mult.m 2KB
cycle.m 2KB
s_box_inversion.m 644B
rot_word.m 416B
s_box_gen.m 3KB
inv_shift_rows.m 756B
rcon_gen.m 2KB
shift_rows.m 731B
共 20 条
- 1
资源评论
mYlEaVeiSmVp
- 粉丝: 1921
- 资源: 19万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功