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

标签 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);

最佳答案

如果你看the docs您会看到正在调用的构造函数中的 rand 参数未被使用。

实现注意:Random 参数被忽略。此实现使用 OpenSSL 的 bn_rand 作为加密强伪随机数的来源。

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

关于java - 结果在 Android 中不是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351637/

相关文章:

java - 在 JFrame 上隐藏 LoadingImage 或 LoadingPanel

java - 如何判断RESTful webservice中执行了正确的请求方法?

java - Neo4j 数据检索中出现空点异常

android - LocationManager requestLocationUpdates 和 android 中的 timertask

powershell - Plink/PuTTY 使用加密密码

encryption - 使用与 key 相同的字符串来加密字符串是否安全?

java - 如何使用外部公共(public) IP 从另一台计算机查看 GWT devmode

android - Google Place Picker 获取城市?

android - 如何在非gui linux服务器上构建android apk

asp.net - System.Security.Cryptography.CryptographicException : Not enough storage is available to process this command