Does .NET framework support AES-256 CTR mode?

asked 2021-05-06T16:02:41.46+00:00
Linden Vo 1 Reputation point

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace EncryptionDecryptionUsingSymmetricKey
{
class Program
{
static void Main(string[] args)
{

        var key = new byte[16] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
        var iv = new byte[16] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
        var input = new byte[16] { 0x12, 0x34, 0x56, 0x78, 0x90, 0x09, 0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78, 0x90, 0x98 };

        var crypto = new AesCryptographyService();

        var encrypted = crypto.Encrypt(input, key, iv);
        var encryptedstr = BitConverter.ToString(encrypted).Replace("-", "");

        var decrypted = crypto.Decrypt(encrypted, key, iv);
        var decryptedstr = BitConverter.ToString(decrypted).Replace("-", "");
        Console.WriteLine(encryptedstr);
        Console.WriteLine(decryptedstr);
    }
}


public class AesCryptographyService
{
    public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
    {
        using (var aes = Aes.Create())
        {
            aes.KeySize = 128;
            aes.BlockSize = 128;
            aes.Padding = PaddingMode.Zeros;

            aes.Key = key;
            aes.IV = iv;

            using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
            {
                return PerformCryptography(data, encryptor);
            }
        }
    }

    public byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
    {
        using (var aes = Aes.Create())
        {
            aes.KeySize = 128;
            aes.BlockSize = 128;
            aes.Padding = PaddingMode.Zeros;

            aes.Key = key;
            aes.IV = iv;

            using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
            {
                return PerformCryptography(data, decryptor);
            }
        }
    }

    private byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)
    {
        using (var ms = new MemoryStream())
        using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
        {
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();

            return ms.ToArray();
        }
    }
}

}

I've implemented this code snippet to try to encrypt and decrypt an array of bytes, with great success. However, the requirement and desired block cipher mode of operation is supposed to be AES-256 CTR, not CBC by default.

Unfortunately, I have not been able to find much help on AES-256 CTR. (regarding the 128 bit key, I would simply change it to 256.)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,904 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-05-07T07:41:50.19+00:00
    Timon Yang-MSFT 9,501 Reputation points

    The built-in AES class does not implement the CTR mode.

    CipherMode Enum

    But I found some custom implementations, please see if they can work for you:

    Can I use AES in CTR mode in .NET?

    AesCounterMode.cs


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments