private void DoTestAgreement() { IAsymmetricCipherKeyPairGenerator kpGen = new X25519KeyPairGenerator(); kpGen.Init(new X25519KeyGenerationParameters(Random)); AsymmetricCipherKeyPair kpA = kpGen.GenerateKeyPair(); AsymmetricCipherKeyPair kpB = kpGen.GenerateKeyPair(); X25519Agreement agreeA = new X25519Agreement(); agreeA.Init(kpA.Private); byte[] secretA = new byte[agreeA.AgreementSize]; agreeA.CalculateAgreement(kpB.Public, secretA, 0); X25519Agreement agreeB = new X25519Agreement(); agreeB.Init(kpB.Private); byte[] secretB = new byte[agreeB.AgreementSize]; agreeB.CalculateAgreement(kpA.Public, secretB, 0); if (!AreEqual(secretA, secretB)) { Fail("X25519 agreement failed"); } }
/// <summary> /// Create a key pair for according to the key size. /// For X25519 the key size is not relevant, it always has a strength of 255 bits. /// </summary> /// <param name="keySize">the key size in bits</param> public void CreateKeyPair(int keySize = 255) { var parameters = new X25519KeyGenerationParameters(new SecureRandom()); var keyGenerator = new X25519KeyPairGenerator(); keyGenerator.Init(parameters); keyPair = keyGenerator.GenerateKeyPair(); }