How to construct private key from generated previously ECDSA both encoded key pair?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 21:17:15

问题


Having generated the private key like this:

    fun getKeyPair(): Pair<ByteArray, ByteArray> {
        Security.addProvider(provider)
        val generator = KeyPairGenerator.getInstance("ECDSA")
        val ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
        generator.initialize(ecSpec)
        val keyPair = generator.generateKeyPair()
        val publicKey = keyPair.public as ECPublicKey
        val privateKey = keyPair.private
        return Pair(publicKey.q.getEncoded(true), privateKey.getEncoded())
    }

The public key can be reconstructed again like this:

    Security.addProvider(...spongy castle provider)
    val ecSpecs = ECNamedCurveTable.getParameterSpec("secp256r1")
    val q = ecSpecs.curve.decodePoint(publicKeyEncoded)
    val pubSpec = ECPublicKeySpec(q, ecSpecs)
    val keyFactory = KeyFactory.getInstance("ECDSA")
    val generatedPublic = keyFactory.generatePublic(pubSpec)

How it is possible to reconstruct private key from bytes also along with this?

UPDATE:

This code works well in actual app but it doesnt in JUnit testing:

val keyFactory = KeyFactory.getInstance("ECDSA")
val privSpec = PKCS8EncodedKeySpec(privateEncoded)
val generatedPrivate = keyFactory.generatePrivate(privSpec)

In JUnit test I am getting this error:

java.security.spec.InvalidKeySpecException: encoded key spec not recognised

My private key as encoded bytes has 150 bytes size.


回答1:


Since the key is encoded using the standard Key.getEncoded(), the following standard solution should work:

val keyFactory = KeyFactory.getInstance("EC")
val privSpec = PKCS8EncodedKeySpec(privateEncoded)
val generatedPrivate = keyFactory.generatePrivate(privSpec)

The encoded key should contain all the required information to rebuild the private key without specifying additional parameters like you need to do for the reduced public key.



来源:https://stackoverflow.com/questions/53764831/how-to-construct-private-key-from-generated-previously-ecdsa-both-encoded-key-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!