Question about AES encrypton library.

I’m using the AES library posted here and a curiosity to me is that the plaintext is:

 byte plain[64] =
  {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  };

But after a run of encryption and decryption, this is what I see as decrypted:

decrypt 0
00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
Done one block
97 
D7 E9 62 F2 08 AD E0 52 C3 98 13 39 E4 E7 A1 31 
F6 70 B0 7B 19 44 87 27 D6 4F CF 64 DB 69 7D FE 
A9 0C 99 38 07 EF 99 59 47 BF BB C7 7A C2 12 9B 
2A 90 B6 A3 42 75 01 9D 2E 8D 5C A8 F9 CB 69 
Done one block
00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
Done one block
9B 
2A 90 B6 A3 42 75 01 9D 2E 8D 5C A8 F9 CB 69 
Done one block

I’ve edited the example to print a little differently and some of the comments are mine but this is the output.

The relevant section of the code is this:

  	  if (blocks == 1)
    	    succ = aes.decrypt (cipher, plain) ;
    	  else
    	  {
    	    for (byte i = 0 ; i < 16 ; i++)
    	      iv[i] = my_iv[i] ;
    	    succ = aes.cbc_decrypt (cipher, check, blocks, iv) ;
    	  }
    	  //t1 = micros () - t0 ;
    	  Serial.println ("decrypt ") ; Serial.print ((int) succ) ;
    	  //Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;
        
    	  for (byte ph = 0 ; ph < (blocks == 1 ? 3 : 4) ; ph++)
    	  {
    	    for (byte i = 0 ; i < (ph < 3 ? blocks*N_BLOCK : N_BLOCK) ; i++)
    	    {
    	      byte val = ph == 0 ? plain[i] : ph == 1 ? cipher[i] : ph == 2 ? check[i] : iv[i] ;
    	      Serial.print (val>>4, HEX) ; Serial.print (val&15, HEX) ; Serial.print (" ") ;
            // One byte finished decrypting at this point.
            if( i % 16 == 0)
            {
              Serial.println();
            }
    	    }
          Serial.println () ;
          // One block finished decrypting.
          Serial.print("Done one block");
    	    Serial.println () ;
    	  }
        Serial.print("Done decrypt!");

I don’t understand why it decrypts one ‘block’ as the correct plaintext, some random text in between and again the plaintext and some more random stuff after.

Also, I thought a ‘block’ in AES is the size of the table initially used, 16 bytes. So how is it that a ‘block’ here seems to be the entire 64 bytes of plaintext? And where is the other information even coming from?

jori_castell: I don't understand why it decrypts one 'block' as the correct plaintext, some random text in between and again the plaintext and some more random stuff after.

The plaintext can be a multiple of the block size in size. The 'aes' example uses a key array of which 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) are used. The examples use the following key lengths and block lengths:

prekey (128, 4) ;
  prekey (192, 3) ;
  prekey (256, 2) ;
  prekey (128, 1) ;
  prekey (192, 1) ;
  prekey (256, 1) ;

The display for the multi-block examples (the first three) show the plaintext, the ciphertext, the decrypted plaintext, and the 16-byte Initialization Vector (IV) that would be used for the next block. The one-block examples (the last three) only show the plaintext, ciphertext, and the decrypted plaintext since the IV is the same as the cyphertext in those cases.

D'oh, thanks, it's obvious once you pointed it out. That for loop threw me, since I thought it was just looping through one array.

hi I am use arduino DUE how I can do this with AES library ? enter voice by microphone in (A0) the voice out encryption by " AES" from (DAC0) the encryption voice enter to (A1) then the voice out decryption from ( DAC1)