1

I want to create a random alpha-numeric string whose bit count will always be of size k. The size k will be something relatively big (varying from 128 to 2048 or more). I'm reading this excellent thread and I'm trying to figure something out using the Random and the SecureRandom class but to no avail.

To be more precise, the result doesn't have to be necessarily a string, it could be anything as long as it is random and its bit count is always k.

Community
  • 1
  • 1
Aventinus
  • 1,322
  • 2
  • 15
  • 33
  • use [BigInteger](http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(int,%20java.util.Random)) – BeyelerStudios Nov 12 '15 at 12:10
  • 1
    How do you count bits of an alphanumeric string? – Sergey Kalinichenko Nov 12 '15 at 12:12
  • 3
    It's easy to generate a byte array of the right size: just use `SecureRandom.nextBytes(byte[])`. Round up to the nearest 8 bits and ignore the others if you need to... – Jon Skeet Nov 12 '15 at 12:13
  • If you already read the thread which answers your question, you should precisely explain why you still think needing to open a new question. The sentence “I'm trying to figure something out using … but to no avail” is not comprehensible considering that said thread already provides answers containing complete, working code. – Holger Nov 12 '15 at 12:41

3 Answers3

3

Have you checked http://docs.oracle.com/javase/7/docs/api/java/security/SecureRandom.html?

If you need 128 bit of random Data you would need 16 bytes in Java (because a byte is 8 bit) so after (shamelessly copied from said API):

  SecureRandom random = new SecureRandom();
  byte bytes[] = new byte[16];
  random.nextBytes(bytes);

now you have a Array with 128 bit of random Data. For 2048 it's a byte[256];

reineckm
  • 56
  • 5
  • This is probably a stupid a question but using this method always produces the exact same result, more specifically `[B@a881b5`. Is this supposed to happen? I want a different random output each time I run this. – Aventinus Nov 12 '15 at 13:10
  • 2
    [B@a881b5 is not the data but the ID of the Array when you do System.out.print(random) - See http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/types.html to understand. To get a String representation use java.util.Arrays.toString() – reineckm Nov 12 '15 at 13:24
1

Another possibility is the BigInteger constructor: BigInteger(int numBits, Random rnd).

Depending on how secure you need your random bits to be, use either Random or SecureRandom for the second parameter. There are various ways to convert a BigInteger to whatever final format you want. To get a string, use BigInteger.toString(2) where the 2 is the radix for binary.

rossum
  • 15,344
  • 1
  • 24
  • 38
0

If you truly want a specific number of bits, and cannot settle for the multiples of 8 that bytes can do, try using booleans with SecureRandom

SecureRandom sr = new SecureRandom();
boolean bools[] = new boolean[k];
for(int x = 0; x < bools.length; x++){
 bools[x] = sr.nextBoolean();
}
MrPublic
  • 520
  • 5
  • 16