Android extract private and public key from JKS in assets

63 Views Asked by At

I have a JKS file in /src/main/res/raw/ecc.jks. The EC algorithm makes the JKS file with 256 lengths of key size. My issue is that I want to extract the private and public keys of the JKS file and return the new KeyPair. I used the below method but got java.io.IOException: Wrong version of key store.

public KeyPair easyLoadJKS(String alias, String pass) {
    try {
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keystore = KeyStore.getInstance(keyStoreType);
        InputStream in = _context.getResources().openRawResource(R.raw.ecc);
        keystore.load(in, pass.toCharArray());
        Key key = keystore.getKey(alias, pass.toCharArray());
        if (!(key instanceof PrivateKey))
            return null;
        Certificate cert = keystore.getCertificate(alias);
        PublicKey publicKey = cert.getPublicKey();
        return new KeyPair(publicKey, (PrivateKey) key);
    } catch (Throwable e) {
        System.out.println(e.getMessage());
        return null;
    }
}

The Keystore Explorer app show the below details:

Algorithem

Certificate Details

Private key details

public key details

0

There are 0 best solutions below