How to get a key stream block for a specific counter in AES/CTR encryption

1.7k views Asked by At

I'm using Crypto++ library

I made the IV and key and passed them to the function

    CTR_Mode< AES >::Encryption e;
    e.SetKeyWithIV(key, 16, iv);

Then when I encrypt I use the transformation filter on the string itself to get the encrypted string.

enter image description here

I want to get the final cipher key which XORed with the string to encrypt it ? Is there a way to get it from encryption object ?

1

There are 1 answers

4
Maarten Bodewes On BEST ANSWER

If you just need the key stream that was XORed with the first 16 bytes of plaintext (as the picture suggests) then you need to encrypt a block of 16 bytes set to 00 using the same AES CTR mode. A block of key stream, when XOR'ed with all zero's simply returns the same stream. So you can do this for as many blocks of plaintext that you require. As you already guessed you need to use the same key and IV.

If you want to only decrypt something much further in the stream then you need to calculate a new 16 byte IV. You can do this because the IV is directly used as initial counter (possibly right-padded with zeros). So you just need to calculate how many blocks where pre-processed and then add this value (as big endian value) to the IV. This of course only works for block boundaries, you may need to discard some bytes from the key stream if you want to jump to a very specific offset.