user4469411
user4469411

Reputation:

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?

Upvotes: 1

Views: 3618

Answers (2)

Sani Huttunen
Sani Huttunen

Reputation: 24395

Something like this should work:

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

Upvotes: 4

user2299523
user2299523

Reputation: 133

The serial number in your object is exactly the serial number in XML but in hexadecimal notation. So the real question is 'Given a large hexadecimal string, how to obtain it's decimal representation?' If you are targeting NET 4+, you can use BigInteger.

Upvotes: 0

Related Questions