suculent / thinx-aes-lib

AES wrapper for ESP8266/ESP32/Arduino/nRF5x
Other
113 stars 39 forks source link

Uexpected encoding result (ESP32) #45

Closed tilchl closed 3 years ago

tilchl commented 3 years ago

When i encrypt with the lib i get different results then with cyberchef, cryptii, cryptojs. It's still decodable but only in my c code. I suspect I set the key or iv wrong.

With my code i get:

Cleartext: username:password
key: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 
enc IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a
Ciphertext: X1rr7fuUJvEmK10s4EaICb94yXZw+XMIw+g5hWhtO6o=
dec IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 
Cleartext: username:password

with cyperchef (already configured)

Cleartext: username:password
key: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 
enc IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a
Ciphertext: PmCPqlih/pdH2q0OBxh6UQ2MdiAp3OZ2+S+X1XPHTVk=

I mostly copied your example I set aesLib.set_paddingmode((paddingMode)0); Main Code:

  char cleartext[256]="username:password";
  char ciphertext[512];
    Serial.print("key: ");
    for (int i = 0; i < 16; i++) {
      Serial.printf("%x ", aes_key[i]);
    }
    Serial.println();
    byte enc_iv[N_BLOCK]   = { 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A };
    byte dec_iv[N_BLOCK]   = { 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A };
    Serial.print("enc IV: ");
    for (int i = 0; i < 16; i++) {
      Serial.printf("%x ", enc_iv[i]);
    }
    Serial.print("\n");
    // Encrypt
    uint16_t clen = String(cleartext).length();
    String encrypted = encrypt(cleartext, clen, enc_iv);
    sprintf(ciphertext, "%s", encrypted.c_str());
    Serial.print("Ciphertext: ");
    Serial.println(encrypted);
    Serial.print("dec IV: ");
    for (int i = 0; i < 16; i++) {
      Serial.printf("%x ", dec_iv[i]);
    }
    // Decrypt
    uint16_t dlen = encrypted.length();
    String decrypted = decrypt( ciphertext, dlen, dec_iv);
    Serial.print("\nCleartext: ");
    Serial.println(decrypted);
    Serial.println();

functions:

String encrypt(const char * msg, uint16_t msgLen, byte iv[]) {
  int cipherlength = aesLib.get_cipher64_length(msgLen);
  char encrypted[cipherlength];
  aesLib.encrypt64(msg, msgLen, encrypted, aes_key, sizeof(aes_key), iv);
  return String(encrypted);
}

String decrypt(char * msg, uint16_t msgLen, byte iv[]) {
  char decrypted[msgLen];
  aesLib.decrypt64(msg, msgLen, decrypted, aes_key, sizeof(aes_key), iv);
  return String(decrypted);
}
suculent commented 3 years ago

Don’t use encrypt64/decrypt64. Just encrypt/decrypt.

Happy coding.

    1. 2021 v 8:17, tilchl notifications@github.com:

 When i encrypt with the lib i get different results then with cyberchef, cryptii, cryptojs. It's still decodable but only in my c code. I suspect I set the key or iv wrong.

With my code i get:

key: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a enc IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a Ciphertext: X1rr7fuUJvEmK10s4EaICb94yXZw+XMIw+g5hWhtO6o= dec IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a Cleartext: username:password with cyperchef (already configured)

key: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a enc IV: 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a 6a Ciphertext: PmCPqlih/pdH2q0OBxh6UQ2MdiAp3OZ2+S+X1XPHTVk= I mostly copied your example I set aesLib.set_paddingmode((paddingMode)0); Main Code:

char cleartext[256]="username:password"; char ciphertext[512]; Serial.print("key: "); for (int i = 0; i < 16; i++) { Serial.printf("%x ", aes_key[i]); } Serial.println(); byte enc_iv[N_BLOCK] = { 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A }; byte dec_iv[N_BLOCK] = { 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A, 0x6A }; Serial.print("enc IV: "); for (int i = 0; i < 16; i++) { Serial.printf("%x ", enc_iv[i]); } Serial.print("\n"); // Encrypt uint16_t clen = String(cleartext).length(); String encrypted = encrypt(cleartext, clen, enc_iv); sprintf(ciphertext, "%s", encrypted.c_str()); Serial.print("Ciphertext: "); Serial.println(encrypted); Serial.print("dec IV: "); for (int i = 0; i < 16; i++) { Serial.printf("%x ", dec_iv[i]); } // Decrypt uint16_t dlen = encrypted.length(); String decrypted = decrypt( ciphertext, dlen, dec_iv); Serial.print("\nCleartext: "); Serial.println(decrypted); Serial.println(); functions:

String encrypt(const char * msg, uint16_t msgLen, byte iv[]) { int cipherlength = aesLib.get_cipher64_length(msgLen); char encrypted[cipherlength]; aesLib.encrypt64(msg, msgLen, encrypted, aes_key, sizeof(aes_key), iv); return String(encrypted); }

String decrypt(char * msg, uint16_t msgLen, byte iv[]) { char decrypted[msgLen]; aesLib.decrypt64(msg, msgLen, decrypted, aes_key, sizeof(aes_key), iv); return String(decrypted); } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

roysG commented 3 years ago

How to get base64 string from the encrypt function?

suculent commented 3 years ago

Those are two steps.

Normally, decrypt operates on byte-array (not a string as it can contain null-bytes in the middle).

The byte-array can be converted to base64 using int base64_encode(char output, const char input, int inputLen) from included xbase64.cpp.

M.

On 16. 5. 2021, at 13:52:47, roysG @.***> wrote:

How to convert the encrypt to get output of base64?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/suculent/thinx-aes-lib/issues/45#issuecomment-841806775, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABWFRYDG25RZKTSBXHV2CLTN6WY7ANCNFSM4XKOH3NA.

adriancs2 commented 11 months ago

I have just published a working example of using this library with ESP32.

You can have a look a my demo project: https://github.com/adriancs2/arduino.aes.asp.net

here's the full explanation: https://adriancs.com/c-sharp/1081/aes-encrypted-http-request-between-arduino-esp32-and-c-asp-net/

Here's the simplified version: https://adriancs.com/arduino/1096/arduino-aes-encryption-128-bits-cbc/