Class Chacha20Poly1305
Represents a key to be used with ChaCha20 and Poly1305 for Authenticated Encryption with Associated Data.
Inheritance
Chacha20Poly1305
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[])
Declaration
public Chacha20Poly1305(byte[] key)
Parameters
| Type |
Name |
Description |
| Byte[] |
key |
The secret key to use for this instance.
|
Exceptions
Fields
|
Improve this Doc
View Source
KeySize
Key size, in bytes, supported by this instance.
Declaration
public const int KeySize = 32
Field Value
|
Improve this Doc
View Source
NonceSize
Nonce size, in bytes, supported by this instance.
Declaration
public const int NonceSize = 12
Field Value
|
Improve this Doc
View Source
Tag size, in bytes, supported by this instance.
Declaration
public const int TagSize = 16
Field Value
Properties
|
Improve this Doc
View Source
Key
Declaration
protected override byte[] Key { get; }
Property Value
Overrides
|
Improve this Doc
View Source
KeyByteSizes
Declaration
public override KeySizes KeyByteSizes { get; }
Property Value
Overrides
|
Improve this Doc
View Source
NonceByteSizes
Declaration
public override KeySizes NonceByteSizes { get; }
Property Value
Overrides
|
Improve this Doc
View Source
TagByteSizes
Declaration
public override KeySizes TagByteSizes { get; }
Property Value
Overrides
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
Returns
Overrides
|
Improve this Doc
View Source
Dispose()
Declaration
|
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
Overrides
Implements