Skip to content

any example for use it with NodeJs ?  #1

Open

Description

@bag7dad

c# with nodeJS key exchange

Activity

  1. bag7dad commented on Oct 5, 2020

    @bag7dad
    Author

    i use this example in c#

    class Program
    {
      static void Main(string[] args)
      {
          using (ECDiffieHellman alice = ECDiffieHellman.Create(ECCurve.NamedCurves.brainpoolP256r1))
          {
              var alicePublicKey = Convert.ToBase64String(alice.PublicKey.ToByteArray());
              //send alicePublicKey
              var nodejsKey = ""; //NODEJS brainpoolP256r1 publickey  base64
              byte[] nodejsKeyBytes= Convert.FromBase64String(nodejsKey);
    
              var aliceKey = Convert.ToBase64String(getDeriveKey(nodejsKeyBytes,alice));
              byte[] encryptedMessage = null;
              byte[] iv = null;
              // Send(aliceKey, "Secret message", out encryptedMessage, out iv);
          }
      }
      static byte[] getDeriveKey(byte[] key1, ECDiffieHellman alice)
      {
          byte[] keyX = new byte[key1.Length / 2];
          byte[] keyY = new byte[keyX.Length];
          Buffer.BlockCopy(key1, 1, keyX, 0, keyX.Length);
          Buffer.BlockCopy(key1, 1 + keyX.Length, keyY, 0, keyY.Length);
          ECParameters parameters = new ECParameters
          {
              Curve = ECCurve.NamedCurves.brainpoolP256r1,
              Q =
              {
                  X = keyX,
                  Y = keyY,
              },
          };
          byte[] derivedKey;
          using (ECDiffieHellman bob = ECDiffieHellman.Create(parameters))
          using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
          {
              return derivedKey = alice.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA256);
          }
      }
    }
    

    and the nodeJs

    `const crypto = require("crypto");
    const bob = crypto.createECDH('brainpoolP256r1')
    bob.generateKeys();
    const bobKey = bob.getPublicKey('base64');
    var bobLength = Buffer.from(bobKey, 'base64').length;
    //send bobkey to c#
    //recive alicekey
    
    var alicePublicKey = "RUNLUCAAAAB/xP7JhSIhYIYAijyC2zHu7obB5CwfK/ynQPxcRAIhBI6OLRRcHyPo61AhfSZN3qA2vGDfWO2mrdWWvqqhVaDf";
    var aliceKeyBuffer = Buffer.from(alicePublicKey, 'base64');
    var aliceKey = Buffer.alloc(bobLength)
    aliceKeyBuffer.copy(aliceKey, 1, 8);
    aliceKey[0] = 4;
    const hash = crypto.createHash('sha256');
    var tt = bob.computeSecret(aliceKey);
    var bobSecretKey = hash.update(tt).digest('base64');`
    
    

    but the c# code not work in Unity so i want to use your library for that can you provide an example

  2. mandelmonkey commented on Nov 26, 2022

    @mandelmonkey

    @medozeus did you ever figure this out? I guess the curve type needs to change to use what ever which seems to be some 25519 montgomery thing which nodejs doesnt support?

  3. gonsodany commented on Jul 16, 2024

    @gonsodany

    Did you ever figure out, im trying to use DiffieHellman for a unity - Python flask api scenario

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions