Generating a Secure Random Number

This example creates a random number using a secure random number generator: See also Listing All Available Secure Random Number Generators.
try { // Create a secure random number generator SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // Get 1024 random bits byte[] bytes = new byte[1024/8]; sr.nextBytes(bytes); // Create two secure number generators with the same seed int seedByteCount = 10; byte[] seed = sr.generateSeed(seedByteCount); sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); SecureRandom sr2 = SecureRandom.getInstance("SHA1PRNG"); sr2.setSeed(seed); } catch (NoSuchAlgorithmException e) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.