使用timestamp itextSharp 5.x签名pdf

时间:2017-04-04 13:44:59

标签: java itext

我需要添加ocsp,它看起来像版本5.x并且与版本4.x不同

在版本4中要求在此新请求中发送日期以发送字节类型这些将发送的数据

byte[] ocsp = null;
byte[] sh = signature.getAuthenticatedAttributeBytes(hashByte, ocsp, null, CryptoStandard.CMS);

我还需要添加TSAClientBouncyCastle,但是我正在使用准备pdf以便稍后订阅的选项。

我的签名类是下一个,我有一个方法,我准备外部签名的文件,两个服务restm客户端和服务器。

我向服务器请求要签名的文件的哈希值,我签名并将其返回给服务器以添加到pdf。

我只是不知道如何填写ocsp和tsclient

class PDFSigner {
    private List<Org.BouncyCastle.X509.X509Certificate> chain = new List<Org.BouncyCastle.X509.X509Certificate>();

    public void certificate(List<string> certificate)
    {
        Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
        foreach (string cert in certificate)
        {
            this.chain.Add(cp.ReadCertificate(Convert.FromBase64String(cert)));
        }

    }

    public string prepared(string file)
    {
        try
        {

            FileStream signedPdf = new FileStream(file.Substring(0, file.Length - 4) + "_signed.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            PdfReader reader = new PdfReader(file);
            PdfStamper stamper = PdfStamper.CreateSignature(reader, signedPdf, '\0');


            // Criar a assinatura aparente
            PdfSignatureAppearance appearance = stamper.SignatureAppearance;
            appearance.Reason = "Because I can";
            appearance.Location = "My location";
            appearance.SetVisibleSignature(new Rectangle(100, 100, 350, 150), reader.NumberOfPages, "Signature");
            appearance.SignatureGraphic = Image.GetInstance("C:\\signature.png");
            appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
            appearance.Certificate = this.chain[0];            

            PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
            dic.Reason = appearance.Reason;
            dic.Location = appearance.Location;
            dic.Contact = appearance.Contact;
            dic.Date = new PdfDate(appearance.SignDate);
            appearance.CryptoDictionary = dic;
            Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>();
            exc.Add(PdfName.CONTENTS, (int)(8192 * 2 + 2));
            appearance.PreClose(exc);
            PdfPKCS7 signature = new PdfPKCS7(null, this.chain, "SHA1", false);

            Stream data = appearance.GetRangeStream();
            byte[] hashByte = DigestAlgorithms.Digest(data, "SHA1");

            DateTime cal = DateTime.Now;
            byte[] ocsp = null;

            byte[] sh = signature.getAuthenticatedAttributeBytes(hashByte, ocsp, null, CryptoStandard.CMS);

            string hash = Convert.ToBase64String(hashByte);
            string key = hash;

            if (Variable.appearance.ContainsKey(key))
                Variable.appearance.Remove(key);                

            if (Variable.signature.ContainsKey(key))
                Variable.signature.Remove(key);



            Variable.appearance.Add(key, appearance);
            Variable.signature.Add(key, signature);

            return hash;
        }
        catch (Exception e)
        {
            throw new System.Exception(e.Message);
        }

    }

    public Contracts.File savePdf(Contracts.File file)
    {
        try
        {
            byte[] signByte = Convert.FromBase64String(file.signature);

            PdfSignatureAppearance appearance = Variable.appearance[file.hash];
            PdfPKCS7 signature = Variable.signature[file.hash];

            signature.SetExternalDigest(signByte, null, "RSA");
            byte[] encodedSig = signature.GetEncodedPKCS7();

            byte[] paddedSig = new byte[8192];
            Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length);
            PdfDictionary dic = new PdfDictionary();
            dic.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true));

            appearance.Close(dic);

            Variable.appearance.Remove(file.hash);
            Variable.signature.Remove(file.hash);
            //verifySignature(file.name, "");

            return file;
        }
        catch (Exception e)
        {

            throw new System.Exception(e.Message);
        }
    } 
}

0 个答案:

没有答案