Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用iText7和Vb.Net签署Pdf时缺少MethodException_Vb.net_Itext_Bouncycastle - Fatal编程技术网

使用iText7和Vb.Net签署Pdf时缺少MethodException

使用iText7和Vb.Net签署Pdf时缺少MethodException,vb.net,itext,bouncycastle,Vb.net,Itext,Bouncycastle,我试图测试翻译成Vb.Net的iText文档示例,但在执行pdfSigner.SignDetached(…)时,我遇到了下一个奇怪的异常 MissingMethodException:'System.Collections.IEnumerator Org.BouncyCastle.Asn1.Asn1Sequence.GetObjects()' 代码成功读取pfx证书和源文件,并创建目标文件,但在最后一步失败,导致目标文件损坏: Imports System.IO Imports Org.Boun

我试图测试翻译成Vb.Net的iText文档示例,但在执行pdfSigner.SignDetached(…)时,我遇到了下一个奇怪的异常

MissingMethodException:'System.Collections.IEnumerator Org.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

据我所知,当前的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时,您必须始终非常注意使用哪个确切版本。

据我所知,当前的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时,您必须始终非常注意要使用的确切版本。

通常在使用不同版本的DLL时会遇到这种情况,而不是使用构建代码时使用的版本,运行时发现该DLL没有引用的方法。您是否使用了任何绑定重定向来消除有关“定位的程序集与清单不匹配”的错误消息?My App.Config为BouncyCastle.Crypto提供了一个“bindingRedirect oldVersion=“0.0.0.0-1.8.6.0”newVersion=“1.8.6.0”,该版本实际上已经是1.8.6.0。我试图删除它,但我得到了相同的异常。好的,当它得到这个异常时,这个代码在哪里运行?在整个机器上搜索包含bouncy castle crypto的DLL名称(您可以在参考资料中进行检查)-您找到了哪些版本的DLL?有旧的吗?可能是其中一个旧版本(例如1.7)导致了问题,因为它们不包含该方法,但应用程序仍在查找并尝试使用旧的dll?你是对的,该代码在另一台具有过时版本的BouncyCastley的计算机上成功运行。通常,当使用与生成代码所用的dll版本不同的dll时,运行库发现的DLL没有引用的方法。您是否使用了任何绑定重定向来消除有关“定位的程序集与清单不匹配”的错误消息?My App.Config为BouncyCastle.Crypto提供了一个“bindingRedirect oldVersion=“0.0.0.0-1.8.6.0”newVersion=“1.8.6.0”,该版本实际上已经是1.8.6.0。我试图删除它,但我得到了相同的异常。好的,当它得到这个异常时,这个代码在哪里运行?在整个机器上搜索包含bouncy castle crypto的DLL名称(您可以在参考资料中进行检查)-您找到了哪些版本的DLL?有旧的吗?这些旧dll(例如1.7)中的一个可能会导致问题,因为它们不包含该方法,但应用程序正在查找并尝试使用旧dll?你是对的,该代码在另一台具有过时版本的BouncyCastlecan的计算机上成功运行。请帮助:@Buttanivjay我对那里的元数据细节不太了解。如果您分析并显示pdf文件中的实际差异,我可能有助于使itext输出类似于acrobat输出。但由于只有该网站的输出,我不知道需要更改什么。谢谢您的回复。我刚刚更新了我的问题并附上了两份PDF。你检查过了吗?我目前正忙于一个项目。下周我可能会有时间。你能帮忙吗:@Buttivijay我对那里的元数据细节不太了解。如果您分析并显示pdf文件中的实际差异,我可能有助于使itext输出类似于acrobat输出。但由于只有该网站的输出,我不知道需要更改什么。谢谢您的回复。我刚刚更新了我的问题并附上了两份PDF。你检查过了吗?我目前正忙于一个项目。也许下周我会抽出时间。