我试图从私钥生成一个公共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,但它与私钥对应的不一样。
答案 0 :(得分:3)
所以过了一会儿,我想出了一个解决方案并决定发布它以防万一其他人和我有同样的问题:
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的代码。
答案 1 :(得分:2)
在前一个答案的基础上,可以将此扩展到所有给出的是满足BCECPrivateKey接口的私钥的情况:
DerivePubKeyFromPrivKey(BCECPrivateKey definingKey, Provider provider) {
KeyFactory keyFactory = KeyFactory.getInstance("EC", provider);
BigInteger d = definingKey.getD();
org.bouncycastle.jce.spec.ECParameterSpec ecSpec =
definingKey.getParameters();
ECPoint Q = definingKey.getParameters().getG().multiply(d);
org.bouncycastle.jce.spec.ECPublicKeySpec pubSpec = new
org.bouncycastle.jce.spec.ECPublicKeySpec(Q, ecSpec);
PublicKey publicKeyGenerated = keyFactory.generatePublic(pubSpec);
return publicKeyGenerated;
}
答案 2 :(得分:0)
我在Kotlin遇到了同样的问题,因此,如果对任何人都有用,这就是我从Kotlin的ECDSA PublicKey
派生PrivateKey
的方式。这段代码基于Lev Knoblock's Java answer,然后反复试验直到起作用。
就我而言,我知道我的钥匙使用了secp256k1
曲线,因此可以对该部分进行硬编码。我没有费心学习如何将其推广到其他曲线。
import java.security.KeyFactory
import java.security.KeyPair
import java.security.PrivateKey
import java.security.PublicKey
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo
import org.bouncycastle.jce.ECNamedCurveTable
import org.bouncycastle.jce.interfaces.ECPrivateKey
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec
import org.bouncycastle.jce.spec.ECPublicKeySpec
import org.bouncycastle.math.ec.ECPoint
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
import org.bouncycastle.openssl.PEMParser
fun getECPrivateKeyFromPEM(privatePem: String): ECPrivateKey {
val pemParser = PEMParser(privatePem.reader())
val privateKeyInfo = pemParser.readObject() as PrivateKeyInfo
return JcaPEMKeyConverter().getPrivateKey(privateKeyInfo) as ECPrivateKey
}
fun getKeyPairFromECPrivateKey(privateKey: ECPrivateKey): KeyPair {
val keyFactory: KeyFactory = KeyFactory.getInstance("ECDSA", "BC")
val spec: ECNamedCurveParameterSpec = ECNamedCurveTable.getParameterSpec("secp256k1")
val Q: ECPoint = spec.getG().multiply(privateKey.getD())
val publicKey: PublicKey = keyFactory.generatePublic(ECPublicKeySpec(Q, spec))
return KeyPair(publicKey, privateKey)
}
这是我的测试工具:
import java.io.StringWriter
import org.bouncycastle.openssl.jcajce.JcaPEMWriter
fun main() {
val privatePem = """
|-----BEGIN PRIVATE KEY-----
|MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg55EMdhNJX+YN/bjN
|Eof91oKqEqD0QidEsRMhHBwSRjShRANCAARnSFpE0LDugORBWlSJz0Zf9e0mR9s6
|tlxSeo1Nbd2vv9LDedm+l/CfZpbyYvPm49DAKDhkUHFIVDd2SsiPrRa7
|-----END PRIVATE KEY-----
""".trimMargin()
val privateKey: ECPrivateKey = getECPrivateKeyFromPEM(privatePem)
val pair: KeyPair = getKeyPairFromECPrivateKey(privateKey)
val pems: String = StringWriter().use {
val pemWriter = JcaPEMWriter(it)
pemWriter.writeObject(pair.getPublic())
pemWriter.writeObject(pair.getPrivate())
pemWriter.flush()
it.toString()
}
println(pems)
}
打印输出:
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEcE4HMAHLDvPr6xHKsjhPXJzTdxLlRRR8
BfYnI2TGb0QLTFyyXm13CeYiLnxbkGhSvz9ZRo0zGQygKPVpgiThSw==
-----END PUBLIC KEY-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIOeRDHYTSV/mDf24zRKH/daCqhKg9EInRLETIRwcEkY0oAoGCCqGSM49
AwEHoUQDQgAEZ0haRNCw7oDkQVpUic9GX/XtJkfbOrZcUnqNTW3dr7/Sw3nZvpfw
n2aW8mLz5uPQwCg4ZFBxSFQ3dkrIj60Wuw==
-----END EC PRIVATE KEY-----