Page 1 of 1

reconstruct an ECPublicKey from JavaCard to Java

Posted: Tue Jun 23, 2015 6:06 am
by Peterson
I need to implement ECDH between a terminal side and a java card side.

Code: Select all

 ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
            KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0100, false);
        pubKey.setFieldFP(p, (short) 0x0001, (short) 0x0020);
        pubKey.setA(a, (short) 0x0001, (short) 0x0020);
        pubKey.setB(b, (short) 0x0000, (short) 0x0020);
        pubKey.setR(r, (short) 0x0001, (short) 0x0020);
        pubKey.setG(g, (short) 0x0000, (short) g.length);

        ECPrivateKey privKey = (ECPrivateKey) KeyBuilder.buildKey(
            KeyBuilder.TYPE_EC_FP_PRIVATE, (short) 0x0100, false);

        KeyPair keypair = new KeyPair(pubKey, privKey);
        keypair.genKeyPair();

        pubKey.getW(apduBuffer, (short) 0x0000);
        setOutgoingAndSend((short) 0x0000, (short) 0x0041);

On the card side, the code above is performed .
I generate a KeyPair for ECDH,and send the the public one to the terminal side.

But i can't use java to reconstruct an ECPublicKey provided the response APDU I get.
What can i do? Could anyone give me any clues?Thanks a lot.

Re: reconstruct an ECPublicKey from JavaCard to Java

Posted: Tue Jun 23, 2015 6:16 am
by horse dream
The ECPublicKey 's format returned in JavaCard is as follows: 04 x y. On the terminal side, you should extract x and y coordinates first.

Code: Select all

KeyFactory kf = KeyFactory.getInstance("ECDSA", "BC");
ECPoint point = new ECPoint(x, y);
ECParameterSpec domainparameters = new ECParameterSpec(...); // initialize your domain parameters
ECPublicKeySpec spec = new ECPublicKeySpec(point, domainparameters);
ECPublicKey publickey = (ECPublicKey)kf.generatePublic(spec);