Thursday, July 2, 2026
HomeLanguagesJavaSecureRandom nextBytes() method in Java with Examples

SecureRandom nextBytes() method in Java with Examples

The nextBytes() method of java.security.SecureRandom class is used to generate a user-specified number of random bytes.

If a call to setSeed had not occurred previously, the first call to this method forces this SecureRandom object to seed itself. This self-seeding will not occur if setSeed was previously called.

Syntax:

public void nextBytes(byte[] bytes)

Parameters: This method takes the array to be filled in with random bytes as parameter .

Note:

  1. The programs will not run on online IDE.
  2. Every time Secure Random class will generate random output.

Below are the examples to illustrate the nextBytes() method:

Example 1:




// Java program to demonstrate
// nextBytes() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  
            // Declaring the string variable
            String str = "Tajmahal";
  
            // Declaring the byte Array
            // converting string into byte
            byte[] b = str.getBytes();
  
            // printing the byte array
            System.out.println("Byte array before operation : " + Arrays.toString(b));
  
            // generating user-specified number of random bytes
            // using nextBytes() method
            sr.nextBytes(b);
  
            // printing the new byte array
            System.out.println("Byte array after operation : " + Arrays.toString(b));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [-79, -110, -18, -31, -54, -36, 63, -61]

Note: The following program throws the following Exception on GeeksForGeeks IDE but it will run efficiently on any Command Prompt(JDK)

Exception thrown : java.security.ProviderException: init failed

Example 2:




// Java program to demonstrate
// nextBytes() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
            // creating the object of SecureRandom
            SecureRandom sr = new SecureRandom(new byte[] { 1, 2, 3, 4 });
  
            // Declaring the string variable
            String str = "Tajmahal";
  
            // Declaring the byte Array b
            byte[] b = str.getBytes();
  
            // printing the byte array
            System.out.println("Byte array before operation : " + Arrays.toString(b));
  
            // generating user-specified number of random bytes
            // using nextBytes() method
            sr.nextBytes(b);
  
            // printing the new byte array
            System.out.println("Byte array after operation : " + Arrays.toString(b));
        }
  
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108]
Byte array after operation : [-14, 77, 123, 121, 116, 50, -89, -86]
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6966 POSTS0 COMMENTS