Android中的结果不是随机的

时间:2012-11-12 21:13:12

标签: java android encryption private-key

我正在实现一个使用大数字的加密算法,因此在创建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);

2 个答案:

答案 0 :(得分:3)

如果查看the docs,您将看到正在调用的构造函数中的rand参数未使用。

Implementation Note: the Random argument is ignored. This implementation uses OpenSSL's bn_rand as a source of cryptographically strong pseudo-random numbers.

也许删除它并提供null作为参数,或删除确定性参数,在这种情况下它将使用您的Random对象(根据文档。)

答案 1 :(得分:2)

根据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)