问题描述
当前正在使用以下方法创建密钥
Currently when I am creating key using following 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有另一种方法,其中我可以指定keySize,但我不确定我将如何通过ecSpec。
KeyPairGenerator has another method, in which I can specify keySize but I am not sure how I will pass ecSpec then.
public void initialize(int keysize, SecureRandom random)
推荐答案
您的代码已经足够了,并指定secp256k1已经设置正确的大小。 initialize(int,SecureRandom)
方法是一个替代到 initialize(AlgorithmParameterSpec,SecureRandom)
;你呼叫一个或另一个,而不是两者。如果您调用指定keysize(例如256)的那个,BC提供商将尝试选择正确大小的默认曲线(对于256,它将是prime256v1,又称P-256或secp256r1)。
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").
这篇关于如何使用海绵城堡为比特币曲线(secp256k1)创建ECDSA密钥对(256位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!