How to pick an appropriate IV (Initialization Vector) for AES/CTR/NoPadding?

前端 未结 5 827
别跟我提以往
别跟我提以往 2020-12-07 21:10

I would like to encrypt the cookies that are written by a webapp and I would like to keep the size of the cookies to minimum, hence the reason I picked AES/CTR/NoPadding.

5条回答
  •  悲&欢浪女
    2020-12-07 21:15

    I dont have a direct answer for you question but a few things to add though.

    First of all, encrypting the cookie does not make sense to me. If you want confidentiality of your data, you shouldn't store it in a cookie anyway. If you want integrity (i.e. not possible to tamper with the content of the cookie), you should use a keyed hash (HMAC, for example).

    Another note is to never use a IV which is all 0 just for convenience.

    IV's are equal in size with of your block. In case of AES-128, the blocksize is 128, the keysize is 128 and hence the IV is 128 bits.

    The best way to do this is by creating a random AES key and using it as IV. This random IV may be public as long as it is not reused in subsequent encryptions with the same key

    edit:

    You may want to look at this wiki page for more info on which mode to use. However, never use ECB unless you're sure you should use it. And even then, verify with an expert. CBC is as far as I know the safest (together with PCBC).

    http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation

提交回复
热议问题