Secret Key Cryptography _Authenticated encryption (Default) - 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 = SodiumSecretBox.GenerateKey();
MessageBox.Show(new System.Numerics.BigInteger(SecretBoxKey).ToString());
Byte[] Nonce = SodiumSecretBox.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 = SodiumSecretBox.GenerateKey();
Nonce = SodiumSecretBox.GenerateNonce();
CipherText = SodiumSecretBox.Create(RandomByte, Nonce, StreamCipherKey);
OriginalText = SodiumSecretBox.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 = SodiumSecretBox.GenerateKey();
Nonce = SodiumSecretBox.GenerateNonce();
MyDetachedBox = SodiumSecretBox.CreateDetached(RandomByte, Nonce, StreamCipherKey);
OriginalText=SodiumSecretBox.OpenDetached(MyDetachedBox,Nonce,StreamCipherKey);
OriginalText2 = SodiumSecretBox.OpenDetached(MyDetachedBox.CipherText, MyDetachedBox.Mac, Nonce, StreamCipherKey);
MessageBox.Show(RandomByte.SequenceEqual(OriginalText).ToString());
MessageBox.Show(RandomByte.SequenceEqual(OriginalText2).ToString());