java 如何在ANDROID中生成NONCE并在客户端打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2414230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:01:07  来源:igfitidea点击:

how to generate NONCE and print it on client side in ANDROID

javaandroidclient-server

提问by sebby_zml

I am now working on a program for Androidwhich 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.

我现在正在开发一个与 IMS 相关的Android程序。我希望服务器将随机数作为字符串发送回客户端并在客户端打印。为了生成nonce,我尝试使用此站点中的代码。

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

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 nullat the client side. When I tried to print it on the server side, it also appear as null.

NONCE = generateNonce();一开始就声明。但问题是它没有获得 nonce 值,而是在客户端打印为null。当我尝试在服务器端打印它时,它也显示为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.

问候,塞比。

回答by Anthony Forloney

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

您需要将return语句添加到您想要返回的值中,截至目前您的代码生成(或似乎生成)了 NONCE,但您返回null因此 StringNONCE的值为null.

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

您还应该删除该return null语句。如果您保留它并且它是最后一个return 语句,您的代码将生成一个有效的 NONCE将返回null.

回答by Cetra

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

好吧,我会先整理一下您的代码的格式,因为它非常难以阅读。

Then I would remove the second SecureRandom instance.

然后我将删除第二个 SecureRandom 实例。

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

然后我会删除最后的return null;声明。

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.

还要确保函数被声明为返回一个整数而不是一个字符串。