作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图从私钥生成 ECDSA 公钥,但我未能在互联网上找到太多关于如何执行此操作的帮助。几乎所有的东西都是为了从公钥规范生成公钥,我不知道如何得到它。到目前为止,这是我放在一起的:
public void setPublic() throws GeneralSecurityException {
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
ECCurve curve = params.getCurve();
java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
java.security.spec.ECPoint point = ECPointUtil.decodePoint(ellipticCurve, this.privateKey.getEncoded());
java.security.spec.ECParameterSpec params2=EC5Util.convertSpec(ellipticCurve, params);
java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2);
this.publicKey = fact.generatePublic(keySpec);
}
但是,运行时出现如下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid point encoding 0x30
at org.bouncycastle.math.ec.ECCurve.decodePoint(Unknown Source)
at org.bouncycastle.jce.ECPointUtil.decodePoint(Unknown Source)
at Wallet.Wallet.setPublic(Wallet.java:125)
我做错了什么?有没有更好/更简单的方法来做到这一点?
编辑:我设法编译了一些代码,但它不能正常工作:
public void setPublic() throws GeneralSecurityException {
BigInteger privKey = new BigInteger(getHex(privateKey.getEncoded()),16);
X9ECParameters ecp = SECNamedCurves.getByName("secp256k1");
ECPoint curvePt = ecp.getG().multiply(privKey);
BigInteger x = curvePt.getX().toBigInteger();
BigInteger y = curvePt.getY().toBigInteger();
byte[] xBytes = removeSignByte(x.toByteArray());
byte[] yBytes = removeSignByte(y.toByteArray());
byte[] pubKeyBytes = new byte[65];
pubKeyBytes[0] = new Byte("04");
System.arraycopy(xBytes, 0, pubKeyBytes, 1, xBytes.length);
System.arraycopy(yBytes, 0, pubKeyBytes, 33, xBytes.length);
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
ECCurve curve = params.getCurve();
java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
java.security.spec.ECPoint point = ECPointUtil.decodePoint(ellipticCurve, pubKeyBytes);
java.security.spec.ECParameterSpec params2 = EC5Util.convertSpec(ellipticCurve, params);
java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2);
this.publicKey = fact.generatePublic(keySpec);
}
private byte[] removeSignByte(byte[] arr)
{
if(arr.length==33)
{
byte[] newArr = new byte[32];
System.arraycopy(arr, 1, newArr, 0, newArr.length);
return newArr;
}
return arr;
}
当我运行它时,它会生成一个publicKey,但它与私钥对应的不是同一个。
最佳答案
所以过了一段时间,我想出了一个解决方案并决定发布它以防其他人遇到与我相同的问题:
KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
ECPoint Q = ecSpec.getG().multiply(((org.bouncycastle.jce.interfaces.ECPrivateKey) this.privateKey).getD());
ECPublicKeySpec pubSpec = new ECPublicKeySpec(Q, ecSpec);
PublicKey publicKeyGenerated = keyFactory.generatePublic(pubSpec);
this.publicKey = publicKeyGenerated;
编辑:根据@MaartenBodewes 评论删除了解码 ECPoint 的代码。
关于java - 从私钥导出 ECDSA 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204787/
我是一名优秀的程序员,十分优秀!