Secret Key Cryptography _AEAD_XChaCha20Poly1305 - Chewhern/ASodium GitHub Wiki

For detailed documentations, kindly refer libsodium. There're also other various functions which I didn't cover in this wiki page, for details kindly refer to this link (https://github.com/Chewhern/ASodium/blob/main/Source/SodiumSecretAeadXChaCha20Poly1305IETF.cs)

Nonce and Key Generation

Initial Functions

public static Byte[] GenerateKey()
public static Byte[] GeneratePublicNonce()

Example Code

Byte[] Key = SodiumSecretAeadXChaCha20Poly1305IETF.GenerateKey();
Byte[] NoncePublic = SodiumSecretXAeadChaCha20Poly1305IETF.GeneratePublicNonce();

Combined encryption and decryption

Initial Functions

public static Byte[] Encrypt(Byte[] Message, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity=null,Boolean ClearKey=false)
public static Byte[] Decrypt(Byte[] CipherText,Byte[] NoncePublic,Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)

Example Code

Byte[] RandomData = SodiumRNG.GetRandomBytes(128);
Byte[] Key = SodiumSecretAeadXChaCha20Poly1305IETF.GenerateKey();
Byte[] NoncePublic = SodiumSecretAeadXChaCha20Poly1305IETF.GeneratePublicNonce();
Byte[] EncryptedData = SodiumSecretAeadXChaCha20Poly1305IETF.Encrypt(RandomData, NoncePublic, Key);
Byte[] DecryptedData = SodiumSecretAeadXChaCha20Poly1305IETF.Decrypt(EncryptedData, NoncePublic, Key);

Detached encryption and decryption

Initial Functions

public static ChaCha20Poly1305DetachedBox CreateDetachedBox(Byte[] Message,Byte[] NoncePublic,Byte[] Key,Byte[] NonceSecurity=null,Byte[] AdditionalData=null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(ChaCha20Poly1305DetachedBox MyDetachedBox, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(Byte[] CipherText,Byte[] MAC , Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false) 

Example Code

Byte[] RandomData = SodiumRNG.GetRandomBytes(128);
Byte[] Key = SodiumSecretAeadXChaCha20Poly1305IETF.GenerateKey();
Byte[] NoncePublic = SodiumSecretAeadXChaCha20Poly1305IETF.GeneratePublicNonce();
Byte[] DecryptedText1 = new Byte[] { };
Byte[] DecryptedText2 = new Byte[] { };
ChaCha20Poly1305DetachedBox MyDetachedBox = SodiumSecretAeadXChaCha20Poly1305IETF.CreateDetachedBox(RandomData, NoncePublic, Key);
DecryptedText1 = SodiumSecretAeadXChaCha20Poly1305IETF.OpenDetachedBox(MyDetachedBox, NoncePublic, Key);
DecryptedText2 = SodiumSecretAeadXChaCha20Poly1305IETF.OpenDetachedBox(MyDetachedBox.CipherText, MyDetachedBox.MAC, NoncePublic, Key);