Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ECDSA keypair (256bit) for bitcoin curve (secp256k1) using spongy castle?

Currently, I am 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 am not sure how I will pass the keySpecs?

 public void initialize(int keysize, SecureRandom random)
like image 266
ua741 Avatar asked Apr 21 '15 17:04

ua741


1 Answers

Your code is already sufficient, and specifying "secp256k1" already sets the correct size. The initialize(int, SecureRandom) method is an alternative to initialize(AlgorithmParameterSpec, SecureRandom); you call one or the other, not both. If you call the one specifying the keysize (say, 256), the BC provider will try to choose a default curve of the right size (for 256, it will be "prime256v1" a.k.a. "P-256" or "secp256r1").

like image 177
Peter Dettman Avatar answered Oct 08 '22 15:10

Peter Dettman