Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ciphertext, round]= aes_demo(plaintext_hex,key_hex,R)
- plaintext=hex2dec(plaintext_hex).'
- key=hex2dec(key_hex).'
- s_box= s_box_gen (1);
- rcon = rcon_gen (1);
- poly_mat= poly_mat_gen (1);
- k = key_expansion (key, s_box, rcon, 1);
- [ciphertext, round,states_output]= (cipher(plaintext, k, s_box, poly_mat,R));
- endfunction
- function [ciphertext,round] = cipher(plaintext, w, s_box, poly_mat,nb_ronde_max)
- if iscell (plaintext) | prod (size (plaintext)) ~= 16
- error ('Plaintext has to be a vector (not a cell array) with 16 elements.')
- end
- if any (plaintext < 0 | plaintext > 255)
- error ('Elements of plaintext vector have to be bytes (0 <= plaintext(i) <= 255).')
- end
- if iscell (w) | any (size (w) ~= [44, 4])
- error ('w has to be an array (not a cell array) with [44 x 4] elements.')
- end
- if any (w < 0 | w > 255)
- error ('Elements of key array w have to be bytes (0 <= w(i,j) <= 255).')
- end
- verbose_mode = 0;
- if verbose_mode
- disp(' ')
- disp('********************************************')
- disp('* *')
- disp('* C I P H E R *')
- disp('* *')
- disp('********************************************')
- disp(' ')
- end
- disp ('******************initial plaintext and key:***************************')
- state = reshape (plaintext, 4, 4)
- round_key = (w(1:4, :))'
- state = add_round_key (state, round_key)
- disp ('*********************initial state round 0*****************************')
- for i_round = 1 : nb_ronde_max
- disp ('***************************************************')
- disp('The round number:')
- disp( i_round)
- disp ('***************************************************')
- round(i_round).start=state;
- state = sub_bytes (state, s_box)
- round(i_round).s_box=state;
- state = shift_rows (state)
- round(i_round).s_row=state;
- state = mix_columns (state, poly_mat)
- round(i_round).m_col=state;
- round_key = (w((1:4) + 4*i_round, :))'
- round(i_round).k_sch=round_key ;
- disp ('********************output state of this *********')
- state = add_round_key (state, round_key)
- states_output(i_round).state
- disp ('***********************end of this round!!****************************')
- end
- disp ('************************Final round****************************')
- round(nb_ronde_max+1).start=state;
- state = sub_bytes (state, s_box)
- round(nb_ronde_max+1).s_box=state;
- state = shift_rows (state)
- round(nb_ronde_max+1).s_row=state;
- round_key = (w(41:44, :))'
- round(nb_ronde_max+1).k_sch=round_key ;
- disp ('************************Final State****************************')
- state = add_round_key (state, round_key)
- disp ('************************Ciphertext****************************')
- ciphertext = reshape (state, 1, 16);
- end
- [ciphertext, round]= aes_demo({'32' '43' 'f6' 'a8' '88' '5a' '30' '8d' '31' '31' '98' 'a2' 'e0' '37' '07' '34'},
- {'2b' '7e' '15' '16' '28' 'ae' 'd2' 'a6' 'ab' 'f7' '15' '88' '09' 'cf' '4f' '3c'},9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement