使用iText7 C#签署pdf

时间:2018-08-27 13:06:28

标签: c# itext7

我在文件中有一个私钥,例如。 “ privatekey.pem”,并且不知道如何创建ICipherParameters pk = ;,有人可以帮助我吗? 我正在尝试使用iText7签署pdf。

using System;
using System.Drawing;
using System.Windows.Forms;
using Org.BouncyCastle.Crypto;
using System.Security.Cryptography.X509Certificates;
using iText.Signatures;
using System.IO;
using iText.Kernel.Pdf;
using Org.BouncyCastle.X509;
/* ..... */

    protected void SignTest2()
    {
        bool isAppendMode = false;
        string dest = "signtest.pdf";
        string source = "test.pdf";
        int certificationLevel = 1;
        string reason = "Test reason";
        string location = "Warsaw";
        bool setReuseAppearance = false;
        string name = "Test name";


        ICipherParameters pk = ;

        System.Security.Cryptography.X509Certificates.X509Certificate cert = new System.Security.Cryptography.X509Certificates.X509Certificate("fullchain.pem");
        X509Certificate2 signatureCert = new X509Certificate2(cert);
        Org.BouncyCastle.X509.X509Certificate bcCert = new X509CertificateParser().ReadCertificate(cert.GetRawCertData());

        Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[1] { bcCert };

        PdfReader reader = new PdfReader(source);
        StampingProperties properties = new StampingProperties();
        if (isAppendMode)
        {
            properties.UseAppendMode();
        }
        PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), true);
        signer.SetCertificationLevel(certificationLevel);
        PdfSignatureAppearance appearance = signer.GetSignatureAppearance().SetReason(reason).SetLocation(location
            ).SetReuseAppearance(setReuseAppearance);

        signer.SetFieldName(name);
        // Creating the signature
        IExternalSignature pks = new PrivateKeySignature(pk, "SHA-512");
        signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
    }

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案。如果您在“ privkey.pem”文件中仅包含私钥,则可以使用它。

    private static AsymmetricKeyParameter readPrivateKey(string privateKeyFileName)
    {
        AsymmetricKeyParameter key;

        using (var reader = File.OpenText(privateKeyFileName))
        {
            PemReader pemReader = new PemReader(reader);
            key = (AsymmetricKeyParameter)pemReader.ReadObject();

        }
        return key;
    }

    protected void SignTest2()
    {
        bool isAppendMode = false;
        string dest = "signtest.pdf";
        string source = "test.pdf";
        int certificationLevel = 1;
        string reason = "Test reason";
        string location = "Warsaw";
        bool setReuseAppearance = false;
        string name = "Test name";


        //ICipherParameters pk = Pkcs12FileHelper.ReadFirstKey("privkey.pem", null, null);
        ICipherParameters pk = readPrivateKey("privkey.pem");
        System.Security.Cryptography.X509Certificates.X509Certificate cert = new System.Security.Cryptography.X509Certificates.X509Certificate("fullchain.pem");
        X509Certificate2 signatureCert = new X509Certificate2(cert);
        Org.BouncyCastle.X509.X509Certificate bcCert = new X509CertificateParser().ReadCertificate(cert.GetRawCertData());

        Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[1] { bcCert };
        //ICipherParameters pk = signatureCert.GetECDsaPrivateKey();

        PdfReader reader = new PdfReader(source);
        StampingProperties properties = new StampingProperties();
        if (isAppendMode)
        {
            properties.UseAppendMode();
        }
        PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), true);
        signer.SetCertificationLevel(certificationLevel);
        PdfSignatureAppearance appearance = signer.GetSignatureAppearance().SetReason(reason).SetLocation(location
            ).SetReuseAppearance(setReuseAppearance);

        signer.SetFieldName(name);
        // Creating the signature
        IExternalSignature pks = new PrivateKeySignature(pk, "SHA-512");
        signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
    }