I am using AES/CTR/NoPadding to encrypt while downloading files in one of my application. The encryption is done along with downloading. When the internet is not available in the middle of downloading, I need to pause the download and resume it when the network is available. Since the iv gets initialised to 0 state on resuming encryption, my resulting file gets corrupted. Is there any way to move the counter/iv to the previous state so I can resume encryption properly?

  • 8
    $\begingroup$ In cryptographic theory? Yes. You can just remember at which counter value you were and how much of the cipher output you used and continue right there. In practice with the API you have? That belongs on Stack Overflow (with a minimal complete verifieable example) $\endgroup$ – SEJPM Dec 5 '19 at 12:45
  • $\begingroup$ @SEJPM I think I've provided examples of this before (oh, here you go). However, note that you cannot just change the counter, you also likely need to jump into the key stream at a certain point. That's all doable, but if the API is sufficiently high level, it may require encryption of some bytes to skip ahead in the key stream block - unless it provides the functionality itself of course. $\endgroup$ – Maarten Bodewes Dec 5 '19 at 22:31
  • $\begingroup$ Counter mode can be constructed from ECB mode on the counter values and an XOR with the plaintext block. If your API does not do what you want, then construct your own counter mode encryption function which does allow you to set the initial counter value as needed. $\endgroup$ – rossum Dec 6 '19 at 12:37
  • $\begingroup$ Dunno, then you'd have to implement CTR mode and still have to skip bytes. Seems to me that using a (possibly optimized) pre-existing CTR mode will always be easier unless you cannot specify the full 16 byte counter value. $\endgroup$ – Maarten Bodewes Dec 7 '19 at 13:44
  • $\begingroup$ @Maarten-reinstateMonica The link you provided solved my problem. Cheers :) $\endgroup$ – Ruthwik Warrier Dec 13 '19 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.