Why Java’s Random Is Insecure and How SecureRandom Saves Your Data

The article explains that Java’s Random class uses a predictable linear‑congruential generator, making its output vulnerable to seed‑guessing attacks, and demonstrates how SecureRandom with strategies like SHA1PRNG, NativePRNGBlocking, and NativePRNGNonBlocking provides stronger, non‑predictable randomness suitable for security‑critical applications.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Java’s Random Is Insecure and How SecureRandom Saves Your Data

Random seed same → same sequence

When the seed is fixed, e.g., 0, Java Random produces identical sequences across runs, allowing an attacker who knows the seed to predict all future values.

Random random = new Random(0);
int cnt = 10;
for (int i = 0; i < cnt; i++) {
    System.out.println(random.nextLong());
}

Java Random does not use 0 as default seed; it combines a dynamic seed using seedUniquifier() and System.nanoTime() with XOR to obtain a unique initial seed.

The seedUniquifier() method uses an AtomicLong and a CAS loop to generate a different seed for each thread.

private static final AtomicLong seedUniquifier = new AtomicLong(8682522807148012L);
private static long seedUniquifier() {
    for (;;) {
        long current = seedUniquifier.get();
        long next = current * 181783497276652981L;
        if (seedUniquifier.compareAndSet(current, next))
            return next;
    }
}

Even with this, the 48‑bit state of java.util.Random can be recovered from a few observed outputs; an attacker can brute‑force the seed in far less than 2^48 attempts.

Seed can be guessed

Regardless of the seed generation method, attackers can infer the seed from observed outputs, and in practice can predict the next values within seconds.

Java Random security analysis

Knowing two consecutive outputs of nextLong or nextDouble is enough to predict the rest of the sequence; the same applies to nextInt and nextFloat if the first two values are leaked.

Random generates only 48 bits of randomness, making exhaustive search feasible on modern CPUs.

SecureRandom for safe randomness

Java provides SecureRandom, which generates cryptographically strong random numbers. Compared to Random, SecureRandom can produce up to 128 bits and does not rely on a fixed seed.

SecureRandom can generate 128‑bit values, while Random is limited to 48 bits.

SecureRandom continuously reads entropy from the operating system’s random devices ( /dev/random or /dev/urandom).

On Linux, three SecureRandom strategies are available:

SHA1PRNG

A pseudo‑random generator based on SHA‑1, used as the default on Windows.

NativePRNGBlocking

Initial seed is taken from 20 bytes of /dev/random; each generation XORs the internal SHA‑1 output with fresh data from /dev/random, which may block if entropy is low.

NativePRNGNonBlocking

Uses /dev/urandom for non‑blocking entropy, providing faster but slightly lower‑quality randomness.

In web services, NativePRNGNonBlocking is recommended for stable performance.

Performance comparison

In a single‑threaded benchmark of 1 000 000 iterations, Random completes in about 10 ms, while each SecureRandom variant takes more than ten times longer.

Summary

Random is insecure for high‑security scenarios; knowing two outputs can compromise the whole sequence.

Use SecureRandom for generating passwords, tokens, or redemption codes.

SecureRandom relies on /dev/random and /dev/urandom to keep the seed constantly changing.

For web applications, NativePRNGNonBlocking offers a good balance of security and performance.

Random provides the best raw performance, but SecureRandom variants are roughly ten times slower.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javacryptographyrandomSecureRandom
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.