/// <summary> /// Generates a full key pair (Public and private) given the private key /// </summary> /// <param name="privateKey">The private key to generate public key from</param> /// <returns>A full key pair</returns> public static X25519KeyPair GenerateKeyFromPrivateKey(byte[] privateKey) { X25519KeyPair key = new X25519KeyPair { PrivateKey = privateKey }; key.PublicKey = Curve25519.ScalarMultiplication(key.PrivateKey, Curve25519.Basepoint); return(key); }
/// <summary> /// Uses RNG Random to generate a key pair /// </summary> /// <returns>A random key pair</returns> public static X25519KeyPair GenerateKeyPair() { // at first generate the private key X25519KeyPair key = new X25519KeyPair { PrivateKey = new byte[32] }; using (var rnd = new RNGCryptoServiceProvider()) rnd.GetBytes(key.PrivateKey); // as defined in https://cr.yp.to/ecdh.html do these operation to finalize the private key key.PrivateKey[0] &= 248; key.PrivateKey[31] &= 127; key.PrivateKey[31] |= 64; // compute the public key key.PublicKey = Curve25519.ScalarMultiplication(key.PrivateKey, Curve25519.Basepoint); return(key); }
/// <summary> /// Generate a shared secret with the other users public key and your private key /// </summary> /// <param name="myPrivateKey">Your private key</param> /// <param name="otherPublicKey">The public key of the other user</param> /// <returns>A shared secret</returns> public static byte[] Agreement(byte[] myPrivateKey, byte[] otherPublicKey) { return(Curve25519.ScalarMultiplication(myPrivateKey, otherPublicKey)); }