ChaCha20Poly1305 Class
Namespace: Rebex.Security.Cryptography
Assembly: Rebex.Security.dll (version 7.0.9119)
Represents an authenticated encryption with associated data (AEAD) ChaCha20/Poly1305 cipher.
Syntax
public sealed class ChaCha20Poly1305 : IDisposable
Implements
Inherited Members
Examples
public void EncryptAndDecryptData()
{
//Prepare plain text.
byte[] plainText = GetMyPlainText();
//Create the array for the resulting cipher text.
byte[] cipherText = new byte[plainText.Length];
byte[] key;
byte[] nonce;
using (var randomGenerator = RandomNumberGenerator.Create())
{
//Generate random key.
key = new byte[ChaCha20Poly1305.KeySize];
randomGenerator.GetBytes(key);
//Generate random nonce.
nonce = new byte[ChaCha20Poly1305.NonceSize];
randomGenerator.GetBytes(nonce);
}
//Prepare additional authentication data. Can be null.
byte[] aaaData = GetMyAadData();
//Prepare the array in which will be stored authentication tag.
var authTag = new byte[ChaCha20Poly1305.AuthenticationTagSize];
//Create the instance of the ChaCha20Poly1305 class.
using (var chacha20Poly1305 = new ChaCha20Poly1305(key))
{
//Encrypt data.
chacha20Poly1305.Encrypt(nonce, plainText, cipherText, authTag, aaaData);
//After a successful call cipherText contains encrypted data and authTag contains authentication tag.
}
//Later decrypt data.
//Prepare an array in which decrypted data will be stored.
byte[] decryptedData = new byte[cipherText.Length];
//Create the instance of the ChaCha20Poly1305 class and use the same key.
using (var chacha20Poly1305 = new ChaCha20Poly1305(key))
{
//Decrypt data. Use the same nonce, same additional authentication data, and use the authTag from the previous encrypt operation.
chacha20Poly1305.Decrypt(nonce, cipherText, authTag, decryptedData, aaaData);
//After a successful call decryptedData contains decrypted data.
}
}
Constructors
Name | Description |
---|---|
ChaCha20Poly1305(Byte[]) | Initializes a new ChaCha20Poly1305 instance with the provided |
Fields
Name | Description |
---|---|
AuthenticationTagSize | ChaCha20Poly1305 authentication tag size (in bytes). The value of the constant is 16. |
KeySize | Required ChaCha20Poly1305 key size (in bytes). The value of the constant is 32. |
NonceSize | Required ChaCha20Poly1305 nonce size (in bytes). The value of the constant is 12. |
Methods
Name | Description |
---|---|
Decrypt(Byte[], Byte[], Byte[], Byte[], Byte[]) | Decrypts |
Dispose() | Releases the resources used by the current instance. |
Encrypt(Byte[], Byte[], Byte[], Byte[], Byte[]) | Encrypts |