1. Ong Chin Keat
  2. PowerBuilder
  3. Monday, 15 July 2024 00:49 AM UTC

hi all,

Sorry I'm not good in VS C#, I just want to complete my job by importing C# Certificate class library into PB and hope anyone can help me. I get an solution from some where that can hashing Certificate. Would like to know how we can convert it into Class Library and import into PB. For this, what value should we pass in for Certificate object "X509Certificate2" ?

Below is the script from C# : 

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
public class XadesSigner
    {
        public static byte[] SignDigest(string base64Digest, X509Certificate2 certificate)
        {
            if (string.IsNullOrEmpty(base64Digest) || certificate == null)
            {
                throw new ArgumentNullException("Digest and certificate must not be null or empty");
            }
            // Convert base64 digest string to byte array
            byte[] digest = Convert.FromBase64String(base64Digest);
            // Get the private key from the certificate
            using (RSA rsa = certificate.GetRSAPrivateKey())
            {
                if (rsa == null)
                {
                    throw new InvalidOperationException("Certificate does not have an RSA private key");
                }
                // Sign the digest using the RSA private key
                byte[] signedDigest = rsa.SignHash(digest, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                return signedDigest;
            }
        }
        public static void Main()
        {
            // Example base64-encoded digest value (SHA-256 hash of the data to be signed)
            string base64Digest = "<enter DocDigest String>";
            // Path to the certificate file
            string certificatePath = @"<enter path to PFX file>";
            // Load the certificate from a file or certificate store
            X509Certificate2 certificate = new X509Certificate2(certificatePath, "<Enter PFX Password>");
            // Sign the digest
            byte[] signedDigest = SignDigest(base64Digest, certificate);
            // Print the signed digest in base64
            Console.WriteLine(Convert.ToBase64String(signedDigest));
        }
    }

regards,

Dev Ong

Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 16 July 2024 14:12 PM UTC
  2. PowerBuilder
  3. # 1

Hi Ong,

Where does the certificate you want to import come from? What do you want to achieve by importing the certificate?

 

Regards,
Francisco

Comment
There are no comments made yet.
Ong Chin Keat Accepted Answer Pending Moderation
  1. Wednesday, 17 July 2024 00:59 AM UTC
  2. PowerBuilder
  3. # 2

hi Francisco,

Morning. The usage is for eInvoice processing. The detail of signing the eInvoice can refer to "Signing Process - Documentation - Fatoora Developer Community (zatca1.discourse.group)"  or  "Signature (hasil.gov.my)" .

Please advise is there any PB solution been made for accomplish such eInvoice requirement so far in any countries? Understand from the statistic, this eInvoice similar to Europe e-invoice using the PEPPOL network.

regards,

Dev Ong

 

 

Comment
  1. Francisco Martinez @Appeon
  2. Wednesday, 17 July 2024 15:58 PM UTC
Where does the certificate come from? Do you have a .cer file? Or is it in the system's cert store?
  1. Helpful
There are no comments made yet.
Ong Chin Keat Accepted Answer Pending Moderation
  1. Thursday, 18 July 2024 00:37 AM UTC
  2. PowerBuilder
  3. # 3

hi Francisco,

Hereby I attached trial cert and ultimate output of XML file that being enveloped in Cert Sign extension element. Please find those files attached.

Attachments (1)
Comment
  1. Francisco Martinez @Appeon
  2. Thursday, 18 July 2024 15:35 PM UTC
Hi Ong,



If the certificate is a file, then I suggest you modify the C# method a little bit to instead of passing the certificate as parameter, you pass the path of the certificate. Also, instead of returning a byte[] you pass it back out through a reference parameter. After those changes you will be able to import the C# class into PB by using the .NET Assembly Importer Tool: https://docs.appeon.com/pb2022r3/application_techniques/ch05s01s01.html



You can load the certificate into a X509Certificate2 object with the following method: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.createfrompemfile?view=net-6.0
  1. Helpful 1
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.