编程语言
首页 > 编程语言> > java – 结果在Android中不是随机的

java – 结果在Android中不是随机的

作者:互联网

我正在实现一个使用大数字的加密算法,因此在创建java应用程序时我必须使用BigInteger类.

但是,当我试图在android应用程序中实现相同的构造函数时

public BigInteger(int bitLength,int certainty,Random rnd), 

不生成随机BigInteger(同样生成相同的整数35879:P).在简单的Java应用程序中成功生成随机BigInteger.

供参考

http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#constructor_detail

另外,请告诉我,如果我导入的形式java.util.*,它是否可以在任何Android应用程序中工作?

如果Android不支持BigInteger或任何其他类似于支持的类,请发表您的评论???

这里方法systemoutprintln()用于将相应的字符串打印到布局.

这是一个供您参考的代码,

public class keyGenerator {

/**
 * @param args the command line arguments
 */

  static  Vector check = new Vector();


  static protected BigInteger p= new BigInteger("161");
  static protected BigInteger q= new BigInteger("47");
  static protected Random s =new Random();
  static protected BigInteger n = new BigInteger("1");
  static protected BigInteger trails;
  static protected BigInteger lambda ;
  static protected BigInteger nsq  = new BigInteger("1");
  static protected BigInteger g = new BigInteger("1");
  static protected BigInteger temp = new BigInteger("1");
  static protected long timetkn;
  static protected View myview;
  static protected String[] printarray = new String[1000];
  static protected int ii=0;

static protected int maxbit;
private static BigInteger two = new BigInteger("2");

public keyGenerator() {

  //  Activity activity = new Activity();
// et.append("Second Part!!");
// EditText et = (EditText)activity.findViewById(R.string.second);
//    et.append("Working");


    long keyStart = SystemClock.uptimeMillis();
    p = new BigInteger(7,1,s);

            Systemoutprintln("Assumed p :" +p.toString());
  /*  while(!isPrime(p) && ( (p.compareTo(two)==1) || (p.compareTo(two))==0) )
    {
            p = new BigInteger(7,1,s);

    Systemoutprintln(p.toString());
    }*/


    Systemoutprintln("Assumed q :" +p.toString());
    q = new BigInteger(7,1,s);

解决方法:

根据Random numbers上的java文档,这应该可行.但是,我注意到您正在使用两个不同的标签打印bigInteger p两次.您是否可能将它们混合在一起?

编辑:在digitaljoel的帮助下,我找到了这个先前的任务:

Is there a java equivalent to OpenSSL’s bn_rand_range?

基本上,你应该使用SecureRandom,并使用那里给出的方法,即:

Random r = new SecureRandom();    
BigInteger q = something_big;
BigInteger ans;

do
    BigInteger(bits_in_q, r);
while (ans > q)

标签:android,private-key,java,encryption
来源: https://codeday.me/bug/20190901/1784273.html