AES Encryption with ECB Mode

Hello to All Arduino Members,

I am Doing a project by using AES Encryption…

It’s Everything Fine with 16bit data , the problem was occurs when i want to encrypt more than 16bit data,

my sample code and find bellow attached library file,

//#include<String.h>

#include <AES.h>

AES aes ;

byte key [] = "123458748785485" ;
byte plain [] = "Hello World Well Come to the ARDUINO World";
byte cipher [N_BLOCK] ;
byte check [N_BLOCK] ;

char edata[100],msgarray[500];
char * hex = "0123456789abcdef" ;

void clearbuf(char *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

void clearbuf1(byte *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

void setup ()
{
  Serial.begin (9600) ;
  Serial.println ("AES EBC Mode Checking") ;
  encryption(plain);
}

byte encryption(byte d[150])
{
   int i=0,j=0;
   byte a[16],b[16];
   aes.set_key (key, 128) ;
  
  for( i=0;i<16;i++)
  {
      b[j++]=d[i];
  }
  b[i]='\0';  j=0;
  
  aes.encrypt (d, cipher) ;
  
  print_value(cipher,128);  
  clearbuf1(b);
  clearbuf1(cipher);
  
  for( i=16;i<sizeof(d);i++)
  {
      b[j++]=d[i];
  }
  b[i]='\0';
  
   aes.set_key (key, 128) ;
   aes.encrypt (b, cipher) ;
  
  print_value(cipher,128);  
  clearbuf1(b);
  Serial.print("DATA is: ");
  Serial.println(msgarray); 
}
void loop () 
{
;
}

void print_value (byte * a, int bits)
{
  int j;
  bits >>= 3 ;
  for (int i = 0 ; i < bits ; i++)
    {
      byte b = a[i] ;
      edata[j++] = hex [b>>4];
      edata[j++] = hex [b&15];
     // Serial.print (hex [b >> 4]) ;
    //  Serial.print (hex [b & 15]) ;
    }
    edata[j]='\0';
    strcat(msgarray,edata);
    clearbuf(edata);
 // Serial.println () ;
}

In the above code I am trying to dividing 16bit data and individually encryption but finally i got the wrong hex decimal values .

I am trying one of encryption tool for to check my encryption data is correct or wrong by using this URL Online encrypt tool - Online tools

Kindly do the needful.

Thanks in advance.

AESlibrary.zip (43 KB)

void clearbuf(char *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

void clearbuf1(byte *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

Why do you have two functions that do EXACTLY the same thing?

byte encryption(byte d[150])
{

You lied when you said that this function returns a value, since it does not.

   int i=0,j=0;
   byte a[16],b[16];
   aes.set_key (key, 128) ;
 
  for( i=0;i<16;i++)
  {
      b[j++]=d[i];
  }

Since j is initialized to 0 every time the function is called, i and j will have the same value on every pass through the for loop. So, why do you need two variables that contain the same value?

  for( i=16;i<sizeof(d);i++)
  {
      b[j++]=d[i];
  }
  b[i]='\0';

Since d is an array of 150 elements of type byte, sizeof(d) is going to return 150. You will then copy bytes from d[16] through d[149] into b[0] through b[133]. The b array is not that big!

You need to figure out how many times you need to iterate (encrypting 16 bytes at a time) in the encrypt function. Then, you need to encrypt 16 bytes per iteration. 150 is not a multiple of 16, so it is a silly size for the argument to the function.

Hellow PaulS , Thanks for your reply,

PaulS:

void clearbuf(char *b)// clearing the buffer

{
   while(*b)
     *b++=’\0’;
}

void clearbuf1(byte *b)// clearing the buffer
{
   while(*b)
     *b++=’\0’;
}



Why do you have two functions that do EXACTLY the same thing?

Both are clearing arrays one is for byte array and another one is char array.

PaulS:

byte encryption(byte d[150])

{



You lied when you said that this function returns a value, since it does not.



int i=0,j=0;
  byte a[16],b[16];
  aes.set_key (key, 128) ;

for( i=0;i<16;i++)
 {
     b[j++]=d[i];
 }



Since j is initialized to 0 every time the function is called, i and j will have the same value on every pass through the for loop. So, why do you need two variables that contain the same value?

Its My mistake , please accept my apology for this.

Actually I am trying to this code first.

#include <AES.h>

AES aes ;

byte key [] = "123458748785485" ;
byte plain [] = "Hello World Well Come to the ARDUINO World";
byte cipher [N_BLOCK] ;
byte check [N_BLOCK] ;

char edata[100],msgarray[500];
char * hex = "0123456789abcdef" ;

void clearbuf(char *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

void clearbuf1(byte *b)// clearing the buffer
{
    while(*b)
      *b++='\0';
}

void setup ()
{
  Serial.begin (9600) ;
  Serial.println ("AES library test vectors") ;
  encryption(plain);
}

void encryption(byte *d)
{
   aes.set_key (key, 128) ;
   
   aes.encrypt (d, cipher) ;
  
   print_value(cipher,128);  
  
   Serial.print("DATA is: ");
   Serial.println(msgarray); 
}

void print_value (byte * a, int bits)
{
  int j;
  bits >>= 3 ;
  for (int i = 0 ; i < bits ; i++)
    {
      byte b = a[i] ;
      edata[j++] = hex [b>>4];
      edata[j++] = hex [b&15];
    }
    edata[j]='\0';
    strcat(msgarray,edata);
    clearbuf(edata);
}

void loop () 
{
;
}

I got the encrypted data is “40b2ddf04fb900d926bed06fbce63212”.

But I check the same Text and I got the encrypted data in one of online tool Online encrypt tool - Online tools

Then it shows the encrypted data is “40b2ddf04fb900d926bed06fbce63212230faf01976c7113b0a343ae779806dcd1c5fd758bd3307b70d5b70cddd7cfa7”

How can I got the same encrypted data.

(Sorry for my bad English)

Thanks in advance.

Both are clearing arrays one is for byte array and another one is char array.

Bytes and chars are the same size. You can use one function for clearing both types of arrays.

In that code, aes.encrypt() is being called once. Since you have not posted a link to the library, but you claim that it only works for 16 character arrays, I have to assume that the method is only expecting 16 element arrays (or pointers to 16 element arrays) as input. So, I'm not surprised that it produces different output from a program that is properly designed to encrypt longer strings.

Looks like my library - it has N_BLOCK defined.

Have you run and examined and run the 2 examples I provide? That shows how it works and verifies against test vectors. Block ciphers process data in blocks, hence the name, and AES does it in blocks of 128 bits (16 bytes)

I provide raw block ops and CBC mode only - its deliberately a minimal implementation with no frills at all. If you don't understand any of these crypto terms you need to read up about these things first - cryptography requires rigour and there are many pitfalls.