Development

這是使用 Bouncy Castle 生成比特幣兼容密鑰的正確方法嗎?

  • September 17, 2018

我想使用 Bouncy Castle 生成用於所有 *coin 變體的密鑰對。

這是獲得 *coin 變體使用的正確 ECC 曲線的正確實現嗎?

   Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair GenerateKeys(int keySize)
   {
       var gen = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();
       var secureRandom = new Org.BouncyCastle.Security.SecureRandom();
       var keyGenParam = new Org.BouncyCastle.Crypto.KeyGenerationParameters(secureRandom, keySize);
       gen.Init(keyGenParam);

       return gen.GenerateKeyPair();
   }

下面的摘錄應該回答你的問題。

公共類 ECKeyPair 實現 Key
{
私有靜態最終 SecureRandom secureRandom = new SecureRandom ();
私有靜態最終 X9ECParameters 曲線 = SECNamedCurves.getByName ("secp256k1");
私有靜態最終ECDomainParameters域=新ECDomainParameters(curve.getCurve(),curve.getG(),curve.getN(),curve.getH());

私人 BigInteger 私人;
私人字節 [] 酒吧;
私有布爾壓縮;


公共靜態 ECKeyPair createNew(布爾壓縮)
{
ECKeyPairGenerator 生成器 = new ECKeyPairGenerator ();
ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters (domain, secureRandom);
generator.init (keygenParams);
AsymmetricCipherKeyPair keypair = generator.generateKeyPair ();
ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic();
ECKeyPair k = 新 ECKeyPair ();
k.priv = privParams.getD();
k.compressed = 壓縮;
如果(壓縮)
{
ECPoint q = pubParams.getQ();
k.pub = new ECPoint.Fp(domain.getCurve(), q.getX(), q.getY(), true).getEncoded();
}
別的
{
k.pub = pubParams.getQ().getEncoded();
}
返回 k;
}

}

查看<https://github.com/bitsofproof/supernode>了解更多詳情。

       var curve = ECNamedCurveTable.GetByName("secp256k1");
       var domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
       var secureRandom = new SecureRandom();
       var keyParams = new ECKeyGenerationParameters(domainParams, secureRandom);
       var generator = new ECKeyPairGenerator("ECDSA");
       generator.Init(keyParams);
       var keyPair = generator.GenerateKeyPair();
       ns.privateKey = keyPair.Private as ECPrivateKeyParameters;
       ns.publicKey = keyPair.Public as ECPublicKeyParameters;

引用自:https://bitcoin.stackexchange.com/questions/5861