Java – Android: Generate elliptic curve KeypPair

Android: Generate elliptic curve KeypPair… here is a solution to the problem.

Android: Generate elliptic curve KeypPair

I’m trying to implement key generation based on elliptic curve Diffie Hellman. Now I want to specify my elliptic curve myself, so I took the parameter from here

The code I wrote to achieve this :

public void createKey(){

 base point (generator???)
        BigInteger x = new BigInteger("2fe13c0537bbc11acaa07d793de4e6d5e5c94eee8", 16);
        BigInteger y = new BigInteger("289070fb05d38ff58321f2e800536d538ccdaa3d9", 16);

 the order of generator
        BigInteger n = new BigInteger("5846006549323611672814741753598448348329118574063", 10);

 curves coefficients
        BigInteger ab = new BigInteger("1", 2);

 curves cofactor
        BigInteger h = new BigInteger("2", 10);

 exponents of the equotation
        int[] ks = {7, 6, 3};

ECFieldF2m ecField = new ECFieldF2m(163, ks);
         Elliptic curve
        EllipticCurve ec = new EllipticCurve(ecField, ab, ab);

 GENERATOR POINT
        ECPoint g = new ECPoint(x, y);

 Parameter specs?
        ECParameterSpec ecps = new ECParameterSpec(ec , g , n, h.intValue());

try {
         get keypair
        KeyPairGenerator kg = KeyPairGenerator.getInstance("ECDSA");
        kg.initialize(ecps, new SecureRandom());
        KeyPair kp = kg.generateKeyPair();
        Log.d("SECLIENT"+type,kp.getPublic().toString());
        Log.d("SECLIENT"+type,kp.getPrivate().toString());
    }catch (Exception e){
        e.printStackTrace();
    }
}

At this point, the code compiles through. However, it fails in a try_catch block that must generate a key pair.

The error message is:

java.lang.RuntimeException: Unable to create EC KeyFactory: Unprocessed field class java.security.spec.ECFieldF2m

Has anyone encountered this issue? How do I fix this?

Solution

I’m not having the exact same issue as you’re having, but since you’re using EC in Android and you’re having a weird RuntimeException, I’m going to leave you my 2 cents. Hope this helps.

I have another weird error (OEM related).

java.lang.RuntimeException: error:0f06707b:elliptic curve routines:EC_GROUP_new_by_curve_name:UNKNOWN_GROUP

Try to use

KeyAgreement.getInstance("ECDH);

What happened to me was that I added SpongyCaSTLe as a SecurityProvider, doing this:

Security.addProvider(new BouncyCastleProvider());

For some devices everything works as expected, but in others (i.e. LGE Nexus 5) I get the exact same exception.

For me the solution to it was to change the way I add SpongyCaSTLe and do it:

Security.insertProviderAt(new BouncyCastleProvider(), 1);

Related Problems and Solutions