I am implementing an encryption algorithm which uses large numbers, so while making a java application I have to use the class BigInteger.
However, when I tried to implement the same in an android application the constructor
public BigInteger(int bitLength,int certainty,Random rnd),
no random BigInteger is generated (The same integer is generated again and again 35879 :P) . A random BigInteger is generated successfully in a simple java Application.
For a reference,
http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#constructor_detail
Also, Please tell me if I import something of the form java.util.* , is it expected to work in any android application ??
Please make your comments if Android does not support BigInteger or any other class exists similar to that which is supported ???
Here the method systemoutprintln() is used for printing the respective string to a layout.
Here is a code for your reference,
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);
This should work, according to the java docs on Random numbers. However, I am noticing that you are printing the bigInteger p twice, with two different labels. Is it remotely possible that you are mixing them up?
EDIT: With some help from digitaljoel, I found this prior quest:
Is there a java equivalent to OpenSSL's bn_rand_range?
Essentially, you should use SecureRandom, and use the method given there, namely: