Retaining the padded bytes of Structural Padding in Python
Last Updated :
10 Jul, 2020
Structural Padding means aligning the data into blocks of fixed size in order to ease the operations of particular Computer Architectures, Cryptographic algorithms accordingly. We pad(add enough bytes) the operational data (which is not necessarily structured) in order to serve the machine. Padded bytes may interrupt the original data, to identify the number of padded bytes, the following approach helps.
This is traditional Structural Padding with ‘White Spaces’ as padding entities, which will not help us extract the precise bytes of original data
Example 1:
def pad(text, block_size):
pad_size = block_size - len (text) % block_size
fit_text = text + ( " " * pad_size)
return (fit_text)
def main():
block_size = 20
text = "HALL OF BYTES "
print (pad(text, block_size))
main()
|
Output :
'HALL OF BYTES '
Example 2:
Here is how to retain the padded number of bytes
def pad(text, block_size):
pad_size = block_size - len (text) % block_size
fit_text = text + chr (pad_size) * pad_size
return (fit_text, )
def main():
text = "HALL OF BYTES"
block_size = 20
print (pad(text, block_size))
main()
|
Output :
'HALL OF BYTES\x07\x07\x07\x07\x07\x07\x07'
# thus 7 bytes were padded.
def pad(text, block_size):
pad_size = block_size - len (text) % block_size
fit_text = text + chr (pad_size) * pad_size
return (fit_text, )
def main():
text = "PRECISELY RETAINED"
block_size = 20
print (pad(text, block_size))
main()
|
Output :
'PRECISELY RETAINED\x02\x02'
# Thus two bytes were padded.
Share your thoughts in the comments
Please Login to comment...