Closed anaisortn closed 6 years ago
please use the provided functions for printing the outputs. check the Documentation at : http://spaniakos.github.io/AES/
please report back if now you are getting the correct results. If you cant use the provided function because of board incompatibility i will be more than happy to walk you threw.
I have the same issue. It seem like a byte-overflow issue on esp8266. The code AES.cpp:
void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits, byte ivl [N_BLOCK]){
calc_size_n_pad(size_p);
byte plain_p[get_size()];
padPlaintext(plain,plain_p);
int blocks = get_size() / N_BLOCK;
set_key (key, bits) ;
cbc_encrypt (plain_p, cipher, blocks, ivl);
}
...
void AES::padPlaintext(void* in,byte* out)
{
memcpy(out,in,size);
for (int i = size-pad; i < size; i++){;
out[i] = arr_pad[pad - 1];
}
}
The parameter "plain_p" pass to "out" in void padPlaintext, then watch & print the parameter "out" value. In theory it will be
[original_plain_text]+[padding]+[padding]+...
But in fact it become
[original_plain_text]+[padding]+[padding]+...+���렝N���0��%�
So finally got another value after decrypt.
I need to modify few code in AES.cpp to fix the issue on esp8266:
void AES::do_aes_encrypt...
...
byte plain_p[get_size()+1];
plain_p[get_size()] = 0x00;
...
to real cut the plain_p (out) value.
I don't know if other mcu has the same issue or not. So I can not confirm my modify code is correct or not (only works on esp8266 confirmed)
the point here is that you are using
printf("check %s \n", (char *)check);
printf function when printing %s (string) are requiring a terminating character
The library cannot predict if the user will use strings or note. it has to stay to a low level byte encoding. thus, i can't code string function in the library.
That's why i have print function in order to print the code based on the size.
after all, in encryption everything except the key is known. thats why when you transmit a cipher, the IV, the cipher and the size are know to everyone.
Taking this as a fact, there is no need to print using string function.
If you really want to use string functions, declare a string with size = cipher + 1, and use low lev g functions, declare a string with size = cipher + 1, and use low level copy in order to move the buffer into the string followed by the terminating character \0 (0x00). (required by printf %s in order to stop the printing).
closing due to inactivity
Hi, using your example on ESP8266, the decrypted output is
�4���gT� ��6΄��Add NodeAdd Node��23z���r������͋D��l��պj�N���0��%��렝N���0��%�
instead ofAdd NodeAdd NodeAdd NodeAdd NodeAdd Node
What am I missing?