A bit of background (sorry if you already know this, just make sure we use the same terminology):
- AES is a block cipher, an encryption algorithm that works with 128-bit blocks.
- CBC is a block cipher mode, a way to use block cipher to encrypt large amounts of data.
- For block encryption modes, an initialization vector (IV) is needed, which is a block of initialization data, usually the same size as the size of the block underlying the encryption.
(The Wikipedia about block encryption modes - http://en.wikipedia.org/wiki/Block_cipher_mode - is really good, and makes it clear why you need IV.)
Different block modes present different requirements for the IV selection process, but they all have one thing in common:
You should never encrypt two different messages with the same IV and key. If you do this, an attacker can get your plaintext, and sometimes your key (or equivalently useful data).
CBC imposes an additional restriction that IV must be unpredictable for an attacker - therefore, artjom-b's SecureRandom to use SecureRandom to create it is a good one.
Also, as artjob-b points out, CBC gives you privacy. In practice, this means that your data is kept secret, but there is no guarantee that it will arrive in one piece. Ideally, you should use authenticated mode such as GCM, CCM, or EAX.
Using one of these modes is really, really a good idea . Encrypt-then-MAC is cumbersome even for experts; Avoid this if you can. (If you need to do this, remember that you must use different keys for encryption and MAC.)
source share