using System; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Numerics; namespace SampleSoftCert { class Program { // Helper method to convert a hexadecimal string to a byte array private static byte[] HexStringToByteArray(string hex) { int length = hex.Length; byte[] bytes = new byte[length / 2]; for (int i = 0; i < length; i += 2) { bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); } return bytes; } static void Main(string[] args) { String PIN = "12345678"; String softcertFile = "D:\\DEVELOPMENT\\DEV-WORKSPACE\\SampleSoftCert\\sample-co.p12"; String dataToSign = "e-invoice data"; // Load the .p12 file into an X509Certificate2 object X509Certificate2 certificate = new X509Certificate2(softcertFile, PIN); // Check if the certificate has a private key if (!certificate.HasPrivateKey) { Console.WriteLine("Certificate does not have a private key."); return; } // Get the private key from the certificate RSA privateKey = certificate.GetRSAPrivateKey(); Console.WriteLine(certificate.Issuer); Console.WriteLine(certificate.SerialNumber); // Export the certificate to a byte array byte[] certBytes = certificate.Export(X509ContentType.Cert); // Convert the byte array to a Base64 string string certBase64 = Convert.ToBase64String(certBytes); // Display the Base64 encoded certificate Console.WriteLine("Base64 Encoded Certificate:"); Console.WriteLine(certBase64); // Get the serial number as a hexadecimal string string serialNumberHex = certificate.SerialNumber; // Convert the hexadecimal string to a byte array byte[] serialNumberBytes = HexStringToByteArray(serialNumberHex); // Reverse the byte array to match the little-endian format Array.Reverse(serialNumberBytes); // Convert the byte array to a BigInteger BigInteger serialNumberBigInt = new BigInteger(serialNumberBytes); // Display the serial number as an integer Console.WriteLine("Serial Number (BigInteger): " + serialNumberBigInt); // Initialize the signature object using (RSA rsa = privateKey) { // Create an instance of the SHA256 hash algorithm using (SHA256 sha256 = SHA256.Create()) { // Compute the hash of the data byte[] hash = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataToSign)); // Create an RSA signature formatter RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(rsa); rsaFormatter.SetHashAlgorithm("SHA256"); // Create the signature byte[] signature = rsaFormatter.CreateSignature(hash); // Convert the signature to a base64 string for display string signatureBase64 = Convert.ToBase64String(signature); Console.WriteLine("Signature: " + signatureBase64); } } } } }