Skip to content

Instantly share code, notes, and snippets.

@Doogiemuc
Created December 26, 2017 17:05
Show Gist options
  • Save Doogiemuc/c4287b09f8342b62083c4dee6e2fea8d to your computer and use it in GitHub Desktop.
Save Doogiemuc/c4287b09f8342b62083c4dee6e2fea8d to your computer and use it in GitHub Desktop.
Damgard Jurik Encryption for eVoting - Not yet working example that 'tries' to use the great SCAPI lib
package org.doogie;
import edu.biu.scapi.midLayer.asymmetricCrypto.encryption.DJKeyGenParameterSpec;
import edu.biu.scapi.midLayer.asymmetricCrypto.encryption.DamgardJurikEnc;
import edu.biu.scapi.midLayer.asymmetricCrypto.encryption.ScDamgardJurikEnc;
import edu.biu.scapi.midLayer.asymmetricCrypto.keys.ScDamgardJurikPublicKey;
import edu.biu.scapi.midLayer.ciphertext.AsymmetricCiphertext;
import edu.biu.scapi.midLayer.plaintext.BigIntegerPlainText;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyException;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.spec.InvalidParameterSpecException;
/**
* Example for casting votes that are encrypted with the DamgardJuric Encryption Scheme
* This is just my try of using this lib. I barely understand what I am doing here :-)
*/
public class DamgardJurikTest {
public static void main(String[] args) {
try {
DamgardJurikTest dj = new DamgardJurikTest();
} catch (Exception e) {
System.out.println("FATAL ERROR: "+e);
e.printStackTrace();
}
}
public DamgardJurikTest() throws Exception {
PublicKey tallyPublicKey = setupTally();
DamgardJurikEnc voteEncryptor = setupVoter(tallyPublicKey);
castVote(voteEncryptor, BigInteger.ONE); // 0/1 == vote for or against a candidate
doTally();
}
/**
* Setup a tally board that can receive (encrypted) votes.
* @return The publicKey of this tally board, that voters need to encrypt their personal votes
* @throws InvalidParameterSpecException
*/
public PublicKey setupTally() throws InvalidParameterSpecException {
log("setupTally...");
//Create a DamgardJurik encryption object.
DamgardJurikEnc tallyEnc = new ScDamgardJurikEnc();
//Generate a keyPair using the DamgardJurik object.
KeyPair tallyKeyPair = null;
try {
tallyKeyPair = tallyEnc.generateKey(new DJKeyGenParameterSpec(128, 40));
} catch (InvalidParameterSpecException e) {
System.out.println("Cannot generate key for Tally " + e);
throw e;
}
//Publish your public key.
BigInteger modulus = ((ScDamgardJurikPublicKey)tallyKeyPair.getPublic()).getModulus();
log("tally public key = "+modulus);
return tallyKeyPair.getPublic();
}
/**
* setup a voter that can later cast a vote
* @param tallyPublicKey the publicKey of the tally board that we will send our (encrypted) vote to. We will encrypt with this public key.
* @throws InvalidParameterSpecException
* @throws InvalidKeyException
*/
public DamgardJurikEnc setupVoter(PublicKey tallyPublicKey) throws InvalidParameterSpecException, InvalidKeyException {
BigInteger modulus = ((ScDamgardJurikPublicKey)tallyPublicKey).getModulus();
log("setupVoter(tallyPublicKey="+modulus+")");
//Create a DamgardJurik encryption object.
DamgardJurikEnc voteEnc = new ScDamgardJurikEnc();
//Generate a keyPair using the DamgardJurik object.
KeyPair votersKeyPair = null;
try {
votersKeyPair = voteEnc.generateKey(new DJKeyGenParameterSpec(128, 40));
} catch (InvalidParameterSpecException e) {
System.out.println("Cannot generate key for Voter " + e);
throw e;
}
//Publish your public key.
//publish(pair.getPublic()); // why is that necessary?
//Set private key and party2's public key:
try {
voteEnc.setKey(tallyPublicKey, votersKeyPair.getPrivate());
} catch (InvalidKeyException e) {
System.out.println("Cannot use public key of authority. InvalidKeyException: " + e);
throw e;
}
return voteEnc;
}
public void castVote(DamgardJurikEnc voteEncryptor, BigInteger vote) {
log("castVote(vote='"+vote+"')");
//Get the BigInteger value to encrypt, create a BigIntegerPlaintext with it and encrypt the plaintext.
BigIntegerPlainText plaintext = new BigIntegerPlainText(vote); //Remark: There is also a constructor for String. Can I also encrypt Strings?
log("Plaintext = "+plaintext);
AsymmetricCiphertext cipher = voteEncryptor.encrypt(plaintext);
log("Encrypted vote = "+cipher);
//Send cipher and keys to the receiver.
log("============");
try {
BigIntegerPlainText decrypted_plaintext = (BigIntegerPlainText) voteEncryptor.decrypt(cipher);
log("Decrypted "+decrypted_plaintext);
//String decr_vote = new String(decrypted_plaintext.getX().toByteArray());
//log("Decrypted vote = "+decr_vote);
} catch (KeyException e) {
System.out.println("ERROR: Cannot decrypt plaintext "+e);
e.printStackTrace();
}
}
/** sum up the (encrypted) votes */
public void doTally() {
log("doTally ... <not implemented yet>");
//TODO:
}
/**
* very simple log output
* @param msg any string message
*/
public static void log(String msg) {
System.out.println(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment