<p>Is it recommended that I use an <a href="http://en.wikipedia.org/wiki/Initialization_vector" rel="noreferrer">initialization vector</a> to encrypt/decrypt my data? Will it make things more secure? Is it one of those things that need to be evaluated on a case by case basis?</p>

<p>To put this into actual context, the Win32 Cryptography function, <a href="http://msdn.microsoft.com/en-us/library/aa380272(VS.85).aspx" rel="noreferrer">CryptSetKeyParam</a> allows for the setting of an initialization vector on a key prior to encrypting/decrypting. Other API's also allow for this. </p>

<p>What is generally recommended and why?</p>

[{'answer_id': 65894, 'author': 'Andrew', 'author_id': 8871, 'author_profile': 'https://Stackoverflow.com/users/8871', 'pm_score': 6, 'selected': True, 'text': '<p>An IV is essential when the same key might ever be used to encrypt more than one message.</p>\n\n<p>The reason is because, under most encryption modes, two messages encrypted with the same key can be analyzed together.  In a simple stream cipher, for instance, XORing two ciphertexts encrypted with the same key results in the XOR of the two messages, from which the plaintext can be easily extracted using traditional cryptanalysis techniques.</p>\n\n<p>A weak IV is part of what made WEP breakable.</p>\n\n<p>An IV basically mixes some unique, non-secret data into the key to prevent the same key ever being used twice.</p>\n'}, {'answer_id': 65930, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>In most cases you should use IV. Since IV is generated randomly each time, if you encrypt same data twice, encrypted messages are going to be different and it will be impossible for the observer to say if this two messages are the same.</p>\n'}, {'answer_id': 65987, 'author': 'Peter Stone', 'author_id': 1806, 'author_profile': 'https://Stackoverflow.com/users/1806', 'pm_score': 2, 'selected': False, 'text': '<p>I found the writeup of HTTP Digest Auth (<a href="http://www.faqs.org/rfcs/rfc2617.html" rel="nofollow noreferrer">RFC 2617</a>) very helpful in understanding the use and need for IVs / nonces.</p>\n'}, {'answer_id': 66026, 'author': 'John', 'author_id': 8298, 'author_profile': 'https://Stackoverflow.com/users/8298', 'pm_score': 0, 'selected': False, 'text': '<p>The IV allows for plaintext to be encrypted such that the encrypted text is harder to decrypt for an attacker. Each bit of IV you use will double the possibilities of encrypted text from a given plain text.</p>\n\n<p>For example, let\'s encrypt \'hello world\' using an IV one character long. The IV is randomly selected to be \'x\'. The text that is then encrypted is then \'xhello world\', which yeilds, say, \'asdfghjkl\'. If we encrypt it again, first generate a new IV--say we get \'b\' this time--and encrypt like normal (thus encrypting \'bhello world\'). This time we get \'qwertyuio\'.</p>\n\n<p>The point is that the attacker doesn\'t know what the IV is and therefore must compute every possible IV for a given plain text to find the matching cipher text. In this way, the IV acts like a <a href="http://en.wikipedia.org/wiki/Password_salt" rel="nofollow noreferrer">password salt</a>. Most commonly, an IV is used with a chaining cipher (either a stream or block cipher). In a chaining block cipher, the result of each block of plain text is fed to the cipher algorithm to find the cipher text for the next block. In this way, each block is chained together.</p>\n\n<p>So, if you have a random IV used to encrypt the plain text, how do you decrypt it? Simple. Pass the IV (in plain text) along with your encrypted text. Using our fist example above, the final cipher text would be \'xasdfghjkl\' (IV + cipher text).</p>\n\n<p>Yes you should use an IV, but be sure to choose it properly. Use a good random number source to make it. Don\'t ever use the same IV twice. And <strong>never</strong> use a constant IV.</p>\n\n<p>The Wikipedia article on <a href="http://en.wikipedia.org/wiki/Initialization_vector" rel="nofollow noreferrer">initialization vectors</a> provides a general overview.</p>\n'}, {'answer_id': 66259, 'author': 'Purfideas', 'author_id': 4615, 'author_profile': 'https://Stackoverflow.com/users/4615', 'pm_score': 3, 'selected': False, 'text': '<p>Take a good look at a picture (see below) of CBC mode.  You\'ll quickly realize that an attacker knowing the IV is like the attacker knowing a previous block of <em>ciphertext</em> (and yes they already know plenty of that).</p>\n\n<p>Here\'s what I say: most of the "problems" with IV=0 are general problems with block encryption modes when you <em>don\'t</em> ensure data integrity.  You really must ensure integrity.</p>\n\n<p>Here\'s what I do: use a strong checksum (cryptographic hash or HMAC) and prepend it to your plaintext before encrypting.  There\'s your known first block of ciphertext: it\'s the IV of the same thing without the checksum, and you need the checksum for a million other reasons.</p>\n\n<p>Finally: any analogy between CBC and stream ciphers is not terribly insightful IMHO.</p>\n\n<p>Just look at the picture of CBC mode, I think you\'ll be pleasantly surprised.</p>\n\n<p>Here\'s a picture:</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation</a></p>\n\n<p><a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation" rel="nofollow noreferrer">link text</a></p>\n'}, {'answer_id': 17841285, 'author': 'Molibar', 'author_id': 1176452, 'author_profile': 'https://Stackoverflow.com/users/1176452', 'pm_score': 2, 'selected': False, 'text': "<p>If the same key is used multiple times for multiple different secrets patterns could emerge in the encrypted results. The IV, that should be pseudo random and used only once with each key, is there to obfuscate the result. You should never use the same IV with the same key twice, that would defeat the purpose of it.</p>\n\n<p>To not have to bother keeping track of the IV the simplest thing is to prepend, or append it, to the resulting encrypted secret. That way you don't have to think much about it. You will then always know that the first or last N bits is the IV.</p>\n\n<p>When decrypting the secret you just split out the IV, and then use it together with the key to decrypt the secret.</p>\n"}, {'answer_id': 19005566, 'author': 'Rushyo', 'author_id': 164188, 'author_profile': 'https://Stackoverflow.com/users/164188', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>Is it one of those things that need to be evaluated on a case by case\n  basis?</p>\n</blockquote>\n\n<p>Yes, it is. Always read up on the cipher you are using and how it expects its inputs to look. Some ciphers don't use IVs but do require salts to be secure. IVs can be of different lengths. The mode of the cipher can change what the IV is used for (if it is used at all) and, as a result, what properties it needs to be secure (random, unique, incremental?).</p>\n\n<p>It is generally recommended because most people are used to using AES-256 or similar block ciphers in a mode called 'Cipher Block Chaining'. That's a good, sensible default go-to for a lot of engineering uses and it needs you to have an appropriate (non-repeating) IV. In that instance, it's not optional.</p>\n"}]
['https://Stackoverflow.com/questions/65879', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4916/']