Skip to content

Instantly share code, notes, and snippets.

@vcsjones
Created June 26, 2026 15:11
Show Gist options
  • Select an option

  • Save vcsjones/cd569386bfd78e12d8b1b71d5953d721 to your computer and use it in GitHub Desktop.

Select an option

Save vcsjones/cd569386bfd78e12d8b1b71d5953d721 to your computer and use it in GitHub Desktop.
AES ECB one-shot vs TransformFinalBlock BenchmarkDotNet repro

AES ECB one-shot vs TransformFinalBlock benchmark

This is the exact BenchmarkDotNet project used for the .NET 10 run of dotnet/runtime issue 58784's benchmark.

Note

This gist was prepared and published with GitHub Copilot.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>
</Project>
{
"sdk": {
"version": "10.0.301",
"rollForward": "latestFeature"
}
}
using System.Security.Cryptography;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Bench>();
public class Bench
{
byte[] _key, _plaintext, _ciphertext;
[GlobalSetup]
public void Setup()
{
_key = RandomNumberGenerator.GetBytes(32);
_plaintext = Encoding.UTF8.GetBytes("This is a test. This is a test. ");
using var tempAes = Aes.Create();
tempAes.Key = _key;
_ciphertext = tempAes.EncryptEcb(_plaintext, PaddingMode.None);
}
[Benchmark(Baseline = true)]
public byte[] Old_Enc()
{
using Aes aes = Aes.Create();
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.ECB;
using var transform = aes.CreateEncryptor(_key, null);
return transform.TransformFinalBlock(_plaintext, 0, _plaintext.Length);
}
[Benchmark]
public byte[] Old_Dec()
{
using Aes aes = Aes.Create();
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.ECB;
using var transform = aes.CreateDecryptor(_key, null);
return transform.TransformFinalBlock(_ciphertext, 0, _ciphertext.Length);
}
[Benchmark]
public byte[] New_Enc()
{
using Aes aes = Aes.Create();
aes.Key = _key;
return aes.EncryptEcb((ReadOnlySpan<byte>)_plaintext, PaddingMode.None);
}
[Benchmark]
public byte[] New_Dec()
{
using Aes aes = Aes.Create();
aes.Key = _key;
return aes.DecryptEcb((ReadOnlySpan<byte>)_ciphertext, PaddingMode.None);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment