Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

        return gen.GenerateKeyPair();
share|improve this question
IIRC, bitcoin uses an ECDSA key that is hashed using RIPEMD-160. For the particulars, you should look at the bitcoin wiki or vanitygen. – Nick ODell Dec 20 '12 at 1:53
@NickODell I ported Base58Check (RIPE160 + Sha256) to .NET / C# and that is passing all tests. All I need help with is the ECDSA curve. I think Bouncy is the only way... – LamonteCristo Dec 20 '12 at 1:59
I just checked, and you are correct about it doing sha256 first. Out of curiosity, what are you making that requires generating keys but can't use bitcoind? – Nick ODell Dec 20 '12 at 2:32
@NickODell I'm afraid I can't announce anything now. I'll have a timeline in Feb. – LamonteCristo Dec 20 '12 at 2:59

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 ();
   = new ECPoint.Fp (domain.getCurve (), q.getX (), q.getY (), true).getEncoded ();
   = pubParams.getQ ().getEncoded ();
        return k;


Check out for more details.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.