Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. function [ciphertext, round]= aes_demo(plaintext_hex,key_hex,R)
  2.  
  3. plaintext=hex2dec(plaintext_hex).'
  4. key=hex2dec(key_hex).'
  5. s_box= s_box_gen (1);
  6. rcon = rcon_gen (1);
  7. poly_mat= poly_mat_gen (1);
  8. k = key_expansion (key, s_box, rcon, 1);
  9. [ciphertext, round,states_output]= (cipher(plaintext, k, s_box, poly_mat,R));
  10. endfunction
  11.  
  12. function [ciphertext,round] = cipher(plaintext, w, s_box, poly_mat,nb_ronde_max)
  13.  
  14. if iscell (plaintext) | prod (size (plaintext)) ~= 16
  15. error ('Plaintext has to be a vector (not a cell array) with 16 elements.')
  16. end
  17. if any (plaintext < 0 | plaintext > 255)
  18.  
  19. error ('Elements of plaintext vector have to be bytes (0 <= plaintext(i) <= 255).')
  20.  
  21. end
  22.  
  23. if iscell (w) | any (size (w) ~= [44, 4])
  24.  
  25. error ('w has to be an array (not a cell array) with [44 x 4] elements.')
  26.  
  27. end
  28.  
  29. if any (w < 0 | w > 255)
  30.  
  31.  
  32. error ('Elements of key array w have to be bytes (0 <= w(i,j) <= 255).')
  33.  
  34. end
  35.  
  36. verbose_mode = 0;
  37.  
  38. if verbose_mode
  39. disp(' ')
  40. disp('********************************************')
  41. disp('* *')
  42. disp('* C I P H E R *')
  43. disp('* *')
  44. disp('********************************************')
  45. disp(' ')
  46. end
  47.  
  48. disp ('******************initial plaintext and key:***************************')
  49.  
  50. state = reshape (plaintext, 4, 4)
  51.  
  52. round_key = (w(1:4, :))'
  53.  
  54. state = add_round_key (state, round_key)
  55. disp ('*********************initial state round 0*****************************')
  56.  
  57. for i_round = 1 : nb_ronde_max
  58.  
  59. disp ('***************************************************')
  60. disp('The round number:')
  61. disp( i_round)
  62. disp ('***************************************************')
  63.  
  64. round(i_round).start=state;
  65.  
  66. state = sub_bytes (state, s_box)
  67.  
  68. round(i_round).s_box=state;
  69.  
  70. state = shift_rows (state)
  71.  
  72. round(i_round).s_row=state;
  73.  
  74. state = mix_columns (state, poly_mat)
  75.  
  76.  
  77. round(i_round).m_col=state;
  78.  
  79. round_key = (w((1:4) + 4*i_round, :))'
  80.  
  81. round(i_round).k_sch=round_key ;
  82.  
  83. disp ('********************output state of this *********')
  84.  
  85. state = add_round_key (state, round_key)
  86.  
  87. states_output(i_round).state
  88.  
  89. disp ('***********************end of this round!!****************************')
  90. end
  91.  
  92. disp ('************************Final round****************************')
  93.  
  94. round(nb_ronde_max+1).start=state;
  95.  
  96. state = sub_bytes (state, s_box)
  97.  
  98. round(nb_ronde_max+1).s_box=state;
  99.  
  100. state = shift_rows (state)
  101.  
  102. round(nb_ronde_max+1).s_row=state;
  103.  
  104.  
  105. round_key = (w(41:44, :))'
  106.  
  107. round(nb_ronde_max+1).k_sch=round_key ;
  108.  
  109. disp ('************************Final State****************************')
  110.  
  111. state = add_round_key (state, round_key)
  112.  
  113. disp ('************************Ciphertext****************************')
  114.  
  115. ciphertext = reshape (state, 1, 16);
  116.  
  117. end
  118.  
  119. [ciphertext, round]= aes_demo({'32' '43' 'f6' 'a8' '88' '5a' '30' '8d' '31' '31' '98' 'a2' 'e0' '37' '07' '34'},
  120. {'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