Page 1 of 1

Implementing Elliptic Curve Cryptography on javacard

Posted: Mon Jan 25, 2016 1:37 am
by choimillen
I would like to implement Elliptic curve cryptography on my java card? Does java card support Elliptic curve cryptography?
If yes, can someone provide me some sample code on how to encrypt and decrypt the data using the same mechanism.

Re: Implementing Elliptic Curve Cryptography on javacard

Posted: Tue Jan 26, 2016 3:10 am
by horse dream
Firstly, you should check whether your card supports EC cryptography.

ECC is rather dedicated to sign/verify operation on java card.
Here is part of sample code which maybe helpful to you.

Code: Select all

        byte[] dataToSend = new byte[64];
        KeyPair ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, KeyBuilder.LENGTH_EC_FP_128);
        ecKeyPair.genKeyPair();
        ECPrivateKey ecPrivateKey = (ECPrivateKey) ecKeyPair.getPrivate();
        ECPublicKey ecPublicKey = (ECPublicKey) ecKeyPair.getPublic();
   
        Signature sig = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);
        sig.init(ecPrivateKey, Signature.MODE_SIGN);
        short resLen = sig.sign(new byte[]{0,1,2,3}, (short)0, (short)4, dataToSend, (short)0);
   
        sig.init(ecPublicKey, Signature.MODE_VERIFY);
        if (!sig.verify(new byte[]{0,1,2,3}, (short)0, (short)4, dataToSend, (short)0, resLen))
            ISOException.throwIt(ISO7816.SW_WRONG_DATA);