# Java: SecureRandom.getInstanceStrong() vs new SecureRandom()

Given SecureRandom class is considered suitable for use in cryptography, I consider new SecureRandom() to be secure (funny term, isn't it?).

If new SecureRandom() already is secure, what would be the benefit of using SecureRandom.getInstanceStrong() instead?

Is this same kind of difference as between /dev/urandom and /dev/random?

I'm debating this in the following scenario, where I'm mostly concerned about making IV random (for use with AES-GCM):

private final SecureRandom secureRandom = new SecureRandom();

[...]

private byte[] getIv() {
int ivLength = 12;
byte[] iv = new byte[ivLength];
secureRandom.nextBytes(iv);
return iv;
}

• See the footnote: Depends on the OS, in IOS they are same Dec 24, 2021 at 20:22
• This is library call to get instance... SecureRandom.getInstanceStrong() See Maarten answer on so Dec 24, 2021 at 20:24
• A GCM IV doesn't need to be random. It only needs to be unique. So for this specific case, it doesn't matter. The only reason to use a random IV for GCM is if you can't remember all the IVs that have been previously used, so you instead rely on statistical uniqueness. For that, it's enough to have a statistically random value, it doesn't even need to be cryptographically random (which is a stronger property, including impredictability and independence from all other random values). Dec 24, 2021 at 23:22
• @kelalaka This is a bit of a Java mess actually. securerandom.source=file:/dev/random which block(ed) of course. So if you spun up an app server, it took like five minutes to return a http request. Even a non https daft one like my site which is pretty surprising (probably cookie generation). Dec 25, 2021 at 3:30
• What @Gilles said is true, If possible use AES-GCM-SIV to mitigate this issue... Dec 25, 2021 at 22:38

SecureRandom.getInstanceStrong() will ensure that a strong algorithm (securerandom.strongAlgorithms) will is used.

• It is available since Java version 8. Check your version before starting to use.

• If no such algorithm is available in running VM, it will throw NoSuchAlgorithmException.

• This failure is a better practice instead of defaulting into weak security.

• Welcome to Cryptography. A link to this claim or code line from the source will make this answer much better. Otherwise, this is a self claim. For example one could also say, it there is no strong ( what is the meaning of strong) algorithm then it will use the default one; java.util.Random. There is need for reference and in Java case, even versions.. Dec 27, 2021 at 21:51
• Thank your for your feedback kelalaka, I've added a bit more clarifications, as you suggested. Dec 27, 2021 at 23:56
• Is there a list of strongAlgorithms? Dec 28, 2021 at 6:33
• @kelalaka 'Strong' is a common term used by cryptographers, agencies and totalitarian governments to label an encryption can can't be defeated in real time. There's a list here. I don't know if AES should be added as there is no mathematical evidence that it's still secure. Dec 28, 2021 at 16:48
• @PaulUszak I'm talking about in the context of the Java Security and specially on the random number generatos. Dec 28, 2021 at 16:58