0

When I generate public key by using ECDH_KeyGeneration.getPublicKey() in robovm library. it throws an exception - NoSuchMethodException. This problem occurred in Android Q only . till android pie it is working fine.

   X9ECParameters ecp = SECNamedCurves.getByName("secp256r1");
    ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(),
            ecp.getG(), ecp.getN(), ecp.getH(),
            ecp.getSeed());

    // Generate a private key and a public key
    AsymmetricCipherKeyPair keyPair;
    ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keyGenParams);
    keyPair = generator.generateKeyPair();
    ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();

    ECPublicKeyParameters publicKey = ((ECPublicKeyParameters) keyPair.getPublic());
    mPrivateKeyBytes2 = privateKey.getD().toByteArray();
    String str = Hex.toHexString(publicKey.getQ().getEncoded(false));

Lcom/android/org/bouncycastle/asn1/sec/SECNamedCurves; or its super classes (declaration of 'com.android.org.bouncycastle.asn1.sec.SECNamedCurves' appears in /apex/com.android.runtime/javalib/bouncycastle.jar)
        at projects.athansys.com.athandoctorassist.helper.ECDH_KeyGeneration.getPublicKey(ECDH_KeyGeneration.java:25)

1 Answers1

0

The following code will help you, you can generate algorithm using bouncy castle library:

private static ECDsa GetEllipticCurveAlgorithm(string privateKey)
{
    var keyParams = (ECPrivateKeyParameters)PrivateKeyFactory
        .CreateKey(Convert.FromBase64String(privateKey));

    var normalizedECPoint = keyParams.Parameters.G.Multiply(keyParams.D).Normalize();

    return ECDsa.Create(new ECParameters
    {
        Curve = ECCurve.CreateFromValue(keyParams.PublicKeyParamSet.Id),
        D = keyParams.D.ToByteArrayUnsigned(),
        Q =
    {
        X = normalizedECPoint.XCoord.GetEncoded(),
        Y = normalizedECPoint.YCoord.GetEncoded()
    }
    });
}

and generate the token in the following way:

var signatureAlgorithm = GetEllipticCurveAlgorithm(privateKey);

        ECDsaSecurityKey eCDsaSecurityKey = new ECDsaSecurityKey(signatureAlgorithm)
        {
            KeyId = settings.Apple.KeyId
        };

        var handler = new JwtSecurityTokenHandler();   
        var token = handler.CreateJwtSecurityToken(
            issuer: iss,
            audience: AUD,
            subject: new ClaimsIdentity(new List<Claim> { new Claim("sub", sub) }),
            expires: DateTime.UtcNow.AddMinutes(5), 
            issuedAt: DateTime.UtcNow,
            notBefore: DateTime.UtcNow,
            signingCredentials: new SigningCredentials(eCDsaSecurityKey, SecurityAlgorithms.EcdsaSha256));
Shah Zain
  • 388
  • 4
  • 10