I have a private key in raw format, a byte array :
val privKeyIControlUInt8 = byteArrayOfInts(
0x00, 0x00, 0x00, 0x00, 0xB2, 0xC6, 0xFE, 0x9D,
0x1F, 0x87, 0x85, 0x8C, 0x00, 0x00, 0x00, 0x0A,
0x7D, 0x90, 0x8E, 0x1C, 0x11, 0x2D, 0x7B, 0xF9,
0x30, 0x8D, 0xF3, 0x8C, 0xD5, 0xC0, 0x41, 0xF0
)
I need to convert this array to a private key. I use this function :
private fun generatePrivateKey(encodedPrivateKey: ByteArray): PrivateKey {
Security.addProvider(BouncyCastleProvider())
val keyFactory = KeyFactory.getInstance("????")
return keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedPrivateKey))
}
I have no idea how to set the getInstance
. My private key is a P256 also known as secp256r1 and prime256v1.
Any idea ?
Thanks a lot in advance!
One way to derive the private key from the raw data is the following (based on this implementation):
The implementation uses pure Java classes, i. e. no BouncyCastle. With this, the private key can be derived from the raw data as follows:
which can be viewed in an ASN.1 parser, e.g. here.
I' ve tested this on Android P, API 28.
Using BouncyCastle a slightly more compact implementation is possible, see here.