从JavaCard恢复ECPublicKey到Java [英] Recovering an ECPublicKey from JavaCard to Java
问题描述
我正在尝试在终端(由计算机模拟)和智能卡(Java卡)之间实现ECDH。
I am trying to implement ECDH between a terminal (simulated by my computer) and a smart card (Java Card).
我固定了想要的椭圆曲线使用,在卡侧,我有以下代码可以运行协议的第一部分:
I fixed the elliptic curve that I want to use, and on the card side I have the following code to run the first part of the protocol :
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);
所以我为ECDH创建了一个密钥对,然后将公共密钥对发送到我的终端。
So I create a KeyPair for ECDH and I send the public one to my terminal.
我的问题如下:考虑到我得到的响应APDU,我无法重构ECPublicKey ...
My problem is the following : I am not able to reconstruct an ECPublicKey given the response APDU I get...
I没有找到任何用Java进行此操作的方法(甚至使用外部库作为Bouncy Castle)。
I did not find any way to do this in Java (even using external library as Bouncy Castle).
有人可以帮我吗?
推荐答案
JavaCard返回的公共密钥的格式如下: 04 xy
。在终端端,首先必须提取x和y坐标。然后,
The public key returned in JavaCard is formatted as follows: 04 x y
. In the terminal side, first you must extract x and y coordinates. Then,
KeyFactory kf = KeyFactory.getInstance( ECDSA, BC);
KeyFactory kf = KeyFactory.getInstance("ECDSA", "BC");
ECPoint point = new ECPoint(x,y);
ECPoint point = new ECPoint(x, y);
ECParameterSpec域参数= new ECParameterSpec(...); //初始化域参数
ECParameterSpec domainparameters = new ECParameterSpec(...); // initialize your domain parameters
ECPublicKeySpec spec = new ECPublicKeySpec(point,domainparameters);
ECPublicKeySpec spec = new ECPublicKeySpec(point, domainparameters);
ECPublicKey publickey =(ECPublicKey)kf.generatePublic(spec );
ECPublicKey publickey = (ECPublicKey)kf.generatePublic(spec);
这篇关于从JavaCard恢复ECPublicKey到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!