Web3j ECKeyPair 到 KeyPair
Web3j ECKeyPair to KeyPair
我正在尝试使用 web3j 使用生成的密钥制作 ECC。我有 ECKeyPair object, but cipher.init()
requires 2nd parameter to be Key
object. ECKeyPair returns BigInteger
私钥和 public 密钥,如何将它们转换为 KeyPair
其中包含 PrivateKey
和 PublicKey
对象?
我试过了(参考:CryptoUtil.java):
private fun decodeKeyPair(ecKeyPair: ECKeyPair): KeyPair {
val xp = getNamedCurveByName("secp256k1")
val p = ECNamedCurveSpec("secp256k1", xp.curve, xp.g, xp.n, xp.h, null)
val curve = convertCurve(p.curve)
val g = EC5Util.convertPoint(curve, p.generator, false)
val n = p.order
val h = BigInteger.valueOf(p.cofactor.toLong())
val dp = ECDomainParameters(curve, g, n, h)
val bytes = Numeric.toBytesPadded(ecKeyPair.publicKey, 64)
val x = Numeric.toBigInt(Arrays.copyOfRange(bytes, 0, 32))
val y = Numeric.toBigInt(Arrays.copyOfRange(bytes, 32, 64))
val q = curve.createPoint(x, y)
val publicKey = BCECPublicKey(
"EC",
ECPublicKeyParameters(q, dp),
BouncyCastleProvider.CONFIGURATION
)
val privateKey = BCECPrivateKey(
"EC",
ECPrivateKeyParameters(ecKeyPair.privateKey, dp),
publicKey,
p,
BouncyCastleProvider.CONFIGURATION
)
return KeyPair(publicKey, privateKey)
}
但这 returns 是一个错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.bouncycastle.math.ec.ECCurve org.bouncycastle.jce.spec.ECParameterSpec.getCurve()' on a null object reference
是否有任何其他方法可以将 Web3j ECKeyPair 转换为 KeyPair
?
摸索了一番https://www.programcreek.com终于搞定了。这里的主要内容是 "how to generate ECPoint using the public key string".
这是我的做法,我没有转换整个 EcKeyPair
对象,而是单独转换键。
Public ECPublicKey
的关键字符串:
private fun toEcPublicKey(publicKey: String): ECPublicKey {
val params = ECNamedCurveTable.getParameterSpec("secp256k1")
val curveSpec = ECNamedCurveSpec("secp256k1", params.curve, params.g, params.n)
//This is the part how to generate ECPoint manually from public key string.
val pubKeyX = publicKey.substring(0, publicKey.length / 2)
val pubKeyY = publicKey.substring(publicKey.length / 2)
val ecPoint = ECPoint(BigInteger(pubKeyX, 16), BigInteger(pubKeyY, 16))
val params2 = EC5Util.convertSpec(curveSpec.curve, params)
val keySpec = java.security.spec.ECPublicKeySpec(ecPoint, params2)
val factory = KeyFactory.getInstance("ECDSA")
return factory.generatePublic(keySpec) as ECPublicKey
}
私钥字符串到 ECPrivateKey
:
private fun toEcPrivateKey(privateKey: String): ECPrivateKey {
val ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(privateKey))
val params = ECNamedCurveTable.getParameterSpec("secp256k1")
val curveSpec = ECNamedCurveSpec("secp256k1", params.curve, params.g, params.n)
val keySpec = java.security.spec.ECPrivateKeySpec(
ecKeyPair.privateKey,
curveSpec)
val factory = KeyFactory.getInstance("ECDSA")
return factory.generatePrivate(keySpec) as ECPrivateKey
}
字符串键输入是从 Web3j 库生成的。
我正在尝试使用 web3j 使用生成的密钥制作 ECC。我有 ECKeyPair object, but cipher.init()
requires 2nd parameter to be Key
object. ECKeyPair returns BigInteger
私钥和 public 密钥,如何将它们转换为 KeyPair
其中包含 PrivateKey
和 PublicKey
对象?
我试过了(参考:CryptoUtil.java):
private fun decodeKeyPair(ecKeyPair: ECKeyPair): KeyPair {
val xp = getNamedCurveByName("secp256k1")
val p = ECNamedCurveSpec("secp256k1", xp.curve, xp.g, xp.n, xp.h, null)
val curve = convertCurve(p.curve)
val g = EC5Util.convertPoint(curve, p.generator, false)
val n = p.order
val h = BigInteger.valueOf(p.cofactor.toLong())
val dp = ECDomainParameters(curve, g, n, h)
val bytes = Numeric.toBytesPadded(ecKeyPair.publicKey, 64)
val x = Numeric.toBigInt(Arrays.copyOfRange(bytes, 0, 32))
val y = Numeric.toBigInt(Arrays.copyOfRange(bytes, 32, 64))
val q = curve.createPoint(x, y)
val publicKey = BCECPublicKey(
"EC",
ECPublicKeyParameters(q, dp),
BouncyCastleProvider.CONFIGURATION
)
val privateKey = BCECPrivateKey(
"EC",
ECPrivateKeyParameters(ecKeyPair.privateKey, dp),
publicKey,
p,
BouncyCastleProvider.CONFIGURATION
)
return KeyPair(publicKey, privateKey)
}
但这 returns 是一个错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.bouncycastle.math.ec.ECCurve org.bouncycastle.jce.spec.ECParameterSpec.getCurve()' on a null object reference
是否有任何其他方法可以将 Web3j ECKeyPair 转换为 KeyPair
?
摸索了一番https://www.programcreek.com终于搞定了。这里的主要内容是 "how to generate ECPoint using the public key string".
这是我的做法,我没有转换整个 EcKeyPair
对象,而是单独转换键。
Public ECPublicKey
的关键字符串:
private fun toEcPublicKey(publicKey: String): ECPublicKey {
val params = ECNamedCurveTable.getParameterSpec("secp256k1")
val curveSpec = ECNamedCurveSpec("secp256k1", params.curve, params.g, params.n)
//This is the part how to generate ECPoint manually from public key string.
val pubKeyX = publicKey.substring(0, publicKey.length / 2)
val pubKeyY = publicKey.substring(publicKey.length / 2)
val ecPoint = ECPoint(BigInteger(pubKeyX, 16), BigInteger(pubKeyY, 16))
val params2 = EC5Util.convertSpec(curveSpec.curve, params)
val keySpec = java.security.spec.ECPublicKeySpec(ecPoint, params2)
val factory = KeyFactory.getInstance("ECDSA")
return factory.generatePublic(keySpec) as ECPublicKey
}
私钥字符串到 ECPrivateKey
:
private fun toEcPrivateKey(privateKey: String): ECPrivateKey {
val ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(privateKey))
val params = ECNamedCurveTable.getParameterSpec("secp256k1")
val curveSpec = ECNamedCurveSpec("secp256k1", params.curve, params.g, params.n)
val keySpec = java.security.spec.ECPrivateKeySpec(
ecKeyPair.privateKey,
curveSpec)
val factory = KeyFactory.getInstance("ECDSA")
return factory.generatePrivate(keySpec) as ECPrivateKey
}
字符串键输入是从 Web3j 库生成的。