josephpal / esp32-Encrypt

ESP32 AES 128bit Encryption with SPIFFS support
48 stars 13 forks source link

Encryption padding #3

Open bjun626 opened 3 years ago

bjun626 commented 3 years ago

It seems like the char after NULL(\0) are filled with random values. This means there will be error when decrypting using the online tools e.g. Calculator According to Using Padding in Encryption

We need to pad the block with padding bytes to make it up to the required length. There are at least five common conventions:-

Pad with bytes all of the same value as the number of padding bytes Pad with 0x80 followed by zero bytes Pad with zeroes except make the last byte equal to the number of padding bytes Pad with zero (null) characters Pad with space characters

Could consider adding this code at line 155

if( plainText.length()%BUFF_SIZE > 0 ) {    
  for(int bytes_read=(index*BUFF_SIZE); bytes_read <= (index*BUFF_SIZE) + plainText.length()%BUFF_SIZE; bytes_read++) {
    buffer += plainText[bytes_read];
  };
  ////ADDITIONAL CODE////
  int no_to_pad = 16 - buffer.length(); 
  while (buffer.length() < 16) {
    char padding = no_to_pad;
    buffer += padding;
  }
  ////////////////////////
  cipherTextString += encryptBuffer(const_cast<char*>(buffer.c_str()), key);
}
josephpal commented 2 years ago

It seems like the char after NULL(\0) are filled with random values. This means there will be error when decrypting using the online tools e.g. Calculator According to Using Padding in Encryption

We need to pad the block with padding bytes to make it up to the required length. There are at least five common conventions:-

Pad with bytes all of the same value as the number of padding bytes Pad with 0x80 followed by zero bytes Pad with zeroes except make the last byte equal to the number of padding bytes Pad with zero (null) characters Pad with space characters

Could consider adding this code at line 155

if( plainText.length()%BUFF_SIZE > 0 ) {    
  for(int bytes_read=(index*BUFF_SIZE); bytes_read <= (index*BUFF_SIZE) + plainText.length()%BUFF_SIZE; bytes_read++) {
    buffer += plainText[bytes_read];
  };
  ////ADDITIONAL CODE////
  int no_to_pad = 16 - buffer.length(); 
  while (buffer.length() < 16) {
    char padding = no_to_pad;
    buffer += padding;
  }
  ////////////////////////
  cipherTextString += encryptBuffer(const_cast<char*>(buffer.c_str()), key);
}

i am working on it, thx