Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any example for use it with NodeJs ? #1

Open
bag7dad opened this issue Oct 5, 2020 · 3 comments
Open

any example for use it with NodeJs ? #1

bag7dad opened this issue Oct 5, 2020 · 3 comments

Comments

@bag7dad
Copy link

bag7dad commented Oct 5, 2020

c# with nodeJS key exchange

@bag7dad
Copy link
Author

bag7dad commented Oct 5, 2020

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

@mandelmonkey
Copy link

@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?

@gonsodany
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants