'Uninitialized Key Exception when using getK() in Elliptic Curve DSA on javacard

When trying to use the getK() method on a key pair in JavaCard I get this exception code: CryptoException.UNINITIALIZED_KEY

Here is my code where I generate the key pair:

KeyPair key = new KeyPair(KeyPair.ALG_EC_FP, (short)256);
key.genKeyPair();

later on in the code I am trying to run

ECPublicKey eCPublicKey = (ECPublicKey) key.getPublic();
short hLeng = eCPublicKey.getK();

but this is when I get the exception thrown. Is there something else I need to do to init the key?



Solution 1:[1]

You need to have initialised the domain parameters a, b, g, k, r and the field. You can find a list of NIST recommended curves here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf

getK() is throwing an exception because it has not been initialised.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LozCodes