注意:我已经对我的大部分帖子进行了大量编辑,因为我现在已经进一步推进了
我目前正在开展一个小项目:一般的想法是用户选择一个文件夹,插入他的E-ID,该文件夹中的所有PDF文件都会被修改,带有他的数字签名和图像,以便在打印时表示。 我目前正在使用iTextSharp框架来实现这一目标。但是因为我必须将源代码转换为VB.NET,所以我已经完全停止了。
但是,以下代码完成了向PDF文档添加数字签名的任务。它只使用我用Visual Studio创建的测试证书。其他任何内容和PDF都没有创建,我已经检查过断点,myPkcs12Store没有填充任何内容:我无法从eID中检索个人密钥。
Private Sub Test()
Dim myKeyStore As New X509Store(StoreName.My, StoreLocation.CurrentUser)
myKeyStore.Open(OpenFlags.[ReadOnly])
Dim myCertificateCollection As X509Certificate2Collection = myKeyStore.Certificates
Dim myCertificate As X509Certificate2 = Nothing
Dim selectedCertificates As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(myCertificateCollection, "Certificaten", "Select een certificaat om te tekenen", X509SelectionFlag.SingleSelection)
If selectedCertificates.Count > 0 Then
Dim certificatesEnumerator As X509Certificate2Enumerator = selectedCertificates.GetEnumerator()
certificatesEnumerator.MoveNext()
myCertificate = certificatesEnumerator.Current
End If
myKeyStore.Close()
'Settings'
Dim source = "source.pdf"
Dim result = "result.pdf"
Dim reason = "test"
Dim Location = "locatie"
Dim myPkcs12Store As New Pkcs12Store()
Using memorystreamPfx As New System.IO.MemoryStream(myCertificate.Export(X509ContentType.Pkcs12))
myPkcs12Store.Load(memorystreamPfx, "")
End Using
For Each strAlias As String In myPkcs12Store.Aliases
If myPkcs12Store.IsKeyEntry(strAlias) Then
Dim pk = myPkcs12Store.GetKey(strAlias).Key
Using myPdfReader As New PdfReader(source)
Using myFileStream As New FileStream(result, FileMode.Create, FileAccess.Write)
Using myPdfStamper As PdfStamper = PdfStamper.CreateSignature(myPdfReader, myFileStream, "0")
Dim myPdfDocument As New Document(myPdfReader.GetPageSizeWithRotation(1))
'Define the digital signature appearance'
Dim myPdfSignatureAppearance As PdfSignatureAppearance = myPdfStamper.SignatureAppearance
myPdfSignatureAppearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED
myPdfSignatureAppearance.Image = Image.GetInstance("Images/poro1_by_justduet-d63wx6c.png")
myPdfSignatureAppearance.Reason = reason
myPdfSignatureAppearance.Location = Location
myPdfSignatureAppearance.SetVisibleSignature(New iTextSharp.text.Rectangle(myPdfDocument.PageSize.Width - 120, 36, myPdfDocument.PageSize.Width - 36, 96), myPdfReader.NumberOfPages, "Digital Signature")
'Attach digital signature to PDF document'
Dim myExternalSignature As IExternalSignature = New PrivateKeySignature(pk, "SHA-256")
MakeSignature.SignDetached(myPdfSignatureAppearance, myExternalSignature, {(myPkcs12Store.GetCertificate(strAlias).Certificate)}, Nothing, Nothing, Nothing, 0, CryptoStandard.CMS)
End Using
End Using
End Using
End If
Next
任何帮助将不胜感激!进一步的问题请问
bdebaere
答案 0 :(得分:0)
对于寻找答案的人:
Dim myX509Store As New X509Store(StoreName.My, StoreLocation.CurrentUser)
myX509Store.Open(OpenFlags.[ReadOnly])
'Dim myCertificateCollection As X509Certificate2Collection = myX509Store.Certificates
Dim myCertificateChain As IList(Of X509Certificate) = New List(Of X509Certificate)()
Dim myCertificate As X509Certificate2 = Nothing
Dim myCertificateCollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(myX509Store.Certificates, "Certificaten", "Select een certificaat om te tekenen", X509SelectionFlag.SingleSelection)
If myCertificateCollection.Count > 0 Then
Dim certificatesEnumerator As X509Certificate2Enumerator = myCertificateCollection.GetEnumerator()
certificatesEnumerator.MoveNext()
myCertificate = certificatesEnumerator.Current
'myCertificate = selectedCertificates(0)
Dim myX509Chain As New X509Chain()
myX509Chain.Build(myCertificate)
For Each myChainElement As X509ChainElement In myX509Chain.ChainElements
myCertificateChain.Add(DotNetUtilities.FromX509Certificate(myChainElement.Certificate))
Next
End If
myX509Store.Close()
Dim ocspClient As IOcspClient = New OcspClientBouncyCastle()
Dim tsaClient As ITSAClient = Nothing
For intI As Integer = 0 To myCertificateChain.Count - 1
Dim cert As X509Certificate = myCertificateChain(intI)
Dim tsaUrl As String = CertificateUtil.GetTSAURL(cert)
If tsaUrl IsNot Nothing Then
tsaClient = New TSAClientBouncyCastle(tsaUrl)
Exit For
End If
Next
Dim crlList As IList(Of ICrlClient) = New List(Of ICrlClient)()
crlList.Add(New CrlClientOnline(myCertificateChain))
'Settings
Dim source = "source.pdf"
Dim result = "result.pdf"
Dim reason = "test"
Dim Location = "locatie"
Using myPdfReader As New PdfReader(source)
Using myFileStream As New FileStream(result, FileMode.Create, FileAccess.Write)
Using myPdfStamper As PdfStamper = PdfStamper.CreateSignature(myPdfReader, myFileStream, "0"c)
Dim myPdfDocument As New Document(myPdfReader.GetPageSizeWithRotation(1))
'Define the digital signature appearance
Dim myPdfSignatureAppearance As PdfSignatureAppearance = myPdfStamper.SignatureAppearance
myPdfSignatureAppearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED
'myPdfSignatureAppearance.Image = Image.GetInstance("Images/poro1_by_justduet-d63wx6c.png")
myPdfSignatureAppearance.Reason = reason
myPdfSignatureAppearance.Location = Location
myPdfSignatureAppearance.SetVisibleSignature(New iTextSharp.text.Rectangle(myPdfDocument.PageSize.Width - 120, 36, myPdfDocument.PageSize.Width - 36, 96), myPdfReader.NumberOfPages, "Digital Signature")
Dim pks As IExternalSignature = New X509Certificate2Signature(myCertificate, DigestAlgorithms.SHA1)
'Attach digital signature to PDF document
'Dim myExternalSignature As IExternalSignature = New PrivateKeySignature(pk, "SHA-256")
MakeSignature.SignDetached(myPdfSignatureAppearance, pks, myCertificateChain, crlList, ocspClient, tsaClient, 0, CryptoStandard.CMS)
End Using
End Using
End Using
唯一的问题是,如果您为每个PDF使用eID,则必须输入PIN码。
bdebaere