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

我怕爱的太早我们不能终老 提交于 2019-12-04 03:11:18

问题


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)

回答1:


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").




回答2:


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

Initializes the key pair generator for a certain keysize with the given source of randomness (and a default parameter set).

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



来源:https://stackoverflow.com/questions/29778852/how-to-create-ecdsa-keypair-256bit-for-bitcoin-curve-secp256k1-using-spongy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!