views:

362

answers:

4

I'm sure there's not one answer to this question, but just trying to find out a general approach.

Using Java 1.4.2, I need to generate a key and IV for use in a symmetric algorithm. These values will be pre-shared with the recipient through a secure channel.

The key I can generate with KeyGenerator.keyGenerate(). But unless I'm missing it, there's no function for generating a random IV.

Should I do something completely arbitrary like pull 16 random bytes from memory? Or is there a preferred way of generating sufficiently random initialization vectors?

Any guidance appreciated.

+3  A: 

For some implementations, the SecureRandom class will help you out by producing true random numbers:

Many SecureRandom implementations are in the form of a pseudo-random number generator (PRNG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a true random seed. Other implementations may produce true random numbers, and yet others may use a combination of both techniques.

It has two methods, getProvider() and getAlgorithm() which should give you some information about which implementation is used. From this page it seems that the pseudo random generator SHA1PRNG (which is seeded with true random data) is one of them or even the only one currently available.

schnaader
@schnaader - nit-pick - SHA1PRNG is not a true RNG. It is a PRNG with (according to the page you linked to) a true random seed.
Stephen C
You're right, edited the answer.
schnaader
A: 
erickson
A: 

As the other answers implied, use a secure random number generator to create the IV.

Just as an aside though, you don't need to send the IV through a secure channel - it's usual to just prepend it to the message. Remember that it's far more important that you use a fresh IV for each message, than that you keep the IVs secret. Pre-sharing the IVs at the same time as the key implies either than you're re-using IVs (bad), or have a limit on the number of messages you can send.

caf
A: 

If you are using GUI or you have access to system calls to user data input hardware (mouse preferred) you can create a vector of pairs of mouse pointer coordinates as user moves it. Add them to some string. Than use your favorite hash function on the string to create completely random IV with high entropy.

beermann