@@ -18,6 +18,33 @@ void print_stream(int row, int column, uint8_t (*input)[column]){
1818 printf ("\n" );
1919}
2020
21+ void print_text (int row , int column , uint8_t (* input )[column ]){
22+ for (int i = 0 ; i < row ; i ++ ){
23+ for (int j = 0 ; j < column ; j ++ ){
24+ if (input [j ][i ] == '\0' ){
25+ printf ("\n" );
26+ return ;
27+ }
28+ printf ("%c" , input [j ][i ]);
29+ }
30+ }
31+ printf ("\n" );
32+ }
33+
34+ void set_text (uint8_t Text [standard_row ][standard_column ], char * plaintext ){
35+ for (int i = 0 ; i < 4 ; i ++ ){
36+ for (int j = 0 ; j < 4 ; j ++ )
37+ Text [j ][i ] = (int )plaintext [4 * i + j ];
38+ }
39+ }
40+
41+ void set_key (int col , uint8_t cipher_key [standard_row ][col ], uint8_t * key ){
42+ for (int i = 0 ; i < col ; i ++ ){
43+ for (int j = 0 ; j < 4 ; j ++ )
44+ cipher_key [j ][i ] = key [4 * i + j ];
45+ }
46+ }
47+
2148void SubBytes_test (uint8_t (* input )[standard_column ]){
2249 printf ("--------- Origin block State ---------\n" );
2350 print_block (standard_row , standard_column , input );
@@ -214,59 +241,71 @@ void seperate_round_key(int column, uint8_t (*output)[standard_row][standard_col
214241
215242/* ------------------------------- AES 128,192,256bit ------------------------------- */
216243
217- void AES_128bit (uint8_t Text [ standard_row ][ standard_column ] , uint8_t cipher_key [standard_row ][ 4 ]){
244+ void AES_128bit (char * plaintext , uint8_t key [standard_row * 4 ]){
218245 printf ("\n----------------- AES 128bit ------------------------\n" );
246+ uint8_t Text [standard_row ][standard_column ];
247+ uint8_t cipher_key [standard_row ][4 ];
248+ set_text (Text , plaintext );
249+ set_key (4 , cipher_key , key );
219250 uint8_t G_round_key [11 ][4 ][4 ] = {0 };
220251 uint8_t round_key [11 ][4 ][4 ] = {0 };
221252 Key_Scheduling (10 , 4 , cipher_key , G_round_key );
222253 seperate_round_key (4 , round_key , G_round_key , 128 );
223254
224- printf ("plain text\n " );
255+ printf ("plain text : " );
225256 print_stream (standard_row , standard_column , Text );
226257
227258 encrypt (Text , round_key , 10 );
228- printf ("\nencrypted text\n " );
229- print_stream (standard_row , standard_column , Text );
259+ printf ("\nencrypted text : " );
260+ print_text (standard_row , standard_column , Text );
230261
231262 decrypt (Text , round_key , 10 );
232- printf ("\ndecrypted text\n " );
233- print_stream (standard_row , standard_column , Text );
263+ printf ("\ndecrypted text : " );
264+ print_text (standard_row , standard_column , Text );
234265}
235266
236- void AES_192bit (uint8_t Text [ standard_row ][ standard_column ] , uint8_t cipher_key [standard_row ][ 6 ]){
267+ void AES_192bit (char * plaintext , uint8_t key [standard_row * 6 ]){
237268 printf ("\n----------------- AES 192bit ------------------------\n" );
269+ uint8_t Text [standard_row ][standard_column ];
270+ uint8_t cipher_key [standard_row ][6 ];
271+ set_text (Text , plaintext );
272+ set_key (6 , cipher_key , key );
238273 uint8_t G_round_key [9 ][4 ][6 ] = {0 };
239274 uint8_t round_key [13 ][4 ][4 ] = {0 };
240275 Key_Scheduling (8 , 6 , cipher_key , G_round_key );
241276 seperate_round_key (6 , round_key , G_round_key , 192 );
242277
243- printf ("plain text\n " );
244- print_stream (standard_row , standard_column , Text );
278+ printf ("plain text : " );
279+ print_text (standard_row , standard_column , Text );
245280
246281 encrypt (Text , round_key , 12 );
247- printf ("\nencrypted text\n " );
282+ printf ("\nencrypted text : " );
248283 print_stream (standard_row , standard_column , Text );
249284
250285 decrypt (Text , round_key , 12 );
251- printf ("\ndecrypted text\n " );
252- print_stream (standard_row , standard_column , Text );
286+ printf ("\ndecrypted text : " );
287+ print_text (standard_row , standard_column , Text );
253288}
254289
255- void AES_256bit (uint8_t Text [ standard_row ][ standard_column ] , uint8_t cipher_key [standard_row ][ 8 ]){
290+ void AES_256bit (char * plaintext , uint8_t key [standard_row * 8 ]){
256291 printf ("\n----------------- AES 256bit ------------------------\n" );
292+ uint8_t Text [standard_row ][standard_column ];
293+ uint8_t cipher_key [standard_row ][8 ];
294+ set_text (Text , plaintext );
295+ set_key (8 , cipher_key , key );
257296 uint8_t G_round_key [8 ][4 ][8 ] = {0 };
258297 uint8_t round_key [16 ][4 ][4 ] = {0 };
259298 Key_Scheduling (7 , 8 , cipher_key , G_round_key );
260299 seperate_round_key (8 , round_key , G_round_key , 256 );
261300
262- printf ("plain text\n " );
263- print_stream (standard_row , standard_column , Text );
301+ printf ("plain text : " );
302+ print_text (standard_row , standard_column , Text );
264303
265304 encrypt (Text , round_key , 14 );
266- printf ("\nencrypted text\n " );
305+ printf ("\nencrypted text : " );
267306 print_stream (standard_row , standard_column , Text );
268307
269308 decrypt (Text , round_key , 14 );
270- printf ("\ndecrypted text\n " );
271- print_stream (standard_row , standard_column , Text );
309+ printf ("\ndecrypted text : " );
310+ print_text (standard_row , standard_column , Text );
272311}
0 commit comments