CSharp-ChaCha20-NetStandard example

CSharp · Console

/Program.cs

using System;
using System.Text;
using CSChaCha20;
					
public class Program
{
	public static void Main()
	{
		byte[] mySimpleTextAsBytes = Encoding.ASCII.GetBytes("Plain text I want to encrypt");

		// Do not use these key and nonce values in your own code!
		byte[] key = new byte[32] { 142, 26, 14, 68, 43, 188, 234, 12, 73, 246, 252, 111, 8, 227, 57, 22, 168, 140, 41, 18, 91, 76, 181, 239, 95, 182, 248, 44, 165, 98, 34, 12 };
		byte[] nonce = new byte[12] { 139, 164, 65, 213, 125, 108, 159, 118, 252, 180, 33, 88 };
		uint counter = 1;
		
		// Print 1
		Console.WriteLine($"Text as ASCII bytes \t{BitConverter.ToString(mySimpleTextAsBytes)}");

		// Encrypt
		ChaCha20 forEncrypting = new ChaCha20(key, nonce, counter);
		byte[] encryptedContent = new byte[mySimpleTextAsBytes.Length];
		forEncrypting.EncryptBytes(encryptedContent, mySimpleTextAsBytes);
		
		// Print 2
		Console.WriteLine($"Encrypted bytes \t\t{BitConverter.ToString(encryptedContent)}");

		// Decrypt
		ChaCha20 forDecrypting = new ChaCha20(key, nonce, counter);
		byte[] decryptedContent = new byte[encryptedContent.Length];
		forDecrypting.DecryptBytes(decryptedContent, encryptedContent);
		
		// Print 2
		Console.WriteLine($"Decrypted bytes \t\t{BitConverter.ToString(decryptedContent)}");
	}
}
;