Advertisement
Guest User

Using Bouncy Castle in C# for ECDH

a guest
Mar 21st, 2018
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Text;
  5.  
  6. using Org.BouncyCastle.Asn1.Sec;
  7. using Org.BouncyCastle.Asn1.X9;
  8. using Org.BouncyCastle.Crypto.Parameters;
  9. using Org.BouncyCastle.Math;
  10. using Org.BouncyCastle.Math.EC;
  11.  
  12. using Org.BouncyCastle.Crypto.Agreement;
  13.  
  14. namespace BouncyCastleECDH
  15. {
  16.     class Program
  17.     {
  18.         public static readonly X9ECParameters _ecparameters = SecNamedCurves.GetByName("secp256k1");
  19.         public static readonly ECDomainParameters _domain = new ECDomainParameters(_ecparameters.Curve, _ecparameters.G, _ecparameters.N, _ecparameters.H);
  20.  
  21.         public static string ByteArrayToString(byte[] ba)
  22.         {
  23.             StringBuilder hex = new StringBuilder(ba.Length * 2);
  24.             foreach (byte b in ba)
  25.                 hex.AppendFormat("{0:x2}", b);
  26.             return hex.ToString();
  27.         }
  28.  
  29.         public static byte[] ECDH(BigInteger privKeyInt, byte[] publicKey)
  30.         {
  31.             ECPrivateKeyParameters ecPrivateKey = new ECPrivateKeyParameters(privKeyInt, _domain);
  32.             ECPoint publicPoint = _ecparameters.Curve.DecodePoint(publicKey);
  33.             ECPublicKeyParameters ecPublicKey = new ECPublicKeyParameters(publicPoint, _domain);
  34.  
  35.             // ECDHCBasicAgreement basicAgreement = new ECDHCBasicAgreement();
  36.             ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement();
  37.             basicAgreement.Init(ecPrivateKey);
  38.             BigInteger symKey = basicAgreement.CalculateAgreement(ecPublicKey);
  39.  
  40.             return symKey.ToByteArray();
  41.         }
  42.  
  43.         public static byte[] GetPublicKey(BigInteger privKeyInt)
  44.         {
  45.             return GetPublicKey(_ecparameters.G.Multiply(privKeyInt));
  46.         }
  47.  
  48.         public static byte[] GetPublicKey(ECPoint p)
  49.         {
  50.             var publicParams = new ECPublicKeyParameters(p, _domain);
  51.             return publicParams.Q.GetEncoded();
  52.         }
  53.  
  54.         static void Main()
  55.         {
  56.             string privkey1 = "82fc9947e878fc7ed01c6c310688603f0a41c8e8704e5b990e8388343b0fd465";
  57.             string privkey2 = "5f706787ac72c1080275c1f398640fb07e9da0b124ae9734b28b8d0f01eda586";
  58.             BigInteger privkey1Int = new BigInteger(privkey1,16);
  59.             BigInteger privkey2Int = new BigInteger(privkey2,16);
  60.             var pubkey1 = GetPublicKey(privkey1Int);
  61.             var pubkey2 = GetPublicKey(privkey2Int);
  62.  
  63.             Console.WriteLine($"pub1:{ByteArrayToString(pubkey1)}");
  64.             Console.WriteLine($"pub2:{ByteArrayToString(pubkey2)}");
  65.             Console.WriteLine($"ecdh:{ByteArrayToString(ECDH(privkey1Int,pubkey2))}");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement