我想将我的applet中生成的私钥 - 公钥对(ECDSA)的公钥发送到主机应用程序/终端。
在RSA中,我会发送模数和指数,并在主机端生成公钥。
在ECDSA中,我从链接中读取了如果你在卡片Click here: Stackoverflow Answer: encode public key on Java外面取W参数字节我们可以做同样的事情
我现在有来自卡片的W字节。有人可以建议如何从这个创建公钥吗?
答案 0 :(得分:2)
我写了这个方法将EC公钥转换为java.security.interfaces.ECPublicKey
密钥对象。为此,我使用了Bouncy Castle提供程序(bcprov-ext-jdk16-1.46.jar
)。您可以从here下载最新版本。
/**
* This method converts the EC public key (ECPublicKey#getW()) into ECPublicKey
* @param cardPublicKey as W
* @param curveName (for example "P-224")
* @return java.security.interfaces.ECPublicKey
*/
public ECPublicKey ucPublicKeyToPublicKey(byte[] cardPublicKey, String curveName) {
//for example curveName = "P-224";
java.security.interfaces.ECPublicKey ecPublicKey = null; // java.security.interfaces.ECPublicKey
java.security.KeyFactory kf = null;
org.bouncycastle.jce.spec.ECNamedCurveParameterSpec ecNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec(curveName);
org.bouncycastle.math.ec.ECCurve curve = ecNamedCurveParameterSpec.getCurve();
java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, ecNamedCurveParameterSpec.getSeed());
java.security.spec.ECPoint ecPoint = ECPointUtil.decodePoint(ellipticCurve, cardPublicKey);
java.security.spec.ECParameterSpec ecParameterSpec = EC5Util.convertSpec(ellipticCurve, ecNamedCurveParameterSpec);
java.security.spec.ECPublicKeySpec publicKeySpec = new java.security.spec.ECPublicKeySpec(ecPoint, ecParameterSpec);
try {
kf = java.security.KeyFactory.getInstance("EC", "BC");
} catch (Exception e) {
System.out.println("Caught Exception kf : " + e.toString());
}
try {
ecPublicKey = (ECPublicKey) kf.generatePublic(publicKeySpec);
} catch (Exception e) {
System.out.println("Caught Exception public key: " + e.toString());
}
return ecPublicKey;
}