EDIT: If the message length is equal to the pad length, you have to pad to the next multiple. E.g. 16 bytes would be padded to 32 because one would not know how long the message was because the last byte defines the number of added (=padded) values.

Here's another example to pad/depad data for many use cases (like Encryption).

Example how it works:

1. You have a string or bytes with a lenght of 5. Let's say it's the string "Hello"
2. The function you use (e.g. AES) needs the data with a length of a multiple of x (here: 16)
3. You then need to fill the rest of the 16 bytes ("Hello" has 5, so it's 11) with a filler (Pad-Value)
4. 16 bytes is easy bit usually you have much more data to pad
5. The example calculates the next multiple of 16 (you can use any pad-size) and fills it with pad-values
6. The last byte (here the 16th) is filled with the total length of padded bytes (otherwise you wouldn't know how many were added)
7. The pad value (what it uses to fill up) depends on the usage and is regulated (RFC's, etc.)
8. It can be

- any byte value
- random bytes
- the count of the added bytes (here it would be 11 bytes or 0xb for all bytes to fill)

I use it to exchange AES encrypted data with my ESP32 (as far as I know it only supports "no padding" so I had to pad the data on my own).

1=Random Bytes

B4X:
``````Sub Pad (mess() As Byte, PadTo As Int,PadMode As Int,PadValue As Object) As Byte()
Dim MessLength As Int = mess.Length
Case 1
Case 2
Case 3
End Select
Next

End Sub``````

B4X:
``````Sub Unpad (PaddedMess() As Byte) As Byte()
Return OrigMess
End Sub``````

Call:
B4X:
``````Dim padded() As Byte=Pad("Hello".GetBytes("UTF8"),16,2,15)

Last edited:

Replies
2
Views
260
Replies
4
Views
774
Replies
9
Views
6K
Replies
15
Views
789
Replies
0
Views
1K