Page MenuHomec4science

OffLineNumbers.java
No OneTemporary

File Metadata

Created
Tue, Jan 7, 00:18

OffLineNumbers.java

package crypto.paillier;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
/**
*
* @author raisaro
*/
public class OffLineNumbers {
private static ResultSet resultSet;
private static PreparedStatement preparedStatement;
private static Connection connect;
public static void main (String[] args) throws ClassNotFoundException, SQLException{
System.out.println("Offline computation --- Start ---");
BigInteger gPOWr;
BigInteger hPOWr;
BigInteger n;
BigInteger g;
BigInteger h;
BigInteger r;
BigInteger nsqr;
int p_code = 1;
String[] pubKey;
String privKey = "";
pubKey = getPublicKey(p_code);
n = new BigInteger(pubKey[0]);
g = new BigInteger(pubKey[1]);
h = new BigInteger(pubKey[2]);
nsqr = n.multiply(n);
int tot_loops = 4000000;
long start_time = System.currentTimeMillis();
for (int i=0;i<tot_loops;i++){
do{
// System.out.println("!");
r = new BigInteger(1024, new Random());
r = r.mod(n.divide(BigInteger.valueOf(4)));
r = r.add(BigInteger.ONE);
// long Gstart_time = System.currentTimeMillis();
gPOWr = g.modPow( r, nsqr );
// long Gend_time = System.currentTimeMillis();
// System.out.println("gPOWr time : "+(Gend_time-Gstart_time)+ " ms.");
}while((crypto.Utils.gcd(gPOWr.longValue(),nsqr.longValue()) != 1)||(r.equals(BigInteger.ZERO)));
// long Hstart_time = System.currentTimeMillis();
hPOWr = h.modPow(r, nsqr);
// long Hend_time = System.currentTimeMillis();
// System.out.println("hPOWr time : "+(Hend_time-Hstart_time)+ " ms.");
uploadDB (i, p_code, gPOWr.toString(), hPOWr.toString());
System.out.println((i+1)+" / "+tot_loops);
}
long end_time = System.currentTimeMillis();
System.out.println("Computational time : "+(end_time-start_time)+ " ms.");
System.out.println ("Offline computation --- End ---");
}
private static void uploadDB (int id, int p_code, String gPOWr, String hPOWr) throws ClassNotFoundException, SQLException{
// long s = System.currentTimeMillis();
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/offline_computation?"
+ "user=root&password=root");
preparedStatement = connect.prepareStatement("INSERT into offline_computation.offline_values VALUES (?,?,?,?) ");
preparedStatement.setInt(1, id);
preparedStatement.setInt(2, p_code);
preparedStatement.setString(3, gPOWr);
preparedStatement.setString(4, hPOWr);
preparedStatement.executeUpdate();
closeConnection();
// long e = System.currentTimeMillis();
// System.out.println("Time to upload : "+(e-s)+" ms.");
}
private static String[] getPublicKey(int p_code){
try{
String[] publicKey = new String[3];
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/genomic_privacy_key?"
+ "user=root&password=root");
preparedStatement = connect.prepareStatement("SELECT * FROM genomic_privacy_key.public_key "
+ "WHERE p_code = ? ");
preparedStatement.setInt(1, p_code);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
publicKey[0] = resultSet.getString("n");
publicKey[1] = resultSet.getString("g");
publicKey[2] = resultSet.getString("h");
closeConnection();
return publicKey;
}else{
closeConnection();
return publicKey;
}
}
catch(SQLException e){
closeConnection();
return null;
} catch (ClassNotFoundException e) {
closeConnection();
return null;
}
}
private static String getPrivateKey(int p_code){
try{
String privateKey = "";
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/genomic_privacy_key?"
+ "user=root&password=root");
preparedStatement = connect.prepareStatement("SELECT mc.key ttp.key FROM genomic_privacy_key.mc_private_key AS mc , genomic_privacy_key.ttp_private_key AS ttp "
+ "WHERE ttp.p_code = ? AND ttp.p_code = mc.p_code ");
preparedStatement.setInt(1, p_code);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
BigInteger mc_pk = new BigInteger(resultSet.getString(1));
BigInteger ttp_pk = new BigInteger(resultSet.getString(2));
BigInteger final_pk = mc_pk.add(ttp_pk);
closeConnection();
privateKey = final_pk.toString();
return privateKey;
}else{
closeConnection();
return privateKey;
}
}
catch(SQLException e){
closeConnection();
return null;
} catch (ClassNotFoundException e) {
closeConnection();
return null;
}
}
private static void closeConnection() {
try {
if (resultSet != null) {
resultSet.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}

Event Timeline