A strong random number generator for Java encryption __java

Source: Internet
Author: User
Tags generator
SecureRandom java.security
Class SecureRandom
Java.lang.Object

  java.util.Random

      java.security.SecureRandom

all implemented interfaces:Serializable
SecureRandom
Extends Random

This class provides a cryptographically strong random number generator (RNG). Many implementations are pseudo random number generators (PRNG), which means that they will use a determined algorithm to generate pseudo-random sequences based on actual random seeds. Other implementations can generate actual random numbers, while others may use these two techniques together.

The strong random number of encryption should at least comply with the statistical random number generator test specified in the 4.9.1 section of the FIPS 140-2, security Requirements for cryptographic Modules. In addition, securerandom must produce non-deterministic output, so, as described in the following article, the seed material must be unpredictable and the securerandom output must be a strong sequence of encryption: RFC 1750:randomness Recommendations for the security.

Like other algorithmic classes in Java security, SecureRandom also provides an implementation-independent algorithm, so the caller (application code) requests a particular RNG algorithm and passes it back to the SecureRandom object of the algorithm. If you want, you can also request a specific algorithm through a specific provider. See GetInstance method.

Therefore, there are two ways to request SecureRandom objects: Specify only the algorithm name, or specify both an algorithm name and a package provider. If you specify only the algorithm name, as follows:

      SecureRandom random = securerandom.getinstance ("sha1prng");
 
The system will determine whether there is a request for the implementation of the algorithm, whether there are multiple, whether there is a preferred implementation.

If you specify both an algorithm name and a package provider, as follows:

      SecureRandom random = securerandom.getinstance ("Sha1prng", "SUN");
 
The system determines whether an algorithm is implemented in the requested package, or throws an exception if it is not.

The SecureRandom implementation attempts to completely randomize the internal state of the generator itself, unless the caller calls the Setseed method after calling the GetInstance method:

      SecureRandom random = securerandom.getinstance ("sha1prng");
      Random.setseed (seed);
 

After the caller obtains the SecureRandom object from the getinstance call, it can invoke nextbytes to generate random bytes:

      byte bytes[] = new BYTE[20];
      Random.nextbytes (bytes);
 

Callers can also call the Generateseed method to generate the given number of seed bytes (for example, to provide seed for other random number generators):

      byte seed[] = Random.generateseed (20);
 

See also: Securerandomspi, Random, serialization table

<!----> <!----> Construction Method Summary

SecureRandom ()
By using this construction method, the caller can obtain a SecureRandom object that contains the implementation from the highest priority installed provider with the SecureRandom implementation.
SecureRandom (byte[] seed)
By using this construction method, the caller can obtain a SecureRandom object that contains the implementation from the highest priority installed provider with the SecureRandom implementation.
Protected SecureRandom (Securerandomspi Securerandomspi, Provider Provider)
Creates a SecureRandom object.
<!---->

<!----> Method Summary

Byte[] Generateseed (int numbytes)
Returns the number of seed bytes given, which can be computed using the seed generation algorithm that this class uses to provide seed for itself.
String Getalgorithm ()
Returns the name of the algorithm implemented by this SecureRandom object.
Static SecureRandom getinstance (String algorithm)
Generates a SecureRandom object that implements the specified random number generator (RNG) algorithm.
Static SecureRandom getinstance (String algorithm, Provider Provider)
If the provider is able to provide such a RNG implementation, the SecureRandom object is generated for the specified RNG algorithm provided by the specified provider.
Static SecureRandom getinstance (String algorithm, string provider)
If the provider is able to provide such a RNG implementation, the SecureRandom object is generated for the specified RNG algorithm provided by the specified provider.
Provider Getprovider ()
Returns the provider for this SecureRandom object.
Static byte[] Getseed (int numbytes)
Returns the number of seed bytes given, which can be computed using the seed generation algorithm that this class uses to provide seed for itself.
protected int Next (int numbits)
Generates an integer containing the user-specified pseudo-random digits (right-aligned, with leading 0).
void nextbytes (byte[] bytes)
Generates a random number of bytes specified by the user.
void Setseed (byte[] seed)
Re-provide the seed for this random object.
void Setseed (Long Seed)
Provides a new seed for this random object using the eight bytes contained in the given long seed.

<!----> methods of inheriting from class Java.util.Random

Nextboolean, Nextdouble, Nextfloat, Nextgaussian, Nextint, Nextint, Nextlong

<!----> methods of inheriting from class Java.lang.Object

Clone, Equals, Finalize, GetClass, Hashcode, notify, Notifyall, toString, wait, wait, wait

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.