Share via

How do I DER encode a signature in .NET Core 3.1?

Sam Amin 31 Reputation points
Jul 29, 2021, 1:16 PM

Hi,

I have a requirement for a new integration we are building to sign the contents of the request we send using a private key, then DER Encode it , and Base 64 Encode it.

I can do it in .Net Core 5.0 easily as follows:

byte[] signedData;
    var dsa = ECDsa.Create();
    dsa.ImportECPrivateKey(Convert.FromBase64String(key), out _);
    signedData = dsa.SignData(requestByteArray, 
         HashAlgorithmName.SHA256,DSASignatureFormat.Rfc3279DerSequence);
    string base64Signature = Convert.ToBase64String(signedData);

But, unfortunately, our project is .Net Core 3.1 and we're not planning on upgrading it at the moment.

In 3.1 this is the only available option for the SignData method, and I need to find a way to DER encode the output but I can't find any way to do it. I'm sure it's doable using BouncyCastle, but I can't figure it out from the documentation.

signedData = dsa.SignData(requestByteArray, HashAlgorithmName.SHA256);

Any help here would be highly appreciated.

Thanks
Sam

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,728 questions
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.
11,183 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.