Secret Key Cryptography _Authenticated encryption (XChaCha20Poly1305) - Chewhern/ASodium GitHub Wiki

For full details, kindly refer to libsodium official documentation..

Key and Nonce Generation

Initial functions

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

Example code

Byte[] SecretBoxKey = SodiumSecretBoxXChaCha20Poly1305.GenerateKey();
MessageBox.Show(new System.Numerics.BigInteger(SecretBoxKey).ToString());
Byte[] Nonce = SodiumSecretBoxXChaCha20Poly1305.GenerateNonce();
MessageBox.Show(new System.Numerics.BigInteger(Nonce).ToString());

Combined mode encryption and decryption

Initial functions

public static Byte[] Create(Byte[] Message, Byte[] Nonce, Byte[] Key,Boolean ClearKey=false)
public static Byte[] Open(Byte[] CipherText, Byte[] Nonce, Byte[] Key, Boolean ClearKey = false)

Example code

Byte[] RandomByte = new Byte[32];
Byte[] CipherText = new Byte[] { };
Byte[] OriginalText = new Byte[] { };
Byte[] StreamCipherKey = new Byte[] { };
Byte[] Nonce = new Byte[] { };
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
//The random byte here serves as message byte
//In actual code, the message can come from numerous sources
StreamCipherKey = SodiumSecretBoxXChaCha20Poly1305.GenerateKey();
Nonce = SodiumSecretBoxXChaCha20Poly1305.GenerateNonce();
CipherText = SodiumSecretBoxXChaCha20Poly1305.Create(RandomByte, Nonce, StreamCipherKey);
OriginalText = SodiumSecretBoxXChaCha20Poly1305.Open(CipherText, Nonce, StreamCipherKey);
MessageBox.Show(OriginalText.SequenceEqual(RandomByte).ToString());

Detached mode encryption and decryption

Initial Functions

public static DetachedBox CreateDetached(String Message, Byte[] Nonce, Byte[] Key,Boolean ClearKey=false)
public static DetachedBox CreateDetached(Byte[] Message, Byte[] Nonce, Byte[] Key,Boolean ClearKey=false)
public static Byte[] OpenDetached(DetachedBox detached, Byte[] Nonce, Byte[] Key, Boolean ClearKey = false)
public static Byte[] OpenDetached(Byte[] CipherText, Byte[] MAC, Byte[] Nonce, Byte[] Key,Boolean ClearKey=false)

Example code

DetachedBox MyDetachedBox = new DetachedBox();
Byte[] RandomByte = new Byte[32];
Byte[] CipherText = new Byte[] { };
Byte[] OriginalText = new Byte[] { };
Byte[] OriginalText2 = new Byte[] { };
Byte[] StreamCipherKey = new Byte[] { };
Byte[] Nonce = new Byte[] { };
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
StreamCipherKey = SodiumSecretBoxXChaCha20Poly1305.GenerateKey();
Nonce = SodiumSecretBoxXChaCha20Poly1305.GenerateNonce();
MyDetachedBox = SodiumSecretBoxXChaCha20Poly1305.CreateDetached(RandomByte, Nonce, StreamCipherKey);
OriginalText=SodiumSecretBoxXChaCha20Poly1305.OpenDetached(MyDetachedBox,Nonce,StreamCipherKey);
OriginalText2 = SodiumSecretBoxXChaCha20Poly1305.OpenDetached(MyDetachedBox.CipherText, MyDetachedBox.Mac, Nonce, StreamCipherKey);
MessageBox.Show(RandomByte.SequenceEqual(OriginalText).ToString());
MessageBox.Show(RandomByte.SequenceEqual(OriginalText2).ToString());