I am working with both C and Java implementations for ECDSA. C implementation is using raw formats - 32-byte private key, 64-byte public key, 64-byte signature.
I am creating keys in Java using Bouncy Castle:
public static KeyPair GenerateKeys() throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidAlgorithmParameterException {
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
return g.generateKeyPair();
}
and want to store them as byte arrays, as I need to export them to my C application later on. I noticed that:
KeyPair vendorKeys = GenerateKeys();
for (byte byt : vendorKeys.getPublic().getEncoded()) {
System.out.println("byt: " + byt);
}
will return bytes of Public key, but length is wrong. I guess it is related to format which is X.509
for PublicKey and PKCS#8
for private. Looking for help I found out that there are some classes in Java like PKCS8EncodedKeySpec
which should help me get raw key, but wasn't able to figure out how to do it.