Page MenuHomec4science

KeyGenerator.java
No OneTemporary

File Metadata

Created
Tue, Jan 7, 00:17

KeyGenerator.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crypto.paillier;
import java.math.BigInteger;
import java.util.Random;
import java.util.Vector;
import crypto.PrimeGenerator;
/**
*
* @author raisaro
*/
public class KeyGenerator {
private BigInteger x;
private BigInteger p;
private BigInteger q;
private BigInteger n;
private BigInteger nsqr;
private BigInteger h;
private BigInteger g;
private BigInteger x1;
private BigInteger x2;
public KeyGenerator(){
PrimeGenerator pg = new PrimeGenerator();
Vector<BigInteger> bigPrimes = pg.getBigPrimes(320, 2);
this.p = bigPrimes.elementAt(0);
this.q = bigPrimes.elementAt(1);
this.n = p.multiply(q);
this.nsqr = n.multiply(n);
}
/**
* Generates a couple of private and public keys.
*/
public void generateKeys(){
generatePrivateKey();
generatePublicKey();
}
public BigInteger getG() {
return g;
}
public BigInteger getH() {
return h;
}
public BigInteger getN() {
return n;
}
public BigInteger getX() {
return x;
}
public BigInteger getX1() {
return x1;
}
public BigInteger getX2() {
return x2;
}
/**
* Generates a Private Key a random integer x € [1,n^2/2].
*/
private void generatePrivateKey(){
Random prng = new Random( System.currentTimeMillis() );
long rand;
// rand = Math.abs( prng.nextLong() ) % (nsqr.divide(BigInteger.valueOf(2))).longValue();
// x = BigInteger.valueOf(rand);
x = new BigInteger(2048, new Random());
x = x.mod(nsqr.divide(BigInteger.valueOf(2)));
x = x.add(BigInteger.ONE);
rand = Math.abs( prng.nextLong() ) % x.longValue();
x1 = BigInteger.valueOf(rand);
x2 = x.subtract(x1);
}
/**
* Generates a Public Key given a random integer a € Z*nsqr.
*/
private void generatePublicKey(){
Random prng = new Random( System.currentTimeMillis() );
long rand;
rand = Math.abs( prng.nextLong() ) % nsqr.longValue();
BigInteger a = BigInteger.valueOf( rand );
g = a.modPow(BigInteger.valueOf(2), nsqr);
h = g.modPow(x, nsqr);
}
}

Event Timeline