0

我想在我的 PDF 文档中添加时间戳(没有数字签名)。我怎样才能做到这一点?

我可以使用 Itext 进行数字签名(我这里有 TSAClient):

MakeSignature.signDetached(appearance, digest, signature, chain, null, null, tsa, 0, subfilter);

但是如何在没有数字签名的情况下做类似的事情?使用 Bouncy Castle 或 Itext 或 Pdfbox... 或与另一个库..

4

3 回答 3

4

在 iText 您正在寻找

LtvTimestamp.timestamp(appearance, tsa, signatureName);

另请参阅。JavaDoc 文档:

/**
 * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
 * @param sap the signature appearance
 * @param tsa the timestamp generator
 * @param signatureName the signature name or null to have a name generated
 * automatically
 * @throws DocumentException 
 * @throws IOException 
 * @throws GeneralSecurityException
 */

您可能需要阅读第 5.4.1 节在PDF 文档的数字签名中添加文档安全存储 (DSS) 和文档级时间戳以在上下文中使用。

请注意,旧的 PDF 查看器无法正确识别文档级时间戳,因为他们最近才进入 PDF 世界,即使用PAdES-4

于 2013-06-24T12:15:54.357 回答
2

要使用 PDFBox 做到这一点,您需要一些简单的 SignatureInterface 实现,如下所示:

public class TimestampSignatureImpl implements SignatureInterface {
    private TSAClient tsaClient;
    public TimestampSignatureImpl(TSAClient tsaClient) {
        super();
        this.tsaClient = tsaClient;
    }
    @Override
    public byte[] sign(InputStream paramInputStream) throws IOException {
        return tsaClient.getTimeStampToken(IOUtils.toByteArray(paramInputStream));
    }
}

和一些像这样的PDSignature:

PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); 
signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161"));
signature.setSignDate(Calendar.getInstance());

然后像这样签署你的pdf:

PDDocument pdf = PDDocument.load(inputFile);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
TSAClient tsaClient = new TSAClient(new URL("your time stamp authority"), null, null, digest);
pdf.addSignature(signature, new TimestampSignatureImpl(tsaClient));
pdf.saveIncremental(new FileOutputStream(outputFile));
pdf.close();

PS:TSAClient 取自 PDFBox 示例。

于 2016-11-02T16:59:53.837 回答
0

使用您可以通过调用以下类方法iText7来添加DTS (文档时间戳)PdfSigner

ITSAClient tsa = new TSAClientBouncyCastle(tsaUrl, tsaUser, tsaPass);
pdfSigner.timestamp(tsa, "SignatureTimeStamp");

或者

ITSAClient tsa = new TSAClientBouncyCastle(tsaUrl, tsaUser, tsaPass, 8192, "SHA-256"); 
pdfSigner.timestamp(tsa, "SignatureTimeStamp");

另外,java文档

/**
 * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
 * NOTE: This method closes the underlying pdf document. This means, that current instance
 * of PdfSigner cannot be used after this method call.
 *
 * @param tsa           the timestamp generator
 * @param signatureName the signature name or null to have a name generated
 *                      automatically
 * @throws IOException
 * @throws GeneralSecurityException
 */
于 2021-06-11T19:06:54.213 回答