A cryptographically secure random number generator produces computationally unpredictable values for cryptographic keys and initialization vectors. In Java, use java.security.SecureRandom; java.util.Random is not safe for cryptographic bytes. section 5.3
Listing 5.1 Generating secure random numbers
import java.security.SecureRandom;
public class CryptoUtils {
private static final SecureRandom secureRandom
= new SecureRandom(); #1
private static byte[] randomBytes(int length) { #2
byte[] bytes = new byte[amount];
secureRandom.nextBytes(bytes);
return bytes;
}
The Java pattern creates one SecureRandom instance, fills a byte array with nextBytes, and returns fresh random material. section 5.3
Fresh IVs for AES-GCM
Generate a new 12-byte IV with SecureRandom before each AES-GCM encryption. Never reuse an IV with the same key: reuse can cause catastrophic security failure. Libraries such as Tink reduce mistakes by applying these practices through a safer API. section 5.5.2 section 5.6


