Java random byte array

Programming for beginners

Random class provides nextBytes() method, it takes a byte array as argument and fill the input byte array with random bytes.

package com.sample.Random; import java.util.Random; public class RandomDemo  private static void printArray(byte[] byteArr)  for (byte b : byteArr)  System.out.print(b + ","); > > public static void main(String args[])  Random random = new Random(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); > > 
-56,108,-21,-73,83,1,-2,-117,-52,-66,-96,-51,89,-79,100,-106,121,68,79,81,

‘java.security.SecureRandom’ class provides nextBytes method, it takes byte array as argument and fill the byte array with random bytes. I would prefer to use Secure Random to generate random numbers than Random. It is because, Random class uses System clock to generate the seed, where as SecureRandom class uses random data from operating system (For ex interval betweem keystorkes, cpul cycles etc.,) to generate the seed. SecureRandom is more secure as compared to Random.

package com.sample.Random; import java.security.SecureRandom; public class RandomDemo  private static void printArray(byte[] byteArr)  for (byte b : byteArr)  System.out.print(b + ","); > > public static void main(String args[])  SecureRandom random = new SecureRandom(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); > > 
9,62,-83,114,6,70,68,-63,-94,33,-100,-75,-91,-36,-60,-26,27,-116,64,-113,

SecureRandom.getInstanceStrong() is avialble in Java1.8. ‘SecureRandom.getInstanceStrong()’ returns a secure random object that was selected by using the algorithms/providers specified in the securerandom.strongAlgorithms (Ex: Windows-PRNG:SunMSCAPI,SHA1PRNG:SUN)

By using below statements, you can get the algorithms specified in ‘securerandom.strongAlgorithms’ property

Источник

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

How to use Java create an array of N random bytes

This tutorial shows you how to use Java create an array of N random bytes.

Answer

To create an array of N random bytes in Java, you can use the java.util.Random class or the java.security.SecureRandom class. Here’s an example using the java.util.Random class:

import java.util.Random; public class RandomByteArrayExample < public static void main(String[] args) < int N = 10; // Number of random bytes Random random = new Random(); byte[] byteArray = new byte[N]; random.nextBytes(byteArray); System.out.println("Random byte array:"); for (byte b : byteArray) < System.out.print(b + " "); > > >

In this example, we create an instance of the Random class and initialize an array byteArray of size N . We then use the nextBytes() method of the Random class to fill the array with random bytes. Finally, we print the contents of the array.

If you need a more secure way to generate random bytes, you can use the java.security.SecureRandom class instead. Here's an example using SecureRandom :

import java.security.SecureRandom; public class SecureRandomByteArrayExample < public static void main(String[] args) < int N = 10; // Number of random bytes SecureRandom secureRandom = new SecureRandom(); byte[] byteArray = new byte[N]; secureRandom.nextBytes(byteArray); System.out.println("Random byte array:"); for (byte b : byteArray) < System.out.print(b + " "); > > >

Note

The usage is similar, but instead of using Random , we create an instance of SecureRandom for more secure random number generation.

  1. How to sort an Array in Java
  2. How to sort an Array in Descending (Reverse) Order in Java
  3. How to sort char array in Java example
  4. How to use Random shuffling of an array in Java
  5. How to use Java Concatenate two byte arrays
  6. How to use Java Convert byte array to object
  7. How to use Java Convert file to byte array
  8. How to use Java Convert InputStream to ByteArray
  9. How to use Java Create and write byte array to a file
  10. How to use Java equals() vs Arrays.equals
  11. How to use Java create an array of N random bytes
  12. How to use Java Search for an element in the array
  13. How to use Java Write byte array to a file
  14. How to Convert a primitive array to wrapper array in Java
  15. How to Convert a wrapper array to primitive array in Java
  16. How to Convert an array to set in Java
  17. How to Convert byte array to private, public keys in Java
  18. How to Convert certificate into base64 encoded array in Java
  19. How to Convert encoded byte array to certificate in Java
  20. How to get shallow copy of array in Java

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Читайте также:  Advanced Marker Simple HTML
Оцените статью