Search code examples
.netx509certificate2serial-numberxml-dsig

Getting X509SerialNumber integer value from X509Certificate2


I am trying to get a X509.v3 certificate's (that I have as X509Certificate2 object) serial number to put it into X509SerialNumber element in XADES XMLDSIG, which is supposed to be an integer. I have an XML signature made by an other software using the very certificate I'm working with, and here's its serial number:

<X509SerialNumber xmlns="http://www.w3.org/2000/09/xmldsig#">1315010063538360283821765366094690</X509SerialNumber>

Unfortunately, I am unable to get this value out of X509Certificate2 object that's initialized with the very certificate used to sign the aforementioned XML. These are the value that I am getting

X509Certificate2->SerialNumber = "40D5C2ADDEFD92740000000B9B62"
X509Certificate2->GetSerialNumber() = "40D5C2ADDEFD92740000000B9B62"
Convert::ToBase64String(X509Certificate2->GetSerialNumber()) = "YpsLAAAAdJL93q3C1UA="

I reckon that GetSerialNumber() returns a Base64String. As you can see, GetSerialNumber() and GetSerialNumber() return different values. What is the way to get the integer of value "1315010063538360283821765366094690" out of these values?


Solution

  • Something like this should work:

    var serialHexString = "40D5C2ADDEFD92740000000B9B62";
    var serial = BigInteger.Parse(serialHexString, NumberStyles.HexNumber);