AES encryption/decryption library problem

Hello,

I am testing this AES encryption/decryption library:

AES library

and more specific I am trying this code:

Serial.begin(57600);
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
char data[] = "0123456789012345";
aes256_enc_single(key, data);
Serial.print("encrypted:");
Serial.println(data);
aes256_dec_single(key, data);
Serial.print("decrypted:");
Serial.println(data);

On the output I am getting:

encrypted:⸮o⸮⸮5Ua⸮R⸮⸮⸮
decrypted:0123456789012345

The problem is that I get strange encrypted characters so If I want to use the encrypted message to another Arduino in order to decrypt it I doubt if it will be decrypted succefully because these characters are unreadable....

Any help? Do you suggest any other library for encryption/decryption?

The encrypted data array contains non-printable characters. If you want to examine the values in hexadecimal representation, try:

Serial.print("encrypted: ");
for (int i=0; i<sizeof(data); i++) {
   Serial.print(data[i], HEX);
   Serial.print(" "); //separator
   }
Serial.println();

I wrote:

void setup() 
{
    // put your setup code here, to run once:
    Serial.begin(57600);
    uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    char data[] = "0123456789012345";
    aes256_enc_single(key, data);
    //Serial.print("encrypted:");
    //Serial.println(data);

    Serial.print("encrypted: ");
    for (int i=0; i<sizeof(data); i++) 
    {
        Serial.print(data[i], HEX);
        Serial.print(" "); //separator
    }
    Serial.println();
    
    aes256_dec_single(key, data);
    Serial.print("decrypted:");
    Serial.println(data);
    

}

and the output is:

encrypted: FFFFFF98 6F FFFFFF9E FFFFFF83 D 7F 6 3 35 55 61 FFFFFFB4 52 FFFFFF98 FFFFFFB5 FFFFFF9B 0 
decrypted:0123456789012345

Ignore the "FF.." prefix, .print() is interpreting the char value as 32 bits.

If it bothers you, one way to fix that is:

        Serial.print(data[i]&0xFF, HEX);

Thanks, it worked!

Something I wanted to ask, that is more general. I compiled the above code for Arduino MEGA 2560. How I find if it can be compiled to Arduino nano? I thought for changing the end device on Arduino IDE and see if it can be compiled, because I have not yet the nano device...is that a solution?

I thought for changing the end device on Arduino IDE

That would work, but there is probably nothing in that code specific to any processor.

360modena_cs:
I compiled the above code for Arduino MEGA 2560. How I find if it can be compiled to Arduino nano? I thought for changing the end device on Arduino IDE and see if it can be compiled, because I have not yet the nano device...is that a solution?

To check the compile, click on the checkmark in the circle on the far left. You don't need to have the board connected.

Memory may become a major issue... the Nano has much less program memory (32 kB) than the Mega (256 kB).

johnwasser:
To check the compile, click on the checkmark in the circle on the far left. You don't need to have the board connected.

I cannot find it.... (a printscreen/image could help).

wvmarle:
Memory may become a major issue... the Nano has much less program memory (32 kB) than the Mega (256 kB).

I get this:

Sketch uses 5050 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 260 bytes (3%) of dynamic memory, leaving 7932 bytes for local variables. Maximum is 8192 bytes.

So if I read correctly it consumes only 5KB of memory. Nano has 32KB in total..so I suppose it does the job?

Should do - depending on the rest of your sketch. Variable space is also more limited on a Nano (2 kB only).

360modena_cs:
I cannot find it.... (a printscreen/image could help).

Pick the correct board and processor in the IDE's tools menu.
Next use IDE menu sketch -> verify compile (R).

360modena_cs:
Thanks, it worked!

Something I wanted to ask, that is more general. I compiled the above code for Arduino MEGA 2560. How I find if it can be compiled to Arduino nano? I thought for changing the end device on Arduino IDE and see if it can be compiled, because I have not yet the nano device...is that a solution?

Hi All,

This is very interesting, but how can I use the output from the serial monitor to decrypt the data?

Can the output from the serial monitor simply be placed into a string and decrypted?

Many Thanks

This library is really helpful, but this library can't encrypt data more than 16 bytes. I need to encrypt data more than 16 bytes but i don't know in what section to edit the library. I'm little bit new in C++ programming. Can anyone help me on this problem?
I'm sorry for my bad english.

Break any amount of data into 16 byte blocks and encrypt the blocks one at a time.

Rutherford16:
This library is really helpful, but this library can't encrypt data more than 16 bytes. I need to encrypt data more than 16 bytes but i don't know in what section to edit the library. I'm little bit new in C++ programming. Can anyone help me on this problem?
I'm sorry for my bad english.

Look in AESLib.h for the list of encrypt and decrypt functions.
The "_single" functions do a single block of 16 bytes.
The "_multiple" functions take a 'length' that must be a multiple of 16. These are good if all of your data is consecutive in memory.
The "_start", "_continue", and "_finish" functions let you do multiple 16-byte blocks one at a time. These are good if you are reading data into memory and need to encrypt/decrypt one block at a time to prevent memory from filling up.
When . encrypting multiple blocks it uses 'CBC' which I think means "Cypher Block Chaining". This mean each block depends on the block(s) before it and they can't be decrypted individually.