gpt4 book ai didi

java - 从 W 参数在主机端生成 ECDSA 公钥

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:56 26 4
gpt4 key购买 nike

我想将在我的小程序中生成的私钥-公钥对 (ECDSA) 的公钥发送到主机应用程序/终端。

在 RSA 中,通常我会发送模数和指数,并在主机端生成公钥。

在 ECDSA 中,我从链接中读到,如果您将 W 参数字节带出卡,我们可以做同样的事情 Click here: Stackoverflow Answer: encode public key on Java

我现在从卡上得到了 W 个字节。有人可以建议如何从中创建公钥吗?

最佳答案

我编写此方法将 EC 公钥转换为 java.security.interfaces.ECPublicKey key 对象。为此,我使用了 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;
}

关于java - 从 W 参数在主机端生成 ECDSA 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com