CSharp-AES-CTR-NetStandard example

CSharp · Console

/Program.cs

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

		// In real world, generate these with cryptographically secure pseudo-random number generator (CSPRNG)
		byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
		byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };
		
		// Print 1
		Console.WriteLine($"Text as ASCII bytes \t{BitConverter.ToString(mySimpleTextAsBytes)}");

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

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