liveBook · Manning

Cryptographically secure random number generator

As explained in Software Security for Developers

A cryptographically secure random number generator produces values that are computationally unpredictable and suitable for cryptographic keys and initialization vectors.

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

How Software Security for Developers covers it

How deep it goes
Defines the security requirement and Java implementation, but defers randomness theory and mathematical proofs.
Examples
Uses Java SecureRandom byte generation and fresh 96-bit IVs for AES-GCM encryption.
What it assumes
Assumes basic Java and encryption familiarity while teaching the distinction between ordinary and cryptographic randomness.
What you can do after
Apply it via java.security.SecureRandom to generate cryptographic keys and fresh AES-GCM IVs.
How it connects
Connects to AES-GCM, initialization vectors, encryption keys, authenticated encryption, and Tink.

Also explained in

Compare how 3 books explain cryptographically secure random number generator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Security for Developers. The section links point to the parts of the book it draws on.