SecureRandom
The JDK includes a class, java.util.Random, that
implements a PRNG. Although it’s fine for light-duty use, it
has the following shortcomings:
It uses an algorithm that produces a predictable sequence of numbers.
If you don’t give
Randoma seed value, it uses the value of the system clock. This is a predictable seed. Let’s say that you create a random number in order to create a cryptographic key. If an attacker knows when you created the random number, even approximately, he or she can guess at likely values of the random number seed. With a relatively small amount of guessing, the attacker can guess which random number seed you’ve used. From this, the attacker can generate the same supposedly random cryptographic key that you just generated. Now the attacker can impersonate you or read your secret messages.
A stronger PRNG, java.security.SecureRandom, was
introduced in JDK 1.1. This class is based around a message digest.
SecureRandom
uses the SHA-1 (Secure Hash Algorithm)
message digest algorithm, which produces a 20-byte digest.
Here’s how it works:
The
SecureRandomis created using a seed. The seed value is digested, and the resulting value is stored as part of theSecureRandom’s internal state. An internal counter is initialized to zero.Every time
SecureRandomneeds to create more pseudo-random numbers, the message digest is updated with the internal state and the counter, which is incremented. This data is digested and returned as the new pseudo-random data.
Because ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access