【问题标题】:Result is not random in Android结果在Android中不是随机的
【发布时间】:2012-11-12 21:13:11
【问题描述】:

我正在实现一个使用大量数字的加密算法,所以在制作 Java 应用程序时我必须使用 BigInteger 类。

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

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

没有生成随机的 BigInteger(同样的整数一次又一次地生成 35879 :P)。在一个简单的 java Application 中成功生成一个随机的 BigInteger。

供参考,

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

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

如果 Android 不支持 BigInteger 或存在与支持的类似的任何其他类,请制作您的 cmets ???

这里使用 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 对象(BigDecimal 只是包装它)来检查它的行为。
  • 您应该使用SecureRandom 进行加密。
  • 您在哪种设备上获得这些结果?也许在模拟器上?
  • @owlstead 是 Galaxy Nexus S 4.1 手机

标签: java android encryption private-key


【解决方案1】:

如果您查看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 对象(根据文档)。

【讨论】:

  • @digitaljeol 我知道文档中提到了它,但是如果我将 null 作为参数传递,问题仍然存在。而且我无法消除确定性,因为算法要求 p 和 q 是素数 请参阅@paillier 密码系统。
  • 有趣。请注意bn_rand 文档,您需要在使用该功能之前播种 PRNG。似乎 PRNG 在使用前没有正确播种。这可能是一个非常重要的错误。
【解决方案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)

【讨论】:

  • 好的,谢谢,那是个错误!! .但是随机数仍然存在问题,它一次又一次地打印相同的p。
  • @pearsonphoto 看起来随机函数实际上是确定性的!每次我调用构造函数时,它都会给出一组不同的数字,但列表是相同的。这意味着您将始终获得相同的第一个数字,相同的第二个数字等等....
  • @pearsonphoto 和 digitaljoel !!您的解决方案值得称赞,请为我扩展一点。根据算法的要求,我需要一个质数。通过包括确定性,我确信在几乎大多数情况下,这个数字是素数。如果我创建一个显式方法来检查数字是否为素数并将其添加到循环的 while 条件中,那么预计会花费大量时间,这是我不想要的。请告诉我如何获得“可能是素数”的随机数。当引入确定性时,SecureRandom 不起作用:(
  • @rajatIIT:这不在您最初的问题中。我会开始一个新的问题来问这个问题,它与你所要求的有点不同......但是,有很多技巧可以确保这个数字是素数并让它快速工作。
  • 我是密码学和安卓的新手。请提供一些与技巧相关的链接或主题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2020-07-24
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多