I need to generate such public key and do the additional signing of the bytes (which will include this generated previously key)
I need to construct bytes of: ASN.1 prefix + signature of (33-byte compressed NIST P-256 public key)
The signature should be delivered from other defined private key
The ECDSA specifications:
● Curve:
NIST P-256 Otherwise known as secp256r1 and prime256v1 (openssl)
● Signature format ASN.1. The r and s values of the ECDSA signature must be positive integers, and DER-encoded as such.
Is there API in Android to do such process? How can I use it then?
WHAT I've tried:
try {
val generator = KeyPairGenerator.getInstance("ECDSA")
val ecSpec = ECNamedCurveTable
.getParameterSpec("prime256v1")
generator.initialize(ecSpec)
val keyPair = generator.generateKeyPair()
val privKey = keyPair.private
val encodedPrivKey = privKey.encoded
System.out.println(toHex(encodedPrivKey))
val pubKey = keyPair.public
val encodedPubKey = pubKey.encoded
System.out.println(toHex(encodedPubKey))
val keyFactory = KeyFactory.getInstance("ECDSA")
val pubKey2 = keyFactory.generatePublic(X509EncodedKeySpec(encodedPubKey))
if (Arrays.equals(pubKey2.getEncoded(), encodedPubKey)) {
println("That worked for the public key")
}
val privKey2 = keyFactory.generatePrivate(PKCS8EncodedKeySpec(encodedPrivKey))
if (Arrays.equals(privKey2.getEncoded(), encodedPrivKey)) {
println("That worked for the private key")
}
} catch (e: GeneralSecurityException) {
throw IllegalStateException(e)
}
Here - the encoded public key has the lenght of 90 bytes which i guess i want it to be 33 bytes