Is there an equivalent mcrypt_create_iv function for Java?
I\'m creating a forum, I need users to be able to signup not only on the website but also within a client
There is no single call to create a random IV, and if you leave the IV out then the Oracle implementation will default to a zero IV (an IV consisting of N bytes set to 00
value, where N is the block size). Also note that in Java the getBlockSize()
method returns the amount of bytes, not bits.
Cipher enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// create IV array of the correct size
final byte[] ivData = new byte[enCipher.getBlockSize()];
// create (or retrieve) a cryptographic secure random implementation (auto-seeded)
final SecureRandom rng = new SecureRandom();
// fill the IV array with random data
rng.nextBytes(ivData);
// generate the ParameterSpec (to create a general parameter for Cipher.init())
IvParameterSpec iv = new IvParameterSpec(ivData);
// and initialize with the new IV
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);