1

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?

2 Answers2

3

Something like this should work:

var serialHexString = "40D5C2ADDEFD92740000000B9B62";
var serial = BigInteger.Parse(serialHexString, NumberStyles.HexNumber);
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • Thanks, but could you please tell me how to get the integer out of the resulting "serial" variable that of BigInteger type? –  Mar 16 '15 at 13:11
  • 1
    The value is too big for an `Int32` (or `Int64`) to represent it. That's why you need to use `BigInteger`. – Sani Huttunen Mar 16 '15 at 13:12
  • Alright, got it. Convert it to string using Convert::ToString(). –  Mar 16 '15 at 13:28
  • 1
    You don't need to use `Convert::ToString()`. `serial.ToString()` will suffice. – Sani Huttunen Mar 16 '15 at 13:30
  • Are we talking about `System.Numerics.BigInteger` or `Org.BouncyCastle.Math.BigInteger`? – FranzHuber23 Mar 19 '19 at 13:49
  • 1
    @FranzHuber23: [System.Numerics.BigInteger](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.7.2). – Sani Huttunen Mar 19 '19 at 13:50
  • [@Sani Singh Huttunen](https://stackoverflow.com/users/26742/sani-singh-huttunen) Thanks for the info. I'm using BouncyCastle. There this operation is possible via: `new Org.BouncyCastle.Math.BigInteger(serialHexString, 16)`. Just if anyone searches. – FranzHuber23 Mar 19 '19 at 13:55
0

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.

user2299523
  • 111
  • 3