Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sat Dec 21, 2019 7:54 am

Hello,
I am trying to test your api in order to sign pdf files with a certificate and LTV enabled. I think I didn't quite understand how it works. I get the message "Spire.Pdf.Exceptions.PdfException: 'lost private key.'" at line
Code: Select all
doc.SaveToFile(aFullSignedFilePath)
.
The code I am using is:

Code: Select all
  Dim page As PdfPageBase = doc.Pages(0)
        certificate = ReturnSelectedCertificate()
        Dim bytecertificate As Byte() = certificate.Export(X509ContentType.Cert, "test")
        Dim cert As New PdfCertificate(bytecertificate, "test")
        Dim signature As PdfSignature = New PdfSignature(doc, page, cert, "demo")
        signature.ContactInfo = "K********s"
        signature.Certificated = True
        signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges
        signature.ConfigureHttpOCSP(Nothing, Nothing)
        doc.SaveToFile(aFullSignedFilePath)

p.chantsaris
 
Posts: 2
Joined: Fri Nov 29, 2019 12:34 pm

Mon Dec 23, 2019 10:50 am

Hi,

Thanks for your inquiry.
Please try to set private key from the certificate. Below is the code for you.
Code: Select all
        Dim bytecertificate As Byte() = certificate.Export(X509ContentType.Cert, "test")
        Dim cert As New PdfCertificate(bytecertificate, "test")
        'Get the private key from the certificate first then set it to the PdfCertificate object
        cert.PrivateKey = store.Certificates(0).PrivateKey

If the issue still exists, please offer us the following information for further investigation.
1. The input PDF file
2. The full code how you get the certificate
3. Provide us your certificate file if possible

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Dec 23, 2019 5:22 pm

Now I am getting the following:
Code: Select all
 
Spire.Pdf.Exceptions.PdfException: export private key failed. ---> System.Security.Cryptography.CryptographicException: Key not valid for use in specified state

 σε System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   σε System.Security.Cryptography.Utils._ExportKey(SafeKeyHandle hKey, Int32 blobType, Object cspObject)
   σε System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(Boolean includePrivateParameters)
   σε spr⥛.ᜁ(RSA A_0)
   σε spr⥛.ᜀ(AsymmetricAlgorithm A_0)
   σε spr⡽.ᜀ(Byte[][] A_0)
   --- Τέλος ιχνηλάτησης στοίβας εσωτερικών εξαιρέσεων ---
   σε spr⡽.ᜀ(Byte[][] A_0)
   σε spr⢐.ᜀ(Byte[][] A_0)
   σε spr⢐.ᜀ()
   σε spr⢐.ᜂ(spr᜵ A_0)
   σε spr⢐.ᜀ(Object A_0, spr᝝ A_1)
   σε spr᝛.ᜀ(spr᝝ A_0)
   σε spr᝛.ᜀ(spr᜵ A_0, Boolean A_1)
   σε spr᝛.ᜅ(spr᜵ A_0)
   σε spr៝.ᜀ(sprᜳ A_0, sprᝢ A_1, spr៦ A_2)
   σε spr៝.ᜀ(sprᜳ A_0, spr៦ A_1)
   σε spr៝.ᜄ(spr៦ A_0)
   σε spr៝.ᜅ(spr៦ A_0)
   σε sprᢊ.ᜁ(spr៦ A_0)
   σε sprᢊ.ᜁ(Stream A_0)
   σε Spire.Pdf.PdfDocumentBase.Save(String filename)
   σε Spire.Pdf.PdfDocument.SaveToFile(String filename)


the code I use to get the certificate is :
Code: Select all
 Protected Function ReturnSelectedCertificate() As X509Certificate2
        Dim st0re As X509Store = New X509Store(StoreLocation.CurrentUser)
        st0re.Open(OpenFlags.ReadOnly)
        Dim count = st0re.Certificates.Count
        Dim certs As X509Certificate2Collection = st0re.Certificates.Find(
            X509FindType.FindBySerialNumber,
            CertificateSerialNumber,
            True)
        st0re.Close()

        Dim aCertificate As X509Certificate2 = certs(0)

        Return aCertificate
    End Function


and the code I use for signing is:

Code: Select all
    Protected Sub SignTheDocumentWithSpirePDF(ByVal aFileName As String, ByVal aFilePath As String, ByVal aFullSignedFilePath As String)
        Try
            Dim aFullFilePath As String = aFilePath & "\" & aFileName
            Dim doc As PdfDocument = New PdfDocument(aFullFilePath)
            Dim page As PdfPageBase = doc.Pages(0)
            certificate = ReturnSelectedCertificate()

            Dim bytecertificate As Byte() = certificate.Export(X509ContentType.Cert, "test")
            Dim cert As New PdfCertificate(bytecertificate, "test")
            cert.PrivateKey = certificate.PrivateKey


            Dim signature As PdfSignature = New PdfSignature(doc, page, cert, "demo")
            signature.ContactInfo = "K********s"
            signature.Certificated = True
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges
            signature.ConfigureHttpOCSP(Nothing, Nothing)
            doc.SaveToFile(aFullSignedFilePath)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

p.chantsaris
 
Posts: 2
Joined: Fri Nov 29, 2019 12:34 pm

Tue Dec 24, 2019 8:21 am

Hi,

Thanks for your information.
After further investigation, we found you need to ensure the certificate is set as exportable when installing the certificate. I have attached the screenshot here, please check it.
And we will consider making an adjustment to eliminate the exception when signing the PDF without setting the certificate as exportable when installing the certificate. Once there is any progress, we will inform you. Sorry for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Wed Nov 04, 2020 10:24 am

Hello,

Thanks for your patience.
Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:6.11.0 that supports signing the PDF without setting the certificate as exportable when installing the certificate. Please download it and refer to the following code to test.
Code: Select all
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"DigitalSignature.pdf");
            PdfPageBase page = doc.Pages[0];
            X509Store store = new X509Store(StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 cert =  store.Certificates[0];
            PdfCertificate pdfcert = new PdfCertificate(cert);
            PdfSignature signature = new PdfSignature(doc, page, pdfcert , "demo");
            signature.ContactInfo = "K********s";
            signature.Certificated = true;
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;
            signature.ConfigureHttpOCSP(null, null);//LTV
            doc.SaveToFile("result.pdf", FileFormat.PDF);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Nov 13, 2020 10:03 am

Hello,

Greetings from E-iceblue!
Have you tested the hotfix? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF