iText7多重签名

时间:2017-11-10 07:32:08

标签: itext digital-signature signature itext7

我想在带有iText7的pdf文档中进行多个签名,但问题是当我曾经签名时,它有效。如果我签了两次,第一个签名无效。它看起来像这样:

enter image description here

enter image description here

unsigned PDF

signed PDF

这是我的代码:

@Test
public void testMutiSign() {

    iTextSignerUtil1.SignMultPDF(getBytes(unsignedPath), destPath1);

    iTextSignerUtil2.SignMultPDF(getBytes(destPath1), destPath2);

}


 IExternalSignatureContainer externalP7DetachSignatureContainer = new IExternalSignatureContainer() {
    @Override
    public byte[] sign(InputStream data) throws GeneralSecurityException {


        //byte[] hashData = HashUtil.hash(data , "SHA256");

        byte signData = null;
        signData = signUtil.signP7DetachData(data);

        return signData;

    }


    @Override
    public void modifySigningDictionary(PdfDictionary signDic) {
        signDic.put(PdfName.Filter, PdfName.Adobe_PPKLite);
        signDic.put(PdfName.SubFilter, PdfName.Adbe_pkcs7_detached);
    }
};


public void SignMultPDF(byte[] pdfFile , String destPath , String name , String fname , String value){

    boolean success = false;

    int estimatedSize = 300000;

    while (!success) {
        try {

            PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(pdfFile));
            PdfSigner pdfSigner = new PdfSigner(pdfReader, new FileOutputStream(destPath), true);

          pdfSigner.signExternalContainer(externalP7DetachSignatureContainer, estimatedSize);

            success = true;

        } catch (IOException e) {
            e.printStackTrace();
            estimatedSize += 1000;
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
    }
}

以上是我的所有示例代码。

还有一个问题,我是否想念创建签名字段?

1 个答案:

答案 0 :(得分:2)

此问题的原因是original file的一个怪癖:它有一个空的间接字典,它既用作信息字典又用作概述词典

每当操纵PDF iText更新其信息字典时,特别是 ModDate 每次都可能更改。

因此,在每次签名过程中,信息字典都会更改,并且当共享字典对象时,概述字典也是如此。但是,不允许更改已签名PDF的大纲。因此,不允许在创建第二个签名期间更改信息(和大纲),并使第一个签名无效。

This fileoriginal file几乎相同,唯一的区别是它为信息信息使用不同的空字典(如文件中当前未使用的空间接字典,我只需要将信息轮廓的引用更改为此未使用的对象)。两次签署此文件时,Adobe不会再抱怨了。

这是iText或PDF中的错误吗?我认为iText在更改 Info 字典时应该使用新的间接对象编号,因为在PDF中通常不禁止使用字典的重复使用。但是利用这样的PDF 重复的间接对象使用真的是在寻找麻烦。所以我认为这个问题带来了iText问题和PDF中的问题。