在 Java 中使用自定义数字签名对 Pdf 进行签名

发布于 2024-11-17 18:10:30 字数 254 浏览 3 评论 0原文

我使用 Sun 的代码创建了一个数字签名:http://下载.oracle.com/javase/tutorial/security/apisign/examples/GenSig.java。如何使用此数字签名对 pdf 文件进行签名?那就是:如何将签名添加到pdf文件中?

I've created a digital signature using Sun's code:http://download.oracle.com/javase/tutorial/security/apisign/examples/GenSig.java. How can I sign a pdf file using this digital signature? That is: how can I add the signature to a pdf file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-11-24 18:10:30

你采取了错误的方法。您无法轻松地将签名附加到 PDF 中。 PDF 规范包括对数字签名的支持。这些签名被嵌入到文档中,并且它们的计算是一个不平凡的过程。

你最好的选择是放弃当前的实现并采取例如。 iText 库或我们的 SecureBlackbox 将为您完成这项工作。

如果您的任务是自己实现签名,那么请获取 PDF 规范,阅读它并实现其中的相应部分(在这种情况下,您的问题太宽泛,无法以 StackOverflow 格式回答)。

You've taken the wrong approach. You can't easily append the signature to the PDF. PDF specification includes support for digital signatures. Those signatures are embedded into the document, and their calculation is a non-trivial process.

Your best bet is to drop your current implementation and take eg. iText library or our SecureBlackbox which will do the job for you.

If your task is to implement signature yourself, then take PDF specification, read it and implement the corresponding pieces of it (and in this case your question is way too broad to be answered in StackOverflow format).

九八野马 2024-11-24 18:10:30

将数字签名添加到 PDF 文件涉及的步骤:

(I) 创建模板 PDFDocument:

创建带有模板签名的 PDFDocument:
PDSignature pdSignature,
pdSignature.setByteRange(new int[]{0, 0, 0, 0});
pdSignature.setContents(new byte[n*1024]);

其中n是整数,即kbs的倍数。

注意:内容大小应大于或等于签名和证书文件的长度之和。

(II) 更新模板 PDF 文档:

(a) 更新 /ByteRange[abcd]:
(i) a=“%PDF”中%的偏移量(默认=0)
(ii) b= 的偏移量<在“/Contents<000...000>”
(iii)c=>的偏移量在“/Contents<000...000>”
(iv) d= F 在“%%EOF”中的偏移量-c

(b) 更新外部参照部分:
更新指定对象位置的交叉引用表(外部参照部分)和

(c) 更新起始外部参照部分:
更新startxref,它是交叉引用表(xref)开始的偏移量。

(三)生成更新后的模板文档的数字签名:
生成不包括“/Contents<000...000>”的签名数据(“000...000”)的更新模板文档

(IV) 更新内容<>部分:
替换“/Contents<000...000>”中签名数据长度的第一个/初始“0”带有模板 PDF 文件的签名数据(已封装)。

建议:

使用PDFBox的SignatureInterface:

(a)实现SignatureInterface来调用sign()方法
(b) 提供输入、输出文件、密钥库、别名、引脚
(c)保存增量

(或)使用任何java pdf库(如iText...)

(或)自己在java中实现步骤I-IV。

Steps involved in Adding Digital Signature to a PDF File:

(I) Create Template PDFDocument :

Create PDFDocument with template signature:
PDSignature pdSignature,
pdSignature.setByteRange(new int[]{0, 0, 0, 0});
pdSignature.setContents(new byte[n*1024]);

where n is an integer, ie multiple of kbs.

Note: Content Size should be greater than or equal to sum of length of Signature and Certificate File.

(II) Update the Template PDF Docuement :

(a) Update /ByteRange[a b c d]:
(i) a= Offset of % in "%PDF"(=0, by default)
(ii) b= Offset of < in "/Contents<000...000>"
(iii) c= Offset of > in "/Contents<000...000>"
(iv) d= Offset of F in "%%EOF"-c

(b) update xref section:
update the cross-reference table(xref section), that specifies the position of the objects and

(c) Update startxref section:
update startxref, which is the offset of start of cross-reference table(xref).

(III) Generate Digital Signature of the Updated Template Document:
Generate of the Updated Template Document Excluding the Signed Data ("000...000) of "/Contents<000...000>"

(IV) Update Content<> Section:
Substitute First/Initial "0"s of Signed Data length in "/Contents<000...000>" with Signed Data (Enveloped) of template PDFFile.

Suggestions:

Use SignatureInterface of PDFBox :

(a) implement SignatureInterface to call sign() method
(b) supply the input, output files, keystore, alias, pin
(c) do save incrment

(or) use any java pdf library (like iText...)

(or) Implement the Steps I-IV in java yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文