I've been trying to convert the below pasted code to a public key. I'm trying to create a shared secret. I have the uncompressed HEX representation of the key. I want to create a public key from it. Likewise, I want the same for creating a private key and joining them after.
String plainPublicKey = "042E3E5CCF6B9AB04BE7A22F3FACCFDE73C87E87155394A34815408A896CA18A374DAC669AF3BF6220FC863767F4AF47507C5BC221FC4A19874DAF39B4074E3EB8";
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Hex.decodeHex(plainPublicKey.toCharArray()));
KeyFactory kf = KeyFactory.getInstance("EC");
PublicKey pub = kf.generatePublic(publicKeySpec);
return pub;
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.engineGeneratePublic(ECKeyFactory.java:157)
at java.base/java.security.KeyFactory.generatePublic(KeyFactory.java:352)
at AESExample.getPublicKey(AESExample.java:66)
at AESExample.main(AESExample.java:74)
Caused by: java.security.InvalidKeyException: invalid key format
at java.base/sun.security.x509.X509Key.decode(X509Key.java:386)
at java.base/sun.security.x509.X509Key.decode(X509Key.java:401)
at jdk.crypto.ec/sun.security.ec.ECPublicKeyImpl.<init>(ECPublicKeyImpl.java:71)
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.implGeneratePublic(ECKeyFactory.java:219)
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.engineGeneratePublic(ECKeyFactory.java:153)
... 3 more