views:

289

answers:

2

Hello every1,

I am now working on a program for Android which is someting related to IMS. I want the Server to send back a nonce to the Client as a string and print it on the client side. In order to generate nonce, I tried using the code from this site.

http://www.exampledepot.com/egs/java.security/CreateSecureRandom.html

Part of my codes is as follow

public static String generateNonce() { 
  try {
   // Create a secure random number generator
   SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

   // Get 1024 random bits
   byte[] bytes = new byte[1024/8];
   sr.nextBytes(bytes);

   // Create two secure number generators with the same seed
   int seedByteCount = 10;
   byte[] seed = sr.generateSeed(seedByteCount);

   sr = SecureRandom.getInstance("SHA1PRNG");
   sr.setSeed(seed);
   SecureRandom sr2 = SecureRandom.getInstance("SHA1PRNG");
   sr2.setSeed(seed);
   } catch (NoSuchAlgorithmException e) {
  }
  //return NONCE;
  return null;
 }

I declared NONCE = generateNonce(); in the begining. But the problem is instead of getting a nonce value, it print as null at the client side. When I tried to print it on the server side, it also appear as null.

Can someone enlighten me the error in my codes or help me with a better or more suitable coding, please. Thanks a lot in advance.

Regards, Sebby.

A: 

Well, I'd sort out the format of your code first as it is pretty unreadable.

Then I would remove the second SecureRandom instance.

Then I would remove the return null; statement at the end.

Then I would rewrite the function to return a random integer like this:

 return sr.next(32);

Also making sure that the function is declared to return an integer rather than a string.

Cetra
A: 

You need to add the return statement to the value you'd like to return, as of now your code generates (or seems to generate) the NONCE but you return null so the String NONCE is given a value of null.

You should also remove the return null statement. If you leave it in and it's the last return statement, your code will generate a valid NONCE but will return null.

Anthony Forloney
thanks a lot. it helped.
sebby_zml
No problem, glad I could help. Good luck.
Anthony Forloney