Is this the proper way to use Bouncy Castle to generate a Bitcoin compatible key?

by random65537   Last Updated September 17, 2018 16:27 PM

I'd like to use Bouncy Castle to generate the keypair for use with all *coin variants.

Is this the correct implementation to get the correct ECC curve that the *coin variants use?

    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();
    }


Answers 2


Below extract should answer your question.

public class ECKeyPair implements Key
{
    private static final SecureRandom secureRandom = new SecureRandom ();
    private static final X9ECParameters curve = SECNamedCurves.getByName ("secp256k1");
    private static final ECDomainParameters domain = new ECDomainParameters (curve.getCurve (), curve.getG (), curve.getN (), curve.getH ());

    private BigInteger priv;
    private byte[] pub;
    private boolean compressed;


    public static ECKeyPair createNew (boolean compressed)
    {
        ECKeyPairGenerator generator = 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 = new ECKeyPair ();
        k.priv = privParams.getD ();
        k.compressed = compressed;
        if ( compressed )
        {
            ECPoint q = pubParams.getQ ();
            k.pub = new ECPoint.Fp (domain.getCurve (), q.getX (), q.getY (), true).getEncoded ();
        }
        else
        {
            k.pub = pubParams.getQ ().getEncoded ();
        }
        return k;
    }

}

Check out https://github.com/bitsofproof/supernode for more details.

Tamas Blummer
Tamas Blummer
June 11, 2013 20:32 PM

        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;
Oleg D
Oleg D
September 17, 2018 15:40 PM

Related Questions


Updated January 16, 2018 00:27 AM

Updated April 04, 2018 08:27 AM

Updated April 01, 2018 00:27 AM

Updated January 05, 2019 13:27 PM

Updated March 26, 2019 17:27 PM