Error "java.security.spec.InvalidKeySpecException: Error parsing public key" with Java

32 views Asked by At

I have to encrypt some data in my project. For this I'm using ECDH and AES-128-CBC. The problem is: I got the key from the other side. I have to create a public key from the mentioned key:

public PublicKey createPublicKey(byte[] key) {
   X509EncodedKeySpec x509key = new X509EncodedKeySpec(key);
   KeyFactory keyfactory = KeyFactory.getInstance("EC");
   return keyfactory.generatePublic(x509key);
}

But the code above returns an error: java.security.spec.InvalidKeySpecException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: Error parsing public key

What am I doing wrong? Do the key need to have specified length? For now it's 32 bytes.

0

There are 0 answers