Java-Generated Private Key Imports in Chrome but Fails in Safari

I am working on a project where I generate an EC private key using Java and then import it in the browser using JavaScript. The key imports successfully in Chrome, but it fails in Safari.Here’s my Java code to generate the private key:

`

package com.example.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.StringWriter;

import java.security.*;

import java.security.spec.ECGenParameterSpec;

import java.util.Base64;

import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import org.bouncycastle.openssl.jcajce.JcaPEMWriter;

@SpringBootApplication

public class TestApplication {

private static final String CURVE = “secp256r1”;

public static void main(String[] args) {

    try {

        // Add BouncyCastle Provider

        Security.addProvider(new BouncyCastleProvider());



        // Generate EC key pair

        ECGenParameterSpec parameterSpec = new ECGenParameterSpec(CURVE);

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");

        keyPairGenerator.initialize(parameterSpec, new SecureRandom());

        KeyPair keyPair = keyPairGenerator.generateKeyPair();



        // Extract public and private keys

        PrivateKey privateKey = keyPair.getPrivate();

        PublicKey publicKey = keyPair.getPublic();



        // Encode keys to Base64

        String base64PrivateKey = Base64.getEncoder().encodeToString(privateKey.getEncoded());

        String base64PublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded());



        // Print keys

        System.out.println("Private Key (Base64): " + base64PrivateKey);

        System.out.println("Public Key (Base64): " + base64PublicKey);

    } catch (Exception e) {

        e.printStackTrace();

    }

}

}`

And here is the JavaScript code that is used to import the key in the browser:

`

async function _loadEccPrivateKey(key) {

try {

const rawKey = base64ToArrayBuffer(key);

console.log(rawKey);



const key = await window.crypto.subtle.importKey(

  "pkcs8", // Format for private keys

  rawKey,

  {

    name: "ECDH",

    namedCurve: "P-256",

  },

  true,

  ["deriveBits", "deriveKey"] // Key usages

);



console.log('Imported Private Key:', key);

return key;

} catch (e) {

console.error('Error importing private key:', e);

throw e;

}

}`

The code works perfectly in Chrome but throws an error in Safari. The error message is
“DATA PROVIDED TO AN OPERATION DOES NOT MEET REQUIREMENTS”

I want to make it work in safari