0

I use android keystore to generate EC public key. But the size of public key byteArray is 91.

When I use bouncyCastle to generate EC public key, the size is 65.

I want to get EC public key with 65 size byteArray using Android keystore. Any help?

This is the code with bouncyCastle.

val ecSpec: ECParameterSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
val g = KeyPairGenerator.getInstance("ECDH", "BC")
g.initialize(ecSpec, SecureRandom())
val keyPair = g.generateKeyPair()
val publicKey = (keyPair.public as ECPublicKey).q.getEncoded(false)
public.size // 65

And this is the code with Android keystore.

val keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC,"AndroidKeyStore")
keyPairGenerator.initialize(
    KeyGenParameterSpec.Builder(
        "key0",
        KeyProperties.PURPOSE_SIGN)
        .setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
        .setUserAuthenticationRequired(true)
        .setKeySize(256)
        .build())
val keyPair = keyPairGenerator.generateKeyPair()
val publicKey = keyPair.public.encoded
public.size // 91 (I need 65 here)

0 Answers0