How to create an ECDSA key pair (256 bit) for a bitcoin curve (secp256k1) using a sponge lock?

I am currently creating keyPair using this method

private KeyPair getKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDsA", "SC"); ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1"); keyGen.initialize(ecSpec, new SecureRandom()); return keyGen.generateKeyPair(); } 

KeyPairGenerator has another method in which I can specify keySize, but I'm not sure how I will pass keySpecs?

  public void initialize(int keysize, SecureRandom random) 
+7
source share
2 answers

Your code is already sufficient, and specifying "secp256k1" already sets the correct size. The initialize(int, SecureRandom) is an alternative to initialize(AlgorithmParameterSpec, SecureRandom) ; you name one or the other, not both. If you call the one that specifies the key (for example, 256), the BC provider will try to select the default curve for the correct size (for 256 it will be "prime256v1", like "P-256" or "secp256r1").

+5
source

The documentation for KeyPairGenerator says that initialize(int, SecureRandom) does this:

Initializes a key pair generator for a specific key with a given source of randomness (and a set of default parameters).

KeyPairGenerator is an abstract class, and I assume that this "default parameter set" is determined by the particular subclass that you are using. You can try to figure out which class for your KeyPairGenerator object really is, and then consult the documentation of that class to find out where you can set its default parameters.

0
source

All Articles