In other words, I would like to know what happens under the hood when the seek method of PDFAESStream is being called? When a new seek position less than the current encrypted file position is being passed, whether the encrypted file is closed first and reopened again? Or just 'seek backward' operation over the encrypted file is executed?
OK, it is coding by me, other supports may not explan well.
PDFAESStream is block based encrypt/decrypt stream.
each origin block is 4080 bytes.
each encrypted block is 4096(4080 + 16) bytes, 16 bytes for CBC mode padding.
for encrypt file:
--split a file to blocks, each has 4080 bytes, last block may less than 4080.
--encrypt each block, and save 4096 bytes to another file.
for decrypt file:
--split encrypted file to blocks, each has 4096 bytes, last block may less that 4096, but must more than 16 bytes.
--decrypt each block, and save 4080 bytes to another file.
static private int BLOCK_ENC_SIZE = 4096;
static private int BLOCK_DEC_SIZE = BLOCK_ENC_SIZE-16;
for seek method implements::
1. check previous block need flush? if yes, write encrypted block back to file.
2. calcute block index by position: int block = pos / BLOCK_DEC_SIZE;
3. if block index of new position, is same to previous block index, return NOP.
4. otherwise, decode the new block, and release previous block.
all reading/writing from current position, is reading/writing on current block.
if position is out of byte range of current block, then flush current block, and reading/writing next block.