Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating 128 bit random key in java

Tags:

java

random

I want to generate a 128 bit random key in java. I am using the following:

byte[] byteBucket = new byte[bytelength];
randomizer.nextBytes(byteBucket);

Will my byte array length be 16 as (16*8=128) or 128?

like image 216
Phalguni Mukherjee Avatar asked May 12 '14 11:05

Phalguni Mukherjee


People also ask

How do you generate a random number from 1 to 100 in Java?

Here is the code to generate a random number between 1 and 100 and save it to a new integer, showMe: int showMe = min + randomNum. nextInt(max);

How do you generate a random number between 1000 and 9999 in Java?

int randomNumber = ( int )( Math. random() * 9999 ); if( randomNumber <= 1000 ) { randomNumber = randomNumber + 1000; Math. random() is a method that generates a random number through a formula.

How do you randomly generate random numbers in Java?

Random random = new Random(); int rand = random. nextInt(); Yes, it's that simple to generate a random integer in java.


1 Answers

try SecureRandom API.

SecureRandom random = new SecureRandom();
byte bytes[] = new byte[16]; // 128 bits are converted to 16 bytes;
random.nextBytes(bytes);
like image 75
Pramod S. Nikam Avatar answered Sep 29 '22 12:09

Pramod S. Nikam