我正在实现一个使用大量数字的加密算法,因此在制作 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/