Open In App

Random vs Secure Random numbers in Java

Last Updated : 10 Nov, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Prerequisite: Generating Random numbers in Java
java.security.SecureRandom class: This class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1. Additionally, SecureRandom must produce non-deterministic output. Therefore any seed material passed to a SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong.
java.util.Random class: The classes defined in Random are not cryptographically strong, and the numbers chosen are not completely random because a definite mathematical algorithm (based on Donald E. Knuth’s subtractive random number generator algorithm) is used to select them. Therefore, it is not safe to use this class for tasks that require a high level of security, like creating a random password etc. 

Random vs SecureRandom

  1. Size: A Random class has only 48 bits whereas SecureRandom can have up to 128 bits. So the chances of repeating in SecureRandom are smaller.
  2. Seed Generation: Random uses the system clock as the seed/or to generate the seed. So they can be reproduced easily if the attacker knows the time at which the seed was generated. But SecureRandom takes Random Data from your OS (they can be interval between keystrokes etc – most OS collect these data and store them in files – /dev/random and /dev/urandom in case of Linux/solaris) and use that as the seed.
  3. Breaking the code: In case of random, just 2^48 attempts are required, with today’s advanced CPU‘s it is possible to break it in practical time. But for securerandom 2^128 attempts will be required, which will take years and years to break even with today’s advanced machines.
  4. Generating Function: The standard Oracle JDK 7 implementation uses what’s called a Linear Congruential Generator to produce random values in java.util.Random. Whereas Secure Random implements SHA1PRNG algorithm, which uses SHA1 to generate pseudo-random numbers. The algorithm computes the SHA-1 hash over a true random number(uses an entropy source) and then concatenates it with a 64-bit counter which increments by 1 on each operation.
  5. Security: Consequently, the java.util. The random class must not be used either for security-critical applications or for protecting sensitive data. 

Generating Random number using java.util.Random; 

Java




// A Java program to demonstrate
// random number generation
// using java.util.Random;
import java.util.Random;
 
public class generateRandom {
 
    public static void main(String args[])
    {
        // create instance of Random class
        Random rand = new Random();
 
        // Generate random integers in range 0 to 999
        int rand_int1 = rand.nextInt(1000);
        int rand_int2 = rand.nextInt(1000);
 
        // Print random integers
        System.out.println("Random Integers: " + rand_int1);
        System.out.println("Random Integers: " + rand_int2);
    }
}


Output: 

Random Integers: 956
Random Integers: 678

Generating Random number using java.security.SecureRandom;

Java




// A Java program to demonstrate secure
// random number generation
// using java.security.SecureRandom
import java.security.SecureRandom;
 
public class generateRandom {
 
    public static void main(String args[])
    {
        // create instance of SecureRandom class
        SecureRandom rand = new SecureRandom();
 
        // Generate random integers in range 0 to 999
        int rand_int1 = rand.nextInt(1000);
        int rand_int2 = rand.nextInt(1000);
 
        // Print random integers
        System.out.println("Random Integers: " + rand_int1);
        System.out.println("Random Integers: " + rand_int2);
    }
}


Output: 

Random Integers: 817
Random Integers: 500



Similar Reads

Difference between Secure Socket Layer (SSL) and Secure Electronic Transaction (SET)
Secure Socket Layer (SSL): Secure Socket Layer (SSL) is the normal security technology for establishing an associate encrypted link between an internet server and a browser. This link ensures that each knowledge passed between the online server and browsers stays personal and integral. SSL is associate trade normal and is employed by numerous websi
7 min read
Difference between File Transfer Protocol (FTP) and Secure File Transfer Protocol (SFTP)
FTP (File Transfer Protocol) It is a protocol that is used to transfer or copy the file from one host to another host. But there may be some problems like different file names and different file directories while sending and receiving a files in different hosts or systems. And in FTP, a secure channel is not provided to transfer the files between t
3 min read
Difference between Secure Socket Layer (SSL) and Transport Layer Security (TLS)
SSL stands for Secure Socket Layer while TLS stands for Transport Layer Security. Both Secure Socket Layer and Transport Layer Security are the protocols used to provide security between web browsers and web servers. The main difference between Secure Socket Layer and Transport Layer Security is that, in SSL (Secure Socket Layer), the Message diges
2 min read
Spring Security - Secure Your Web Application
Spring Security is a powerful and highly customizable security framework that provides authentication, authorization, and other security features for Spring-based applications. It is a widely used open-source project that helps developers to secure their web applications by implementing security policies and rules. Spring Security provides a set of
7 min read
Generating random numbers in Java
Java provides three ways to generate random numbers using some built-in methods and classes as listed below: java.util.Random classMath.random method : Can Generate Random Numbers of double type.ThreadLocalRandom class1) java.util.RandomFor using this class to generate random numbers, we have to first create an instance of this class and then invok
4 min read
Java.util.Random class in Java
Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random data types such as float, double, int. Constructors: Random(): Creates a new random number generator Random(
4 min read
Java.util.Random.nextInt() in Java
Generating random numbers themselves have a good utility value and having them achieved by the usage of function can prove to be very useful. Java in its language has dedicated an entire library to Random numbers seeing its importance in day-day programming. nextInt() is discussed in this article. java.util.Random.nextInt() : The nextInt() is used
4 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java Math random() Method
The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random. Declaration of Java Math random()Below is the declaration of java.lang.Math.random
2 min read
StrictMath random() Method in Java
The random() is an inbuilt method of StrictMath class in java which is used to get a double value with a positive sign that is greater than or equal to 0.0 and less than 1.0. random() method is accurately organized to acquiesce appropriate use by more than one thread. The values which are returned are adopted pseudorandomly with constant distributi
2 min read
Practice Tags :
three90RightbarBannerImg