How can I use System.Security.Cryptography.ChaCha20Poly1305 on Windows 10?

73 views Asked by At

I am trying to use System.Security.Cryptography.ChaCha20Poly1305 in C#. My platform and version information:

  • Windows NT 10.0.19045.0
  • .NET 8.0.3 (8.0.3)
  • System.Security.Cryptography version: System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

But whenever I try to use this class to encrypt anything, I receive the following error:

System.PlatformNotSupportedException: 'Algorithm 'ChaCha20Poly1305' is not supported on this platform.'

Why does this not work? From the docs it appears my platform is supported.

This is my simplified test console app:

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

namespace TestTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
            Console.WriteLine("{0} version: {1}", typeof(ChaCha20Poly1305).Namespace, typeof(ChaCha20Poly1305).Assembly.FullName);
            Console.WriteLine("ChaCha20Poly1305.IsSupported = {0}.\n", ChaCha20Poly1305.IsSupported);

            byte[] key = new byte[32];
            byte[] nonce = new byte[12];
            byte[] plainText = Encoding.UTF8.GetBytes("Hello, World!");
            byte[] cipherText = new byte[plainText.Length];
            byte[] tag = new byte[16];
            using var chaChaPoly = new ChaCha20Poly1305(key);
            chaChaPoly.Encrypt(nonce, plainText, cipherText, tag);
            
            Console.WriteLine("Base64 cipherText: {0}", Convert.ToBase64String(cipherText));
        }
    }
}

While this fails in my environment, it works on as a .NET Fiddle here: https://dotnetfiddle.net/qEURzH.

Update ChaCha20Poly1305.IsSupported is returning false. Why is this happening given that I am not using any of the documented unsupported platforms [UnsupportedOSPlatform("browser")], [UnsupportedOSPlatform("ios")] or [UnsupportedOSPlatform("tvos")]?

0

There are 0 answers