董凤鸿-41040317-信计1002-密码学2.docx_第1页
董凤鸿-41040317-信计1002-密码学2.docx_第2页
董凤鸿-41040317-信计1002-密码学2.docx_第3页
董凤鸿-41040317-信计1002-密码学2.docx_第4页
董凤鸿-41040317-信计1002-密码学2.docx_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

董凤鸿-41040317-信计1002-密码学实验报告2密码学实验报告序号班级姓名学号日期时间地点2信计1002董凤鸿410403178月10日6:00-9:00实验楼102指导教师:实验名称:现代对称密码学实验实验任务:1、 AES A、实验内容:熟悉AES算法,并实现AES;B、测试数据:任意,并验证加/解密结果。程序清单:(1) 公用的一些函数function state_out = add_round_key (state_in, round_key)state_out = bitxor (state_in, round_key);function s_box, inv_s_box, w, poly_mat, inv_poly_mat = aes_initclcs_box, inv_s_box = s_box_gen (1);rcon = rcon_gen (1);key_hex = 00 01 02 03 04 05 06 07 . 08 09 0a 0b 0c 0d 0e 0f;key = hex2dec(key_hex);w = key_expansion (key, s_box, rcon, 1);poly_mat, inv_poly_mat = poly_mat_gen (1);function b_out = aff_trans (b_in)mod_pol = bin2dec (100000001);mult_pol = bin2dec (00011111);add_pol = bin2dec (01100011);temp = poly_mult (b_in, mult_pol, mod_pol);b_out = bitxor (temp, add_pol);function matrix_out = cycle (matrix_in, direction)if strcmp (direction, left) col = (0 : 5 : 15); else col = (16 : -3 : 7); endrow = 0 : 4 : 12; cols = repmat (col, 1, 4); rows = repmat (row, 4, 1); ind_mat = mod (rows + cols, 16) + 1;matrix_out = matrix_in (ind_mat);function disp_hex (string, hex_array)n_hex_array, m_hex_array = size (hex_array); n_string = length (string); empty_string = ones (1, n_string)* ; for i = 1 : n_hex_array if i = 1 line = string; else line = empty_string; end for j = 1 : m_hex_array line = line, lower(dec2hex(hex_array(i,j),2), ; end disp (line) end disp ( )function b_inv = find_inverse (b_in, mod_pol)for i = 1 : 255 prod = poly_mult (b_in, i, mod_pol); if prod = 1 b_inv = i; break end endfunction inv_poly_mat =in_poly_mat_gen (vargin)if nargin 0 verbose_mode = 1;else verbose_mode = 0; endinv_row_hex = 0e 0b 0d 09;inv_row = hex2dec (inv_row_hex);inv_rows = repmat (inv_row, 4, 1);inv_poly_mat = cycle (inv_rows, right); function state_out = inv_shift_rows (state_in)state_out = cycle (state_in, right);function w = key_expansion (key, s_box, rcon, vargin)if nargin 3 verbose_mode = 1; else verbose_mode = 0; endif iscell (key) | prod (size (key) = 16 error (Key has to be a vector (not a cell array) with 16 elements.)end if any (key 255) error (Elements of key vector have to be bytes (0 = key(i) 0 verbose_mode = 1;else verbose_mode = 0; endrow_hex = 02 03 01 01; row = hex2dec (row_hex);rows = repmat (row, 4, 1);poly_mat = cycle (rows, right);inv_row_hex = 0e 0b 0d 09;inv_row = hex2dec (inv_row_hex);inv_rows = repmat (inv_row, 4, 1);inv_poly_mat = cycle (inv_rows, right); function ab = poly_mult (a, b, mod_pol)for i_bit = 1 : 8 if bitget (a, i_bit) b_shift = bitshift (b, i_bit - 1); ab = bitxor (ab, b_shift); end end for i_bit = 16 : -1 : 9 if bitget (ab, i_bit) mod_pol_shift = bitshift (mod_pol, i_bit - 9); ab = bitxor (ab, mod_pol_shift); end endfunction rcon = rcon_gen (vargin)if nargin 0 verbose_mode = 1; else verbose_mode = 0; endmod_pol = bin2dec (100011011);rcon(1) = 1;for i = 2 : 10 rcon(i) = poly_mult (rcon(i-1), 2, mod_pol); endrcon = rcon(:), zeros(10, 3); function s_box, inv_s_box = s_box_gen (vargin)if nargin 0 verbose_mode = 1; else verbose_mode = 0; endmod_pol = bin2dec (100011011);inverse(1) = 0;for i = 1 : 255 inverse(i + 1) = find_inverse (i, mod_pol); endfor i = 1 : 256 s_box(i) = aff_trans (inverse(i); endinv_s_box = s_box_inversion (s_box);if verbose_mode s_box_mat = reshape (s_box, 16, 16); inv_s_box_mat = reshape (inv_s_box, 16, 16); endfunction inv_s_box = s_box_inversion (s_box)for i = 1 : 256 inv_s_box(s_box(i) + 1) = i - 1; end function state_out = shift_rows (state_in)state_out = cycle (state_in, left);function bytes_out = sub_bytes (bytes_in, s_box)bytes_out = s_box (bytes_in + 1);(2)加密:function ciphertext = cipher (plaintext) %plaintext=244 78 39 158 174 188 178 182 128 168 126 13 56 156 3 5;miyao=54 70 10 23; 99 85 93 24; 32 57 35 97; 51 68 92 47;w=key_expansion (miyao, s_box_gen, rcon_gen);s_box=s_box_gen;poly_mat=poly_mat_gen;if nargin 4 verbose_mode = 1; else verbose_mode = 0; endif iscell (plaintext) | prod (size (plaintext) = 16 error (Plaintext has to be a vector (not a cell array) with 16 elements.) endif any (plaintext 255) error (Elements of plaintext vector have to be bytes (0 = plaintext(i) = 255).) endif iscell (w) | any (size (w) = 44, 4) error (w has to be an array (not a cell array) with 44 x 4 elements.) endif any (w 255) error (Elements of key array w have to be bytes (0 = w(i,j) 0 verbose_mode = 1;else verbose_mode = 0; endif iscell (ciphertext) | prod (size (ciphertext) = 16 error (Ciphertext has to be a vector (not a cell array) with 16 elements.) endif any (ciphertext 255) error (Elements of ciphertext vector have to be bytes (0 = ciphertext(i) = 255).) endif iscell (w) | any (size (w) = 44, 4) error (w has to be an array (not a cell array) with 44 x 4 elements.) endif any (w 255) error (Elements of key array w have to be bytes (0 = w(i,j) = 255).) endstate = reshape (ciphertext, 4, 4);fprintf(-n);if verbose_mode disp_hex (Initial state : , state)endround_key = (w(41:44, :);if verbose_mode disp_hex (Initial round key : , round_key)endstate = add_round_key (state, round_key);for i_round = 9 : -1 : 1 if verbose_mode disp_hex (State at start of round , num2str(i_round), : , state) end state = inv_shift_rows (state); if verbose_mode disp_hex (After inv_shift_rows : , state) end state = sub_bytes (state, inv_s_box); if verbose_mode disp_hex (After inv_sub_bytes : , state) end round_key = (w(1:4) + 4*i_round, :); if verbose_mode disp_hex (Round key : , round_key) end state = add_round_key (state, round_key); if verbose_mode disp_hex (After add_round_key : , state) end state = mix_columns (state, inv_poly_mat); endif verbose_mode disp_hex (State at start of final round : , state)endstate = inv_shift_rows (state);if verbose_mode disp_hex (After inv_shift_rows : , state)endstate = sub_bytes (state, inv_s_box);if verbose_mode disp_hex (After inv_sub_bytes : , state)endround_key = (w(1:4, :);if verbose_mode disp_hex (Round key : , round_key)end state = add_round_key (state, round_key);if verbose_mode disp_hex (Final state : , state)endfprintf(-n); fprintf(n); plaintext = state; (4) 主程序:clearfprintf(12n);fprintf(n);fprintf(54 70 10 23n);fprintf(99 85 93 24n);fprintf(32 57 35 97n);fprintf(51 68 92 47n);cp=input(1/2);if cp=1 % plaintext=input(); miwen= cipher (plaintext) endif cp=2 % ciphertext=input(); minwen= inv_cipher (ciphertext)end 结果分析密钥如下:加密请输入1;解密输入2密钥如下:54 70 10 2399 85 93 2432 57 35 9751 68 92 47您的选择(1/2):2请输入密文(注意是矩阵形式): 144 39 36 246 194 157 119 13 216 205 171 231 208 80 49 6-循环十次的每次输出结果-Initial state : 90 27 24 f6 c2 9d 77 0d d8 cd ab e7 d0 50 31 06 Initial round key : e9 b3 9c dd 0a 72 75 52 ec be 41 76 5c 28 53 c5 State at start of round 9 : 79 94 b8 2b c8 ef 02 5f 34 73 ea 91 8c 78 62 c3 After inv_shift_rows : 79 94 b8 2b 5f c8 ef 02 ea 91 34 73 78 62 c3 8c After inv_sub_bytes : af e7 9a 0b 84 b1 61 6a bb ac 28 8f c1 ab 33 f0 Round key : 13 5a 2f 41 90 78 07 27 7c 52 ff 37 df 74 7b 96 After add_round_key : bc bd b5 4a 14 c9 66 4d c7 fe d7 b8 1e df 48 66 State at start of round 8 : d4 89 81 ea 55 34 bd 5b 4d 60 7b b1 bd 88 0b d9 After inv_shift_rows : d4 89 81 ea 5b 55 34 bd 7b b1 4d 60 88 0b d9 bd After inv_sub_bytes : 19 f2 91 bb 57 ed 28 cd 03 56 65 90 97 9e e5 cd Round key : bf 49 75 6e 78 e8 7f 20 29 2e ad c8 40 ab 0f ed After add_round_key : a6 bb e4 d5 2f 05 57 ed 2a 78 c8 58 d7 35 ea 20 State at start of round 7 : 50 d7 5c d1 11 15 c5 f3 34 e9 49 dc 01 d8 41 be After inv_shift_rows : 50 d7 5c d1 f3 11 15 c5 49 dc 34 e9 d8 41 be 01 After inv_sub_bytes : 6c 0d a7 51 7e e3 2f 07 a4 93 28 eb 2d f8 5a 09 Round key : f0 f6 3c 1b 35 90 97 5f b1 07 83 65 ef eb a4 e2 After add_round_key : 9c fb 9b 4a 4b 73 b8 58 15 94 ab 8e c2 13 fe eb State at start of round 6 : 3b 20 f6 b1 e5 4a 76 f7 b2 51 2a 9b 6c 34 dc aa After inv_shift_rows : 3b 20 f6 b1 f7 e5 4a 76 2a 9b b2 51 34 dc aa 6c After inv_sub_bytes : 49 54 d6 56 26 2a 5c 0f 95 e8 3e 70 28 93 62 b8 Round key : 58 06 ca 27 bb a5 07 c8 eb b6 84 e6 23 04 4f 46 After add_round_key : 11 52 1c 71 9d 8f 5b c7 7e 5e ba 96 0b 97 2d fe State at start of round 5 : c5 77 36 c3 5e e2 4b f5 43 53 2b 83 21 d2 86 6b After inv_shift_rows : c5 77 36 c3 f5 5e e2 4b 2b 83 43 53 d2 86 6b 21 After inv_sub_bytes : 07 02 24 33 77 9d 3b cc 0b 41 64 50 7f dc 05 7b Round key : f2 5e cc ed 11 1e a2 cf ea 5d 32 62 76 27 4b 09 After add_round_key : f5 5c e8 de 66 83 99 03 e1 1c 56 32 09 fb 4e 72 State at start of round 4 : 16 01 c8 fb 5a be 61 a7 f4 31 a4 36 c3 b6 64 f7 After inv_shift_rows : 16 01 c8 fb a7 5a be 61 a4 36 f4 31 b6 64 f7 c3 After inv_sub_bytes : ff 09 b1 63 89 46 5a d8 1d 24 ba 2e 79 8c 26 33 Round key : de ac 92 21 42 0f bc 6d c6 b7 6f 50 8b 51 6c 42 After add_round_key : 21 a5 23 42 cb 49 e6 b5 db 93 d5 7e f2 dd 4a 71 State at start of round 3 : aa 7b fb 3e 8e 45 ba 7c cd 12 b9 e9 2a 8e a2 53 After inv_shift_rows : aa 7b fb 3e 7c 8e 45 ba b9 e9 cd 12 8e a2 53 2a After inv_sub_bytes : 62 03 63 d1 01 e6 68 c0 db eb 80 39 e6 1a 50 95 Round key : e8 72 3e b3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论