xtremebytes
xtremebytes

Reputation: 183

Elliptic curve ElGamal Java implementation

Is there a simple implementation (using Java BigInteger) of ElGamal elliptic curve cryptosystem with key generation, encryption and decryption functions; one that could be used to explain to university students in a lecture?

For example, a Paillier encrypt function can be coded, without loss of generality, as:

public BigInteger encrypt(BigInteger input) throws PaillierException  {
    if(!isInZN(input)) {
        throw new PaillierException(PaillierException.TYPE_PLAINTEXT_NOT_IN_ZN, input);
    }
    BigInteger plaintext = handleNegative(input);
    BigInteger r = randomInZStarN();
    return (((n.multiply(plaintext).add(BigInteger.ONE)).multiply(r.modPow(n, nSquared)))).mod(nSquared);
}

which contains an optimisation g=(1+n), which makes the ciphertext c = (1 + mn) r^n mod n^2.

Please DO NOT suggest the Java 7 native implementation or BouncyCastle ones because I don't need a real world JCA compliant complex implementation.

Thanks.

Upvotes: 3

Views: 2312

Answers (2)

x4k3p
x4k3p

Reputation: 1779

Check this out. (Modify for your needs)

//X9ECParameters ecParams = NISTNamedCurves.getByName("B-571");
        X9ECParameters ecParams = NISTNamedCurves.getByName("B-163");
        //X9ECParameters ecParams = NISTNamedCurves.getByName("P-384");
        //X9ECParameters ecParams = NISTNamedCurves.getByName("P-521");
        BigInteger privKey = new BigInteger("38e1", 16);
        ECPoint pubKey = ecParams.getG().multiply(privKey);

        System.out.println("Available curves:\n");

        int counter = 0;
        for ( Enumeration e = NISTNamedCurves.getNames(); e.hasMoreElements(); ){
            if (counter == 3) {
                counter = -1;
                System.out.println( e.nextElement().toString() );
            }else{
                System.out.print( e.nextElement().toString() + "    ");
            }
             counter++;
        }

//      System.out.println(privKey.toString(16));

//      for (int i = 1; i < 30; i++) {
//          ECPoint test = ecParams.getG().multiply(
//                  new BigInteger(Integer.toHexString(i), 16));
//          System.out.println("X: " + test.getX().toBigInteger().toString(10)
//                  + "\n" + "Y: " + test.getY().toBigInteger().toString(10)
//                  + "\n");
//      }

        //Encryption "a" = first Point

        ECPoint pMsg = ecParams.getG().multiply(
                new BigInteger(Integer.toHexString(2), 16));

        System.out.println("\n\n--------------------------------------------------------------------------------------------------------------------------------------------------");

        System.out.println("Selected curve:\n");

        System.out.println("Curve:     " + ecParams.getCurve().getFieldSize() + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        System.out.println("Point:     " + "X: " + pMsg.getX().toBigInteger().toString(16) + "\n" + "           Y: " + pMsg.getY().toBigInteger().toString(16) + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        ECPoint one;
        ECPoint two;

        BigInteger random = new BigInteger(16, new SecureRandom());

        /* g^r */
        one = ecParams.getG().multiply(random);
        /* pk^r+m */
        two = pMsg.add(pubKey.multiply(random));

        //encryptedData edM = new encryptedData(one, two);

        System.out.println("Encrypted:\n");

        System.out.println("One:      " + "X: " + one.getX().toBigInteger().toString(16) + "\n" + "           Y: " + one.getY().toBigInteger().toString(16) + "\n");
        System.out.println("Two:      " + "X: " + two.getX().toBigInteger().toString(16) + "\n" + "           Y: " + two.getY().toBigInteger().toString(16) + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        //Decryption

        ECPoint decrypted = edM.eData.subtract(edM.gInR.multiply(privKey));

        System.out.println("Decrypted: " + "X: " + decrypted.getX().toBigInteger().toString(16) + "\n" + "           Y: " + decrypted.getY().toBigInteger().toString(16) + "\n");

Upvotes: 1

x4k3p
x4k3p

Reputation: 1779

take a look on a diploma-work of a german man here
http://www.cdc.informatik.tu-darmstadt.de/reports/reports/crnjak.diplom.ps

Upvotes: 1

Related Questions