user77416: I am trying to figure it out. Why are my encryption and decryption not correct?
The used library for AES:
https://github.com/DanielVukelich/Arduino-AES
This is for testing encrypt and decrypt.
//https://www.hanewin.net/encrypt/aes/aes-test.htm
#include "Rij_Consts.h"
#include "Rijndael.h"
   
    
    void setup() {
     
      Serial.begin(115200);
     
      //mbedtls_aes_context aes;
    
      unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
      
      //char * key = "abcdefghijklmnop";
      //const unsigned char key[16];
      const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
      //unsigned char key[16] = {0xFA,0xF9,0xF8,0xF7,0xF5,0xF4,0xF3,0xF2,0xF0,0xEF,0xEE,0xED,0xEB,0xEA,0xE9,0xE8};//กลับ
     
      //char *input = "Tech tutorials x";
     const unsigned char input[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
      //unsigned char input[16] ={0x9A,0xE9,0x43,0x36,0x10,0x80,0x51,0x1D,0x33,0x9D,0xA6,0x78,0x22,0xAF,0x4B,0x01};//กลับ
    
      
      unsigned char output[16]; //6743C3D1519AB4F2CD9A78AB09A511BD ต้องได้ตามนี้
    
    
      printf("Testing ECB mode:\n");
    
      printf("Original Plaintext:\n");
      for(int i = 0; i < 16; i++){
        printf("%2.2X ", input[i]);
      }
     
      unsigned char keys[176];//for 128
      
      Schedule_Keys(AES_128, key, 16, keys);
      printf("\n\nKEYs:\n");
      for(int i = 0; i < 16; i++){
        printf("%2.2X ", keys[i]);
      }
    
      Encrypt(AES_128, input, 16, keys, output);
    
    
    
      printf("\n\nCiphertext:\n");
     for(int i = 0; i < 16; i++){
        char str[16];
        sprintf(str, "%2.2X ", output[i]);
        Serial.print(str);
     }
    
    
    Decrypt(AES_128, input, 16, keys, output);
    printf("\n\nCiphertext:\n");
    for(int i = 0; i < 16; i++){
        char str[16];
        sprintf(str, "%2.2X ", output[i]);
        Serial.print(str);
    }
    }
     
    void loop() {}

My output :

You can see my encryption and decryption not correct.
I tried to reconstruct the full code.
#include "Rij_Consts.h"
#include "Rijndael.h"

//https://www.hanewin.net/encrypt/aes/aes-test.htm   สำหรับเทสเทียบ

void setup() {
   Serial.begin(115200);
    unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
    const unsigned char plainText[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
    unsigned char cipherText[16]; //6743C3D1519AB4F2CD9A78AB09A511BD Answer
    printf("Testing ECB mode:\n");

    
      printf("Original Plaintext:\n");
            for(int i = 0; i < 16; i++){
                  Serial.printf("%2.2X ", plainText[i]);
      }
        unsigned char keys[176];//for 128
        
        Schedule_Keys(AES_128, key, 16, keys);
            printf("\n\nKEYs:\n");
                for(int i = 0; i < 16; i++){
                    Serial.printf("%2.2X ", keys[i]);
      }

      
      Encrypt(AES_128, plainText, 16, keys,cipherText );
          Serial.printf("\n\nCiphertext:\n");
              for (int i = 0; i < 16; i++) {
                    Serial.printf("%2.2X ", cipherText[i]);
      } 
      unsigned char decryptedText[16];
      
        Decrypt(AES_128,cipherText , 16, keys, decryptedText);

          Serial.printf("\n\ndecryptedText:\n");
              for (int i = 0; i < 16; i++) {
                    Serial.printf("%2.2X ", decryptedText[i]);
}
}
 
  
void loop() {}


user62523: There are some issues with your code.
As mentioned in my comment, Decrypt () expects the Cipher Text as input and generates the Decrypted text as an output. However, in your code you're using the Plain text (which you confusingly named "input") as an input. The correct input should be your Cipher Text (which is the output from the Encrypt () function and which you called "output"). Please read the documentation of the AES library.
I suggest the following changes to your code to get it working:
Replace the part of your code, following Decrypt (...) until the end of setup() with the following code:
unsigned char decryptedText[16];
Decrypt(AES_128, output, 16, keys, decryptedText);

Serial.printf("\n\ndecryptedText:\n");
for (int i = 0; i < 16; i++) {
    Serial.printf("%2.2X ", decryptedText[i]);
}

I would also suggest you name your variables to avoid confusion, for example you could replace input[] with plainText[] and output[] with cipherText[]. Some of your Hex output code is also unnecessarily complex and could do with some restructuring.
I've compiled and run the above changes with your code on an ESP32 and it produces the expected Cipher text and correctly decrypts it. It produces the following output:
00:21:01.583 -> Testing ECB mode:
00:21:01.583 -> Original Plaintext:
00:21:01.630 -> 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A 
00:21:01.630 -> 
00:21:01.630 -> KEYs:
00:21:01.630 -> E8 E9 EA EB ED EE EF F0 F2 F3 F4 F5 F7 F8 F9 FA 
00:21:01.630 -> 
00:21:01.630 -> Ciphertext:
00:21:01.630 -> 67 43 C3 D1 51 9A B4 F2 CD 9A 78 AB 09 A5 11 BD 
00:21:01.630 -> 
00:21:01.630 -> decryptedText:
00:21:01.630 -> 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A 

In your question you mention that the generated Cipher text is not as expected. I can't help you with that, as I can't reproduce it.

77416: <p>I am trying to figure it out. Why are my encryption and decryption not correct?</p>
<p>The used library for AES:</p>
<p><a href="https://github.com/DanielVukelich/Arduino-AES" rel="nofollow noreferrer">https://github.com/DanielVukelich/Arduino-AES</a></p>
<p>This is for testing encrypt and decrypt.
<a href="https://www.hanewin.net/encrypt/aes/aes-test.htm" rel="nofollow noreferrer">//https://www.hanewin.net/encrypt/aes/aes-test.htm</a></p>
<pre><code>#include &quot;Rij_Consts.h&quot;
#include &quot;Rijndael.h&quot;
   
    
    void setup() {
     
      Serial.begin(115200);
     
      //mbedtls_aes_context aes;
    
      unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
      
      //char * key = &quot;abcdefghijklmnop&quot;;
      //const unsigned char key[16];
      const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
      //unsigned char key[16] = {0xFA,0xF9,0xF8,0xF7,0xF5,0xF4,0xF3,0xF2,0xF0,0xEF,0xEE,0xED,0xEB,0xEA,0xE9,0xE8};//กลับ
     
      //char *input = &quot;Tech tutorials x&quot;;
     const unsigned char input[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
      //unsigned char input[16] ={0x9A,0xE9,0x43,0x36,0x10,0x80,0x51,0x1D,0x33,0x9D,0xA6,0x78,0x22,0xAF,0x4B,0x01};//กลับ
    
      
      unsigned char output[16]; //6743C3D1519AB4F2CD9A78AB09A511BD ต้องได้ตามนี้
    
    
      printf(&quot;Testing ECB mode:\n&quot;);
    
      printf(&quot;Original Plaintext:\n&quot;);
      for(int i = 0; i &lt; 16; i++){
        printf(&quot;%2.2X &quot;, input[i]);
      }
     
      unsigned char keys[176];//for 128
      
      Schedule_Keys(AES_128, key, 16, keys);
      printf(&quot;\n\nKEYs:\n&quot;);
      for(int i = 0; i &lt; 16; i++){
        printf(&quot;%2.2X &quot;, keys[i]);
      }
    
      Encrypt(AES_128, input, 16, keys, output);
    
    
    
      printf(&quot;\n\nCiphertext:\n&quot;);
     for(int i = 0; i &lt; 16; i++){
        char str[16];
        sprintf(str, &quot;%2.2X &quot;, output[i]);
        Serial.print(str);
     }
    
    
    Decrypt(AES_128, input, 16, keys, output);
    printf(&quot;\n\nCiphertext:\n&quot;);
    for(int i = 0; i &lt; 16; i++){
        char str[16];
        sprintf(str, &quot;%2.2X &quot;, output[i]);
        Serial.print(str);
    }
    }
     
    void loop() {}
</code></pre>
<p>My output :
<a href="https://i.stack.imgur.com/6MyVP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6MyVP.png" alt="enter image description here" /></a></p>
<p>You can see my encryption and decryption not correct.</p>
<p>I tried to reconstruct the full code.</p>
<pre><code>#include &quot;Rij_Consts.h&quot;
#include &quot;Rijndael.h&quot;

//https://www.hanewin.net/encrypt/aes/aes-test.htm   สำหรับเทสเทียบ

void setup() {
   Serial.begin(115200);
    unsigned char iv[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    const unsigned char key[16] = {0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
    const unsigned char plainText[16] ={0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A};
    unsigned char cipherText[16]; //6743C3D1519AB4F2CD9A78AB09A511BD Answer
    printf(&quot;Testing ECB mode:\n&quot;);

    
      printf(&quot;Original Plaintext:\n&quot;);
            for(int i = 0; i &lt; 16; i++){
                  Serial.printf(&quot;%2.2X &quot;, plainText[i]);
      }
        unsigned char keys[176];//for 128
        
        Schedule_Keys(AES_128, key, 16, keys);
            printf(&quot;\n\nKEYs:\n&quot;);
                for(int i = 0; i &lt; 16; i++){
                    Serial.printf(&quot;%2.2X &quot;, keys[i]);
      }

      
      Encrypt(AES_128, plainText, 16, keys,cipherText );
          Serial.printf(&quot;\n\nCiphertext:\n&quot;);
              for (int i = 0; i &lt; 16; i++) {
                    Serial.printf(&quot;%2.2X &quot;, cipherText[i]);
      } 
      unsigned char decryptedText[16];
      
        Decrypt(AES_128,cipherText , 16, keys, decryptedText);

          Serial.printf(&quot;\n\ndecryptedText:\n&quot;);
              for (int i = 0; i &lt; 16; i++) {
                    Serial.printf(&quot;%2.2X &quot;, decryptedText[i]);
}
}
 
  
void loop() {}
</code></pre>
 62523: <p>There are some issues with your code.</p>
<p>As mentioned in my comment, <code>Decrypt ()</code> expects the Cipher Text as input and generates the Decrypted text as an output. However, in your code you're using the Plain text (which you confusingly named &quot;input&quot;) as an input. The correct input should be your Cipher Text (which is the output from the <code>Encrypt ()</code> function and which you called &quot;output&quot;). Please read the documentation of the AES library.</p>
<p>I suggest the following changes to your code to get it working:</p>
<p>Replace the part of your code, following <code>Decrypt (...)</code> until the end of <code>setup()</code> with the following code:</p>
<pre><code>unsigned char decryptedText[16];
Decrypt(AES_128, output, 16, keys, decryptedText);

Serial.printf(&quot;\n\ndecryptedText:\n&quot;);
for (int i = 0; i &lt; 16; i++) {
    Serial.printf(&quot;%2.2X &quot;, decryptedText[i]);
}
</code></pre>
<p>I would also suggest you name your variables to avoid confusion, for example you could replace <code>input[]</code> with <code>plainText[]</code> and <code>output[]</code> with <code>cipherText[]</code>. Some of your Hex output code is also unnecessarily complex and could do with some restructuring.</p>
<p>I've compiled and run the above changes with your code on an ESP32 and it produces the expected Cipher text and correctly decrypts it. It produces the following output:</p>
<pre><code>00:21:01.583 -&gt; Testing ECB mode:
00:21:01.583 -&gt; Original Plaintext:
00:21:01.630 -&gt; 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A 
00:21:01.630 -&gt; 
00:21:01.630 -&gt; KEYs:
00:21:01.630 -&gt; E8 E9 EA EB ED EE EF F0 F2 F3 F4 F5 F7 F8 F9 FA 
00:21:01.630 -&gt; 
00:21:01.630 -&gt; Ciphertext:
00:21:01.630 -&gt; 67 43 C3 D1 51 9A B4 F2 CD 9A 78 AB 09 A5 11 BD 
00:21:01.630 -&gt; 
00:21:01.630 -&gt; decryptedText:
00:21:01.630 -&gt; 01 4B AF 22 78 A6 9D 33 1D 51 80 10 36 43 E9 9A 
</code></pre>
<p>In your question you mention that the generated Cipher text is not as expected. I can't help you with that, as I can't reproduce it.</p>
