Since you're working with strings, you can use ljust() to do the padding.
readBytes = f.read(self.chunksize)
if readBytes:
readBytes = readBytes.ljust(self.chunksize, b'\0')
Answer from Jeff Mercado on Stack OverflowSince you're working with strings, you can use ljust() to do the padding.
readBytes = f.read(self.chunksize)
if readBytes:
readBytes = readBytes.ljust(self.chunksize, b'\0')
bytes += "\0"*len_diff
should help
encryption - How can I pad a sequence of bytes in Python 3? - Stack Overflow
aes - Weird padding in Crypto.Util.Padding.pad() function in python - Cryptography Stack Exchange
Python3 bytearray padding
aes - Is this how padding can work? - Cryptography Stack Exchange
This is a simplified (edited) version of a previous answer.
Assuming a file is read in a loop using line = f.read(N) (where N is the block size) until EOF.
1) Trivial zero padding; just add this after the read:
elen = len(line) % N
if elen:
line += bytes(N - elen)
Zero padding is the simplest, but has drawbacks.
2) PKCS#7 padding, N < 256; add this after the read and make sure the loop will be exited afterward:
if len(line) < N:
elen = 1 + (len(line) - 1) % N
line += bytes(elen for _ in range(elen))
Please note that reading from files differs from reading from network sockets. A buffer is needed to read blocks of fixed length from network.
Most encryption implementations support a padding option, usually PKCS#7 (née PKCS#5) that adds the padding on encryption and removed it on decryption.
Note: mcrypt does not support PKCS#7 padding, stay away from it.
You normally want to use a fully invertible padding scheme, i.e. a padding scheme with an associated unpadding scheme such that $unpad(pad(X)) = X$ for every $X$.
Assuming you only append data at the end, and don't do different things depending on the content of the message, this means that you always have to append something, even if the message is already of the right length.
In your scheme, in the case of a message which has already the length of a multiple of the block length, you'll have to append a full block ending with a 16 (assuming a 16-byte block cipher like AES). Your unpadding function then reads the last byte, and knows it has to strip off 16 bytes.
You shouldn't do any special-casing with a block of a certain structure only in the case of a full block appended.
But note that you should preferably use standardized schemes to help interoperability - for example PKCS#7-padding works just like your scheme, but with a bit different filling of the padded data. (This, just like your scheme, requires that the number of appended bytes is encodable in a byte, i.e. a block size of less than 256 bytes = 2048 bits. I'm not aware of any common block cipher with a larger block size, but you would need a different padding scheme in this case.)
It looks fine though you are better off sticking to established standards which already have parsers written. That way you don't have any problems.
To remove the padding, read the last byte, then move that many values back and truncate. In python it would look something like this:
p = decrypt(key, ciphertext)
pad_len = p[len(p)-1]
return p[0:len(p)-pad_len]
How about using the ljust method to first pad the string to 32 bytes with the desired 0xff character? ljust(width[, fillchar]) where width is the total string length after padding, and fillchar is the character to pad with (a space by default.)
struct.pack_into('32s', self.metadata, 4, text[0].ljust(32, b'\xff'))
First initialize the bytearray with 0xFF
metadata = bytearray(b'\xFF'*256)
So I have a .bin file I am attempting to pad. I have two ranges I need to fill with 0xFF. I currently have it working with seek() and its writing bytes correctly to that one specific memory location but i need to do it for certain ranges in the file. Any pointers to the right direction would be much appreciated! I've provided what I have code wise so far.
Edit: After digging some more im wondering if i could put the seek in a for loop with the offset parameter as a counter variable, write the bytes to that offset and then increment the counter.
import os, re
path = "C:\\Users\\comp\\Desktop\\python padding"
os.chdir( path )
files = os.listdir( path )
for filename in files:
if re.match('.*\.bin', filename):
fh = open(filename, 'r+b')
fh.seek(0x37fff)
fh.write(bytes([0xff]))
fh.close()
name, ext = os.path.splitext(filename)
os.rename(filename, name + "padded.bin")