The nextBytes()
method is used to generate cryptographically strong pseudo-random bytes. This method is provided by the SecureRandom
class, which consists of methods to generate strong random numbers.
secureRandomInstnace.nextBytes(byte_array)
This method takes a byte_array
as a parameter and fills it with cryptographically strong pseudo-random bytes.
It doesn't return anything as it is an in-place operation.
import java.security.*;import java.util.*;public class main {public static void main(String[] argv){//create instance of securerandomSecureRandom rndm = new SecureRandom();//create byte arraybyte bytes[] = new byte[40];//generate random bytesrndm.nextBytes(bytes);//display the byte arraySystem.out.println(Arrays.toString(bytes));}}
SecureRandom
class and assign it to variable, rndm
.bytes[]
, with size as 40
.nextBytes()
method and pass byte array as a parameter to it, to generate cryptographically strong random bytes and fills the byte array, bytes[]
.