A .NET wrapper for the XChaCha constructions and APIs provided by libsodium crypto library.
The API supports the new Span<T>
and ReadOnlySpan<T>
types.
Currently only supports .NET Core 2.1.
XChaCha-DotNet contains the following ciphers:
Class | Algorithm | Description |
---|---|---|
XChaChaAeadCipher | XChaCha20-Poly1305 IETF AEAD | XChaCha AEAD cipher with 192-bit nonce and 192-bit key. Poly-1305 used to compute the authentication tag. Supports Additional Authenticated Data. Can safely encrypt messages up to 2^64 bytes. |
XChaChaStream | XChaCha20-Poly1305 | XChaCha20 stream cipher using Poly-1305 to compute the authentication tags. Automatically generates and rotates nonces. Supports using Additional Authenticated Data. Can safely encrypt messages up to any practical limit. |
XChaChaBufferedStream | XChaCha20-Poly1305 | XChaCha20 stream cipher using Poly-1305 to compute the authentication tags. Automatically generates and rotates nonces and buffers the input stream to encrypt data in fixed sized blocks. Can safely encrypt messages up to any practical limit. |
XChaChaSecretBoxCipher | XChaCha20-Poly1305 | An implementation of the secret_box API in the NaCl crypto library using XChaCha20 for the cipher and Poly-1305 to compute the authentication tag. |
Install from nuget:
Using the dotnet CLI:
dotnet add package xchacha-dotnet
Using the package manager:
Install-Package xchacha-dotnet
Example encryption/decryption using the XChaChaAeadCipher:
using (var key = XChaChaKey.Generate())
{
var aeadCipher = new XChaChaAeadCipher();
var nonce = XChaChaNonce.Generate();
var message = Encoding.UTF8.GetBytes("Test Message");
var ciphertext = aeadCipher.Encrypt(message, key, nonce);
}
var keyBytes = Convert.FromBase64String("XPRT6QuYZDdytKM55WW+gnZklhaJBcDnOWi1kEI2we4=");
var nonceBytes = Convert.FromBase64String("2eQuiE8Fy70rwlCAi5T2oVEj5MrwxJaT");
var ciphertext = Convert.FromBase64String("w2jUPkWL0PfvnNFM7xq4o9gcVKrMTkd6SsYhLQ==");
using (var key = new XChaChaKey(keyBytes))
{
var aeadCipher = new XChaChaAeadCipher();
var nonce = new XChaChaNonce(nonceBytes);
var message = aeadCipher.Decrypt(ciphertext, key, nonce);
}
Please raise issues to report bugs or request new features. Pull requests welcome.