Page MenuHomec4science

Test.java
No OneTemporary

File Metadata

Created
Tue, Jan 7, 02:02

Test.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crypto.paillier;
import java.math.BigInteger;
import java.sql.*;
import crypto.CryptoException;
/**
*
* @author raisaro
*/
public class Test {
private static Connection connect;
private static PreparedStatement preparedStatement;
private static ResultSet resultSet;
public static void main (String[] args) throws ClassNotFoundException, SQLException, CryptoException{
BigInteger[] publicKey = getPublicKey(1); //N,G,H
BigInteger mcPrivateKey = getMCkey(1);
BigInteger spuPrivateKey = getSPUkey(1);
Scheme s = new Scheme(publicKey[0], publicKey[1], publicKey[2], spuPrivateKey);
Scheme s2 = new Scheme(publicKey[0], publicKey[1], publicKey[2], mcPrivateKey);
BigInteger[] offline_numbers = null;
BigInteger[] cipher = null;
long start_time = System.currentTimeMillis();
for(int i=0;i<1;i++){
offline_numbers = getOfflineNumbers(i, 1);
// cipher = s.encrypt(BigInteger.valueOf(1), offline_numbers[0], offline_numbers[1]);
}
long end_time = System.currentTimeMillis();
cipher = s.encrypt(new BigInteger("227581098092933686505787"), offline_numbers[0], offline_numbers[1]);
System.out.println("Encryption done");
System.out.println("Encryption time for 1 iterations = "+ (end_time-start_time)+ " ms");
BigInteger[] proxy_cipher = s.proxyDecription(cipher);
BigInteger plain = s2.decrypt(proxy_cipher);
System.out.println("Decryption done");
System.out.println("Result: "+ new String(plain.toByteArray()));
}
private static BigInteger getMCkey(int p_code) throws ClassNotFoundException, SQLException{
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.mc_private_key "
+ "WHERE p_code = ? ");
preparedStatement.setInt(1, p_code);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
BigInteger mcPrivateKey = new BigInteger(resultSet.getString("key"));
closeConnection();
return mcPrivateKey;
}else{
closeConnection();
return null;
}
}
private static BigInteger getSPUkey(int p_code) throws ClassNotFoundException, SQLException{
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.ttp_private_key "
+ "WHERE p_code = ? ");
preparedStatement.setInt(1, p_code);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
BigInteger spuPrivateKey = new BigInteger(resultSet.getString("key"));
closeConnection();
return spuPrivateKey;
}else{
closeConnection();
return null;
}
}
private static BigInteger[] getOfflineNumbers (int id, int p_code) throws ClassNotFoundException, SQLException{
BigInteger[] offline_numbers = new BigInteger[2];
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/offline_computation?"
+ "user=root&password=root");
preparedStatement = connect.prepareStatement("SELECT * FROM offline_computation.offline_values "
+ "WHERE id = ? AND p_code = ? ");
preparedStatement.setInt(1, id);
preparedStatement.setInt(2, p_code);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
offline_numbers[0] = new BigInteger(resultSet.getString("gPOWr"));
offline_numbers[1] = new BigInteger(resultSet.getString("hPOWr"));
closeConnection();
return offline_numbers;
}else{
closeConnection();
return offline_numbers;
}
}
private static BigInteger[] getPublicKey(int p_code){
try{
BigInteger[] publicKey = new BigInteger[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] = new BigInteger(resultSet.getString("n"));
publicKey[1] = new BigInteger(resultSet.getString("g"));
publicKey[2] = new BigInteger(resultSet.getString("h"));
closeConnection();
return publicKey;
}else{
closeConnection();
return null;
}
}
catch(ClassNotFoundException e){
closeConnection();
return null;
} catch (SQLException e) {
closeConnection();
return null;
}
}
/**
* This method close the connection to any database
*/
private static void closeConnection() {
try {
if (resultSet != null) {
resultSet.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}

Event Timeline