X25519 in C#
A simple library based on Golang's Curve25519 library.
- Easy to use front end
- Completely re written the Golang's crypto library in C#
- Nearly fast (about 3000 Keys and Agreements/s)
- Universal library with .Net Standard (You can also use this library anywhere in .Net (Read more below)
Link to library on Nuget
var alice = X25519KeyAgreement.GenerateKeyPair();
In this example, alice is
X25519KeyPair struct that contains public key and private key as 32 byte length byte arrays.
// generate keys var alice = X25519KeyAgreement.GenerateKeyPair(); var bob = X25519KeyAgreement.GenerateKeyPair(); // generate shared secret var shared1 = X25519KeyAgreement.Agreement(alice.PrivateKey, bob.PublicKey); var shared2 = X25519KeyAgreement.Agreement(bob.PrivateKey, alice.PublicKey); // now the shared1 and shared2 must be same
Use in older versions of .Net
This library is built with .Net Standard 2; Means that minimum frameworks are .Net Core 2 and .Net framework 4.6.1. Full list. You might want to use this library in .Net 2. It's easy to make this library in earlier version of .Net
Note: Due to usage of RNGCryptoServiceProvider this library is not compatible with .Net Core 1.X.
Here are steps to include this library in your project:
- At first copy
X25519.csfrom here into your project.
- If you are using .Net Framework 4 or lower, you will get an error because IReadonlyList is available since .Net framework 4.5. To fix this problem, just change
byte. This should fix the problem.
- Use the library
These benchmarks are done with .Net Core 3.1 on Intel-i7 4790K 4.0GHz.
Full key agreement done in 00:00:00.0093703 Key generation done in 00:00:00.3615465. That is about 2770 keys/sec Key agreements done in 00:00:00.2800316. That is about 3571 agreement/sec
I also benchmarked the curve25519_generic (not the assembly one) and this library is about twice slower than Golang. Golang key agreement was about 6625 agreement/s.