A .cer doesn't have a private key.
Paulo
> -----Original Message-----
> From: merco [mailto:[email protected]]
> Sent: Tuesday, October 06, 2009 4:05 PM
> To: [email protected]
> Subject: [iText-questions] Sign with .cer
>
>
> Hi. I'm using VB.net Itextsharp 4.1.6.
>
> I want to sign a PDF but i get an error in
> "sap.PreClose(exc)" (invalid
> cast)
> this is the code (taken from How to sign a PDF using
> iText.mht .net 2.0)
> but i can't understand why.
>
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> Dim FCert As String = "\\sypdc\transito\x
> davide\ItEXTtEST\Firma\SYGEST_Administrator_21_10_2009.cer"
> Dim FromF As String = "\\sypdc\transito\x
> davide\ItEXTtEST\Firma\Progettista.PDF"
> Dim Tof As String = "\\sypdc\transito\x
> davide\ItEXTtEST\Firma\ProgettistaFIRM.PDF"
>
>
> Dim Card As X509Certificate2 = New
> X509Certificate2(X509Certificates.X509Certificate2.CreateFromC
> ertFile(FCert))
>
>
> Dim ms As IO.MemoryStream = New IO.MemoryStream(Card.RawData)
>
> Dim bcCert As Org.BouncyCastle.X509.X509Certificate = New
> Org.BouncyCastle.X509.X509Certificate(Org.BouncyCastle.Asn1.X5
> 09.X509CertificateStructure.GetInstance(New
> Org.BouncyCastle.Asn1.Asn1InputStream(ms).ReadObject()))
>
> Dim chain() As Org.BouncyCastle.X509.X509Certificate = New
> Org.BouncyCastle.X509.X509Certificate(1) {}
> chain(0) = bcCert
>
>
>
> Dim reader As PdfReader = New PdfReader(FromF)
> If IO.File.Exists(Tof) Then
> IO.File.Delete(Tof)
> End If
> Dim stp As PdfStamper = PdfStamper.CreateSignature(reader, New
> IO.FileStream(Tof, IO.FileMode.CreateNew), PdfWriter.VERSION_1_6)
>
> Dim sap As PdfSignatureAppearance = stp.SignatureAppearance
> 'sap.SetVisibleSignature(New Rectangle(100, 100, 300, 200), 1,
> Nothing)
> With sap
> .SetVisibleSignature("Progettista")
> .SignDate = DateTime.Now
> .SetCrypto(Nothing, chain, Nothing, Nothing)
> .Reason = "I like to sign"
> .Location = "Universe"
> .Acro6Layers = True
> .Render =
> PdfSignatureAppearance.SignatureRender.NameAndDescription
> End With
>
>
>
> Dim dic As PdfSignature = New
> PdfSignature(PdfName.ADOBE_PPKLITE,
> PdfName.ADBE_PKCS7_DETACHED)
> With dic
> .Date = New PdfDate(sap.SignDate)
> .Name = PdfPKCS7.GetSubjectFields(chain(0)).GetField("CN")
> If (sap.Reason <> "") Then
> .Reason = sap.Reason
> End If
> If (sap.Location <> "") Then
> .Location = sap.Location
> End If
> End With
> sap.CryptoDictionary = dic
>
> Dim csize As Int64 = 4000
> Dim exc As Hashtable = New Hashtable()
> exc(PdfName.CONTENTS) = csize * 2 + 2
> sap.PreClose(exc)
>
>
> Dim s As IO.Stream = sap.RangeStream
> Dim ss As IO.MemoryStream = New IO.MemoryStream()
> Dim read As Int64 = 0
> Dim buff As Byte() = New Byte(8191) {}
> read = s.Read(buff, 0, 8192)
>
> While ((read) > 0)
> ss.Write(buff, 0, read)
> read = s.Read(buff, 0, 8192)
> End While
> Dim pk() As Byte = SignMsg(ss.ToArray(), Card, True)
>
> Dim outc As Byte() = New Byte(csize - 1) {}
>
>
> Dim dic2 As PdfDictionary = New PdfDictionary()
>
> Array.Copy(pk, 0, outc, 0, pk.Length)
>
> dic2.Put(PdfName.CONTENTS, New
> PdfString(outc).SetHexWriting(True))
> sap.Close(dic2)
>
> reader.Close()
> stp.Close()
>
>
>
>
> End Sub
> Public Shared Function SignMsg(ByVal msg As Byte(), ByVal
> signerCert As
> X509Certificate2, ByVal detached As Boolean) As Byte()
> ' Place message in a ContentInfo object.
> ' This is required to build a SignedCms object.
>
> Dim contentInfo As
> System.Security.Cryptography.Pkcs.ContentInfo =
> New System.Security.Cryptography.Pkcs.ContentInfo(msg)
>
> ' Instantiate SignedCms object with the ContentInfo above.
> ' Has default SubjectIdentifierType IssuerAndSerialNumber.
> Dim signedCms As
> System.Security.Cryptography.Pkcs.SignedCms = New
> SignedCms(contentInfo, detached)
>
> ' Formulate a CmsSigner object for the signer.
> Dim cmsSigner As CmsSigner = New CmsSigner(signerCert)
>
> ' Include the following line if the top certificate in the
> ' smartcard is not in the trusted list.
> cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly
>
> ' Sign the CMS/PKCS #7 message. The second argument is
> ' needed to ask for the pin.
> signedCms.ComputeSignature(cmsSigner, False)
>
> ' Encode the CMS/PKCS #7 message.
> Return signedCms.Encode()
> End Function
>
>
> thank you
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta transmissão desta
mensagem não significa a perca de confidencialidade. Se esta mensagem for
recebida por engano, por favor envie-a de volta para o remetente e apague-a do
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de
usar, revelar ou distribuir qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may contain
confidential or legally protected information. The incorrect transmission of
this message does not mean the loss of its confidentiality. If this message is
received by mistake, please send it back to the sender and delete it from your
system immediately. It is forbidden to any person who is not the intended
receiver to use, distribute or copy any part of this message.
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/