iText PdfStamper增加签名pdf的大小 [英] iText PdfStamper increase size of a signed pdf

查看:1873
本文介绍了iText PdfStamper增加签名pdf的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么PdfStamper每次使用都会增加我签名的pdf的大小?



这是我的代码:

  private static void test(String src,String pwd,String dest)throws Exception {
byte [] pwdByte = pwd!= null? pwd.getBytes():null;
PdfReader r = null;
PdfStamper stp = null;
FileOutputStream fos = null;
try {
r = new PdfReader(src,pwdByte);
fos = new FileOutputStream(dest);
stp = new PdfStamper(r,fos,'\ 0',true);
} finally {
stp.close();
fos.close();
r.close();
}
}

如果我调用test,则生成的pdf会增加大小取决于签名信息:




  • 我的初始pdf有LTV标志,但我需要添加时间戳。我需要添加LRv()一些pdf,然后添加addLtvNoTs(),因为它是一个不同的TSA。这使得在模式中调用两次PdfReader并且每次我这样做都会增加我的pdf 190kb aprox。这意味着增加380kb。



    假设您想要添加第四个签名,并且您希望生成的文件具有与之前相同的大小,您可以必须更改现有文档中的一些字节。这将打破至少一个现有签名。



    你提到LTV。 LTV意味着添加文档安全存储(DSS)和时间戳签名。同样的规则适用:你必须添加额外的字节,因为你改变现有字节的那一刻,你将打破现有的签名:





    每次最新时间戳签名的证书到期时,您都必须重复相同的步骤:





    总文件大小将始终增加。


    Why PdfStamper increase the size of my signed pdfs every i use?

    That's my code:

    private static void test(String src, String pwd, String dest) throws Exception {
        byte[] pwdByte = pwd != null ? pwd.getBytes() : null;
        PdfReader r = null;
        PdfStamper stp = null;
        FileOutputStream fos = null;
        try {
            r = new PdfReader(src, pwdByte);
            fos = new FileOutputStream(dest);
            stp = new PdfStamper(r, fos, '\0', true);
        } finally {
            stp.close();
            fos.close();
            r.close();
        }
    }
    

    If i call test, the resulting pdf increases the size depending on the signed info:

    • My initial pdf has a LTV sign but i need to add a timestamp. I need to addLtv() some pdfs and later addLtvNoTs() because it's a different TSA. This makes calling twice PdfReader in mode append and every time i do it increases my pdf in 190kb aprox. That means increasing it 380kb. http://developers.itextpdf.com/question/how-enable-ltv-timestamp-signature

    • I've tested calling above test method with a pdf with a simple sing and every time i call test() on the resulting pdf it increase 3,5kb each time (i think due to small sign info).

    Why pdfstamper increase my pdf size if it's signed even i don't add any change? How can i avoid it? 390kb it's a problem because i need to resign thousand of pdfs and store them.

    I'm using itext 5.5.9

    Thanks in advance!

    解决方案

    This is a strange question because it reveals that you don't understand the concept of a digital signature. When you sign a PDF, you want to make sure that the signature breaks as soon as someone changes a single byte. Every time you sign a document, you protect the bytes of the previous revision of the document by adding a signature dictionary of which the contents contain a signed hash of at least the bytes you want to protect.

    See the following figure that shows a schema of a PDF that is signed three times:

    Suppose that you would want to add a fourth signature, and you would want the resulting file to have the same size as before, you would have to change some of the bytes in the existing document. That would break at least one of the existing signatures.

    You mention LTV. LTV implies adding a Document Security Store (DSS) and a Timestamp signature. The same rules apply: you have to add extra bytes, because the moment you change existing bytes, you'll break an existing signature:

    Every time the certificate of the latest timestamp signature expires, you have to repeat the same procedure:

    The total file size will always increase.

    这篇关于iText PdfStamper增加签名pdf的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆