Represents an authenticated encryption with associated data (AEAD) ChaCha20/Poly1305 cipher.

Namespace:  Rebex.Security.Cryptography
Assembly:  Rebex.Common (in Rebex.Common.dll)

Syntax

Visual Basic
Public NotInheritable Class ChaCha20Poly1305 _
	Implements IDisposable
C#
public sealed class ChaCha20Poly1305 : IDisposable

Examples

 Copy imageCopy Code
            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.
            
                }
            }
             

Inheritance Hierarchy

System..::..Object
  Rebex.Security.Cryptography..::..ChaCha20Poly1305

Version Information

.NET Compact Framework

Supported in: 3.9, 3.5

See Also