Pdf 客户端通过 ITextSharp 签名

Pdf client side signing via ITextSharp

我想通过 iTextSharp 模拟客户端签名。

我有有效的证书,导出为 2 个文件 - .cer 文件(public 密钥)和 .pfx 文件(带有私钥),pfx 文件用作密钥库。 Public 密钥用于 "server"。

我还有一个 pdf 文件 - 例如 https://yadi.sk/i/6vuDlEPXi7oYz. My code copied in this gist: https://gist.github.com/alex-t0/f446ccb5ca5e8936b778

简而言之,有3种方法:

但是这段代码生成的是pdf文档,签名无效。当使用方法 MakeSignature.SignDetached 签名在 pdf 中有效时,一切正常。

如何调试并发现问题?或者可能是 pdf 客户端签名的其他示例?与服务器部分,在 c# 中。

您在 GetEncodedPKCS7 调用中使用了错误的摘要。

首次构建您(正确)使用的经过身份验证的属性时

sgn.getAuthenticatedAttributeBytes(messageHash, now, null, null, CryptoStandard.CMS)

但稍后在实际构建 PKCS7 签名容器时,您会做

result.Sign.GetEncodedPKCS7(result.Hash, result.Now, null, null, null, CryptoStandard.CMS);

这两个调用的参数必须相同(后一个调用中的附加 ITSAClient 参数除外)。否则,最终经过身份验证的属性(内置于 GetEncodedPKCS7)与原始属性(内置于 getAuthenticatedAttributeBytes)不同,并且需要不同的签名值。

因此,您应该在 DTO 中包含 byte[] messageHash 并在 SaveSignedDocumentOnServer 中使用它。