-1

I want to generate random IV in iOS which is base on the below code:

mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);

Is anybody can tell me how to generate random IV in iOS same with the above php? Thank you.

JacksonNg
  • 53
  • 5
  • [ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29) doesn't use an IV, so you can just use an empty string. – Artjom B. Mar 21 '16 at 08:54
  • 1
    **Never use [ECB mode](http://crypto.stackexchange.com/q/14487/13022)**. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like [CBC](http://crypto.stackexchange.com/q/22260/13022) or [CTR](http://crypto.stackexchange.com/a/2378/13022). It is better to authenticate your ciphertexts so that attacks like a [padding oracle attack](http://crypto.stackexchange.com/q/18185/13022) are not possible. This can be done with authenticated modes like GCM or EAX, or with an [encrypt-then-MAC](http://crypto.stackexchange.com/q/202/13022) scheme. – Artjom B. Mar 21 '16 at 08:55

1 Answers1

0

This question is unanswerable (it's akin to "what is the sound of one hand clapping?"), but in the interest of guiding people who come upon it in the future:

What to do instead: See this answer.

Community
  • 1
  • 1
Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206