using System.Formats.Asn1;
public static bool ShouldRemoveLeadingZeroes(byte[] bytes)
if (0x0 == bytes[0] && (bytes[1] & (1 << 7)) != 0)
public static void Main()
string fail = "MEUCICAiMapES55djGcYoBWjLTIC74+7uWR+ceRHAZyQmJaYAiEA8vfd+Uhg9h3bKIMWA7l9t3Kq8nVk4oa45/Gbs+pQTcM=";
byte[] failBytes = Convert.FromBase64String(fail);
AsnReader asnReaderFail = new(new ReadOnlyMemory<byte>(failBytes), AsnEncodingRules.DER);
var failSeq = asnReaderFail.ReadSequence();
var failIntR = failSeq.ReadIntegerBytes().ToArray();
var failIntS = failSeq.ReadIntegerBytes().ToArray();
Console.WriteLine("Fail R: " + Convert.ToHexString(failIntR) + ", Should remove leading 00: " + ShouldRemoveLeadingZeroes(failIntR));
Console.WriteLine("Fail S: " + Convert.ToHexString(failIntS) + ", Should remove leading 00: " + ShouldRemoveLeadingZeroes(failIntS));
Console.WriteLine(Convert.ToString(failIntS[0], 2).PadLeft(8, '0'));
Console.WriteLine(Convert.ToString(failIntS[1], 2).PadLeft(8, '0'));
string success = "MEQCIHpDRriOIExTuSu/Pps+wz53QNBIVvkZkpKqKDvPL18fAiA3gbgWgHeXLbS/VH55yQsISkJF0enJpDmpVL4k+I5Sng==";
byte[] successBytes = Convert.FromBase64String(success);
AsnReader asnReadersuccess = new(new ReadOnlyMemory<byte>(successBytes), AsnEncodingRules.DER);
var successSeq = asnReadersuccess.ReadSequence();
var successIntR = successSeq.ReadIntegerBytes().ToArray();
var successIntS = successSeq.ReadIntegerBytes().ToArray();
Console.WriteLine("Success R: " + Convert.ToHexString(successIntR) + ", Should remove leading 00: " + ShouldRemoveLeadingZeroes(successIntR));
Console.WriteLine("Success S: " + Convert.ToHexString(successIntS) + ", Should remove leading 00: " + ShouldRemoveLeadingZeroes(successIntS));