Show / Hide Table of Contents

Class Chacha20Poly1305

Represents a key to be used with ChaCha20 and Poly1305 for Authenticated Encryption with Associated Data.

Inheritance
Object
Aead
Chacha20Poly1305
Implements
IDisposable
Inherited Members
Aead.Encrypt(Byte[], Byte[], Byte[], Byte[], Byte[])
Aead.Decrypt(Byte[], Byte[], Byte[], Byte[], Byte[])
Aead.Encrypt(Byte[], Byte[], Byte[])
Aead.Decrypt(Byte[], Byte[], Byte[])
Aead.Encrypt(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Span<Byte>, ReadOnlySpan<Byte>)
Aead.Decrypt(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, ReadOnlySpan<Byte>)
Aead.Encrypt(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>)
Aead.Decrypt(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>)
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: AtlasRhythm.Cryptography.Aeads
Assembly: AtlasRhythm.Cryptography.dll
Syntax
public sealed class Chacha20Poly1305 : Aead, IDisposable
Examples

The following example demonstrates how to encrypt and decrypt a sample string using the Chacha20Poly1305 class.

using AtlasRhythm.Cryptography.Aeads;
using System.Security.Cryptography;
using System.Text;

// Create a new cryptographically secure random number generator
var rng = new RNGCryptoServiceProvider();

// Generate a random key of the appropriate length
var key = new byte[Chacha20Poly1305.KeySize];
rng.GetBytes(key);

// Create the instance
// Note the `using var`, this is necessary to make sure
// the memory containing the key is zeroed after use
using var aead = new Chacha20Poly1305(key);

// Generate a random nonce of the appropriate length
// A nonce must *never* be used twice with the same key
var nonce = new byte[Chacha20Poly1305.NonceSize];
rng.GetBytes(nonce);

// Obtain the plaintext (content to encrypt) and associated data
// The associated data is just used as additional authentication security
// and is optional
var plaintext = Encoding.UTF8.GetBytes("very secret plaintext");
var associatedData = Encoding.UTF8.GetBytes("very secret associated data");

// Encrypt the plaintext and return a buffer containing
// the ciphertext (encrypted contents) and the authentication tag
var output = aead.Encrypt(nonce, plaintext, associatedData);

// Decrypt and authenticate the previously obtained output
string decryptedPlaintext;
try
{
    decryptedPlaintext = Encoding.UTF8.GetString(aead.Decrypt(nonce, output, associatedData));
}
catch (CryptographicException ex)
{
    // An exception will be thrown if the authentication tag can't be verified
    // This usually means the contents have been tampered with
}

Constructors

| Improve this Doc View Source

Chacha20Poly1305(Byte[])

Initializes a new instance of the Chacha20Poly1305 class with a provided key.

Declaration
public Chacha20Poly1305(byte[] key)
Parameters
Type Name Description
Byte[] key

The secret key to use for this instance.

Exceptions
Type Condition
ArgumentNullException

The key parameter is null.

CryptographicException

The key parameter length is other than 32 bytes (256 bits).

Fields

| Improve this Doc View Source

KeySize

Key size, in bytes, supported by this instance.

Declaration
public const int KeySize = 32
Field Value
Type Description
Int32
| Improve this Doc View Source

NonceSize

Nonce size, in bytes, supported by this instance.

Declaration
public const int NonceSize = 12
Field Value
Type Description
Int32
| Improve this Doc View Source

TagSize

Tag size, in bytes, supported by this instance.

Declaration
public const int TagSize = 16
Field Value
Type Description
Int32

Properties

| Improve this Doc View Source

Key

Declaration
protected override byte[] Key { get; }
Property Value
Type Description
Byte[]
Overrides
Aead.Key
| Improve this Doc View Source

KeyByteSizes

Declaration
public override KeySizes KeyByteSizes { get; }
Property Value
Type Description
KeySizes
Overrides
Aead.KeyByteSizes
| Improve this Doc View Source

NonceByteSizes

Declaration
public override KeySizes NonceByteSizes { get; }
Property Value
Type Description
KeySizes
Overrides
Aead.NonceByteSizes
| Improve this Doc View Source

TagByteSizes

Declaration
public override KeySizes TagByteSizes { get; }
Property Value
Type Description
KeySizes
Overrides
Aead.TagByteSizes

Methods

| Improve this Doc View Source

DecryptCore(Byte*, Byte*, Byte*, Int32, Byte*, Byte*, Int32, Byte*)

Declaration
protected override bool DecryptCore(byte *key, byte *nonce, byte *ciphertext, int size, byte *tag, byte *associatedData, int associatedDataSize, byte *plaintext)
Parameters
Type Name Description
Byte* key
Byte* nonce
Byte* ciphertext
Int32 size
Byte* tag
Byte* associatedData
Int32 associatedDataSize
Byte* plaintext
Returns
Type Description
Boolean
Overrides
Aead.DecryptCore(Byte*, Byte*, Byte*, Int32, Byte*, Byte*, Int32, Byte*)
| Improve this Doc View Source

Dispose()

Declaration
public void Dispose()
| Improve this Doc View Source

EncryptCore(Byte*, Byte*, Byte*, Int32, Byte*, Int32, Byte*, Byte*)

Declaration
protected override void EncryptCore(byte *key, byte *nonce, byte *plaintext, int size, byte *associatedData, int associatedDataSize, byte *ciphertext, byte *tag)
Parameters
Type Name Description
Byte* key
Byte* nonce
Byte* plaintext
Int32 size
Byte* associatedData
Int32 associatedDataSize
Byte* ciphertext
Byte* tag
Overrides
Aead.EncryptCore(Byte*, Byte*, Byte*, Int32, Byte*, Int32, Byte*, Byte*)

Implements

System.IDisposable
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX