Skip to content

Instantly share code, notes, and snippets.

@glides
Forked from tmarkovski/Program.cs
Created January 26, 2021 18:35
Show Gist options
  • Select an option

  • Save glides/1a74bdbcd3c3df03246b793fe97f1735 to your computer and use it in GitHub Desktop.

Select an option

Save glides/1a74bdbcd3c3df03246b793fe97f1735 to your computer and use it in GitHub Desktop.

Revisions

  1. @tmarkovski tmarkovski revised this gist Feb 26, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Program.cs
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ static AsymmetricCipherKeyPair GenerateKeyPair()
    var publicKey = keyPair.Public as ECPublicKeyParameters;

    Console.WriteLine($"Private key: {ToHex(privateKey.D.ToByteArrayUnsigned())}");
    Console.WriteLine($"Private key: {ToHex(publicKey.Q.GetEncoded())}");
    Console.WriteLine($"Public key: {ToHex(publicKey.Q.GetEncoded())}");

    return keyPair;
    }
  2. @tmarkovski tmarkovski created this gist Feb 26, 2018.
    41 changes: 41 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    using System;
    using System.Linq;
    using Org.BouncyCastle.Asn1.X9;
    using Org.BouncyCastle.Crypto;
    using Org.BouncyCastle.Crypto.Generators;
    using Org.BouncyCastle.Crypto.Parameters;
    using Org.BouncyCastle.Security;

    namespace Program
    {
    class Program
    {
    static void Main(string[] args)
    {
    GenerateKeyPair();
    }

    static AsymmetricCipherKeyPair GenerateKeyPair()
    {
    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();

    var privateKey = keyPair.Private as ECPrivateKeyParameters;
    var publicKey = keyPair.Public as ECPublicKeyParameters;

    Console.WriteLine($"Private key: {ToHex(privateKey.D.ToByteArrayUnsigned())}");
    Console.WriteLine($"Private key: {ToHex(publicKey.Q.GetEncoded())}");

    return keyPair;
    }

    static string ToHex(byte[] data) => String.Concat(data.Select(x => x.ToString("x2")));
    }
    }