使用iText7和Vb.Net签名Pdf时出现MissingMethodException

问题描述 投票:0回答:1

[我正在尝试测试翻译为Vb.Net的iText文档样本,但是在执行pdfSigner.SignDetached(...)时,我得到了下一个奇怪的异常

MissingMethodException:'System.Collections.IEnumeratorOrg.BouncyCastle.Asn1.Asn1Sequence.GetObjects()'

该代码成功读取了pfx证书和源文件,并创建了目标文件,但在最后一步失败,使目标文件损坏:

Imports System.IO
Imports Org.BouncyCastle.Crypto
Imports Org.BouncyCastle.X509
Imports iText.Kernel.Geom
Imports iText.Kernel.Pdf
Imports iText.Signatures
Imports Org.BouncyCastle.Pkcs

Public Class SignHelloWorld
        Public Shared ReadOnly root As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\tmp\itext7\"
        Public Shared ReadOnly DEST As String = root & "results\signatures\chapter01\"
        Public Shared ReadOnly KEYSTORE As String = root & "resources\encryption\myCert.pfx"
        Public Shared ReadOnly SRC As String = root & "resources\pdfs\hello.pdf"
        Public Shared ReadOnly PASSWORD As Char() = "1234".ToCharArray()
        Public Shared ReadOnly RESULT_FILES As String() = {"hello_signed1.pdf", "hello_signed2.pdf", "hello_signed3.pdf", "hello_signed4.pdf"}

        Public Sub Sign(ByVal src As String, ByVal dest As String, ByVal chain As X509Certificate(), ByVal pk As ICipherParameters, ByVal digestAlgorithm As String, ByVal subfilter As PdfSigner.CryptoStandard, ByVal reason As String, ByVal location As String)
            Dim reader As PdfReader = New PdfReader(src)
            Dim signer As PdfSigner = New PdfSigner(reader, New FileStream(dest, FileMode.Create), New StampingProperties())
            Dim rect As Rectangle = New Rectangle(36, 648, 200, 100)
            Dim appearance As PdfSignatureAppearance = signer.GetSignatureAppearance()
            appearance.SetReason(reason).SetLocation(location).SetReuseAppearance(False).SetPageRect(rect).SetPageNumber(1)
            signer.SetFieldName("sig")
            Dim pks As IExternalSignature = New PrivateKeySignature(pk, digestAlgorithm)
            signer.SignDetached(pks, chain, Nothing, Nothing, Nothing, 0, subfilter)
            reader.Close()
        End Sub

        Public Shared Sub Main(ByVal args As String())
            Dim directory As DirectoryInfo = New DirectoryInfo(DEST)
            directory.Create()

            Dim pk12 As Pkcs12Store = New Pkcs12Store(New FileStream(KEYSTORE, FileMode.Open, FileAccess.Read), PASSWORD)
            Dim [alias] As String = Nothing

            For Each a In pk12.Aliases
                [alias] = (CStr(a))
                If pk12.IsKeyEntry([alias]) Then Exit For
            Next

            Dim pk As ICipherParameters = pk12.GetKey([alias]).Key
            Dim ce As X509CertificateEntry() = pk12.GetCertificateChain([alias])
            Dim chain As X509Certificate() = New X509Certificate(ce.Length - 1) {}

            For k As Integer = 0 To ce.Length - 1
                chain(k) = ce(k).Certificate
            Next

            Dim app As SignHelloWorld = New SignHelloWorld()
            app.Sign(SRC, DEST & RESULT_FILES(0), chain, pk, DigestAlgorithms.SHA256, PdfSigner.CryptoStandard.CMS, "Test 1", "Ghent")
            app.Sign(SRC, DEST & RESULT_FILES(1), chain, pk, DigestAlgorithms.SHA512, PdfSigner.CryptoStandard.CMS, "Test 2", "Ghent")
            app.Sign(SRC, DEST & RESULT_FILES(2), chain, pk, DigestAlgorithms.SHA256, PdfSigner.CryptoStandard.CADES, "Test 3", "Ghent")
            app.Sign(SRC, DEST & RESULT_FILES(3), chain, pk, DigestAlgorithms.RIPEMD160, PdfSigner.CryptoStandard.CADES, "Test 4", "Ghent")
        End Sub
End Class
vb.net itext bouncycastle
1个回答
0
投票
据我所知,当前的iText 7开发版本(7.1.11-SNAPSHOT)是针对BouncyCastle 1.8.5编译的,实际上,在BC 1.8.5中,您可以在Asn1Sequence.cs中找到

[Obsolete("Use GetEnumerator() instead")] public IEnumerator GetObjects() { return GetEnumerator(); }

但在BC 1.8.6中,该方法已从Asn1Sequence.cs中删除。

因此,在使用iText 7 v7.1.10或更早版本进行开发时,当前仍请使用BouncyCastle 1.8.5。


顺便说一句,BouncyCastle以仅在微版本更改中引入API重大更改而闻名。其他大多数项目只有在发行版中才删除方法(即使标记为过时的),而在发行版中至少要在次要版本中进行更改,如果不是在主版本中则应进行更改。

因此,在使用BC时,您始终必须非常注意要使用哪个版本。

© www.soinside.com 2019 - 2024. All rights reserved.