Re:Google's Android Update Alliance Is Already Dead
by X-Istence on Friday December 16, 2011 @03:48PM
(#3362259)
I've written code for this before, I have it over at Github:
https://gist.github.com/fd98541dd158d7e7be9e
So if anyone wants a full example they can run and play with themselves. Your example stuff looks like it was pulled from the code I wrote above...
This also means that you can't do a partial offset into the CTR. You can increment the counter correctly, but you can't start encrypting from within a partial block.
Lets say you need to encrypt 17 bytes and write them to a file, which later can be opened in append mode to append more data to it using AES-128/CTR-BE mode:
C-style terminated string.That is 2 blocks in CTR mode (1 full block used, 1 partial block (1 byte out of a 16 byte block)).
The next time you open the file you want to append data to it, what you would do is this:
Now you can read your file back using the following: So that would look something like this in Java code with JCE: I've left out some stuff like creating the iv (really the counter), so now if we wanted to advance into the first block what we could do (and this works with JCE) is this: Where count contains the amount we want to offset into the next block.On JCE this does exactly what I described above, it moves forward "count" characters into the next block and then you can do:
where "output" is DataOutputStream(new FileOutputStream("filename", true)) which is a file opened in append mode. Now you can write stuff to the file using the normal functions used by an OutputStream. This will then correctly append your new data to the end of the file so that if you start reading at the beginning of the file you can read through the end and get valid data. (We are using this to encrypt log files that are opened in Append only mode).Where BouncyCastle breaks down is that you can not use c.update() to move forward into the block, thus appending is not possible because if you don't end on a block boundary your next write is going to have overlap. The only thing you can do in c.update() is provide it the amount of bytes that is the same as the block size. So you have to pad all input into c.update() to 16 bytes. Basically instead of being a stream cipher that allows seeking it has become yet another block cipher.