I'm trying to implement an AES encryption function on my ESP8266 controller, using Arduino ide and this aes library. I finally made it work after many attempts but the output i get from the encryption function is only numerical and it doesn't correspond to other encryptions made by other softwares.

This is the code part relative to the AES encryption:

 #include <AES.h>

AES aes ;
byte *key = (unsigned char*)"0123456789010123";
unsigned long long int my_iv = 36753562;
 tempToken = RANDOM_REG32 % 10000;
    randomstr = String(tempToken);
    //answer = "{\"token\":\"" + randomstr + "\"}";
    byte plain[4];
    randomstr.getBytes(plain, 4);
    Serial.println(randomstr);
    Serial.println(plain[0]);
    Serial.println(plain[1]);
    Serial.println(plain[2]);
    Serial.println(plain[3]);
    byte cipher [48];
  //  aes.iv_inc();
    byte iv [N_BLOCK] ;
    aes.set_IV(my_iv);
    aes.do_aes_encrypt(plain, 48, cipher, key, 128, iv);
    String ciphertoken = "";
    for (int i = 0; i < 48; i++) {
      ciphertoken += cipher[i];
    }
    answer = "{\"token\":\"" + ciphertoken + "\"}";

It basically generates a random number between 0 - 9999, then converts it in a byte array (because aes function needs a byte array as input), then encrypt this byte array and prints the output, written in cipher byte array.

This is the output I get from it

6188
54
49
56
0
{"token":"521111513725521540120674957151112223103194250224197251261938514437131212302201813929189123134524113912554682501508415421130220"}

I can't understand if I did something wrong of if it is formatted in a strange way and if I can get the right encryption from it

share|improve this question

closed as off-topic by jfpoilpret, Dat Ha, Mattia, KIIV, uint128_t Dec 29 '16 at 17:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – jfpoilpret, Dat Ha, Mattia, KIIV, uint128_t
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Could you replace your code with a minimal, complete and verifiable example (stackoverflow.com/help/mcve) please (remove the random part - use a well-defined input for this purpose), as well as the output you expect? – Mark Smith Dec 13 '16 at 19:12
up vote 0 down vote accepted

I don't know whether this is the only problem, but the way you are building up the string is bad: you are appending the decimal value of each byte of the cipher - so if your cipher array were, say, { 0x52, 0x11, 0x115, 0x137, 0x255, 0x215, 0x40, 0x1, 0x20 .... } you'd get the string you're getting: 521111513725521540120... That's probably not what you want.

Probably what you want for that part is to base64-encode cipher, which you can find out how to do, for example, here. (If you see encrypted stuff in printable format, usually it's base64 encoded.)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.