2
votes

I'm creating a java app to encrypt files using Shamir's Secret Sharing Scheme, and since I can't encrypt the whole file in one go, I would like to encrypt it piecewise - for example, one int (four bytes) at a time. Basically, read the file as a stream of bytes, and encrypt n bytes at a time.

The trouble is, the encrypted piece can be larger or smaller than the input. So I can end up with a four byte value encrypted as five bytes etc. This mean I can't simply concatenate all the encrypted pieces to create the encrypted file - I need some way to delimit the pieces. What would be a good way to do that?

3
A secret sharing scheme is not an encryption scheme by itself. You need something else to make an encryption algorithm out of it. If that something produces bytes that can be of any value, then you can't use a delimiter. - Artjom B.

3 Answers

3
votes

Without knowing anything about the ciphertext domain and assuming every byte value can be in the ciphertext, I would suggest the prepend strategy.

Prepend length

You should prepend a byte before each encrypted int to show how long the next partial ciphertext will be. You will get a ciphertext blowup of one sixth to one fifth for partial ciphertexts of length 4 and 5. This also works for partial ciphertexts up to 255 byte length.

If partial ciphertexts longer than 255 bytes are needed then a variable length encoding is needed. Apache Lucene defines for example the VInt.

The good thing about this kind of encoding is that one can easily skip over long partial ciphertexts without reading them if the underlying stream supports skipping. That would not be possible with using a delimiter.

Delimiter

You can still use a delimiter to do this, but it will have a slightly higher blowup of the ciphertext on average if the ciphertext byte value distribution is uniform.

Take for example the delimiter byte 0x00. It delimits each partial ciphertext, but the ciphertext itself may also contain 0x00 bytes. Now you need to escape the 0x00 ciphertext bytes for example as 0xFF00 and every 0xFF as 0xFFFF. If you now encounter a single 0x00 byte, then you can be sure that it is a delimiter and if you encounter 0xFF00 or 0xFFFF, you transform it into 0x00 or 0xFF of the ciphertext.

Conclusion

As you can see, the probability of escaping a byte is 2 in 256. Which is roughly the same blowup as with the variable length ints from before, but without the convenience of skipping. The implementation difficulty shifts from building the variable length ints to properly unescape ciphertext bytes.

1
votes

Possible solution:
After encryption, the data size is known. create a structure of bytes in the following format:

{length-using-integer}{encrypted-data}{length-using-integer}{encrypted-data}......

So when you start reading the data to decrypt, first read the length and then read the number of bytes mentioned by length, and so on..

1
votes

The standard solution is to prefix the encrypted block by its size. If you want to reduce your final file size, you can use one byte to store the size of the next encrypted block, not only for each int. For example : <53><53 bytes encrypted><12><12 bytes encrypted>..

If your blocks can be longer than 255 bytes, then you can use an variable strcuture to store the size (unsigned) : on one byte from 0 to 127, on two bytes from 128 to 32767 and so on.