/// <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); }