0
嘗試驗證PDF簽名不起作用。 PDF由Adobe Acrobat簽名,然後嘗試使用客戶端證書的公鑰進行驗證。C#驗證PDF簽名
因此,我得到客戶端證書的公鑰,散列PDF並驗證哈希是否等於pdf簽名,但失敗。
HttpClientCertificate cert = request.ClientCertificate;
X509Certificate2 cert2 = new X509Certificate2(cert.Certificate);
PdfReader pdfreader = new PdfReader("path_to_file");
AcroFields fields = pdfreader.AcroFields;
AcroFields.Item item = fields.GetFieldItem("Signature1");
List<string> names = fields.GetSignatureNames();
foreach (string name in names){
PdfDictionary dict = fields.GetSignatureDictionary(name);
PdfPKCS7 pkcs7 = fields.VerifySignature(name);
Org.BouncyCastle.X509.X509Certificate pdfSign = pkcs7.SigningCertificate;
// Get its associated CSP and public key
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert2.PublicKey.Key;
// Hash the data
SHA256 sha256 = new SHA256Managed();
byte[] pdfBytes = System.IO.File.ReadAllBytes("path_to_pdf");
byte[] hash = sha256.ComputeHash(pdfBytes);
// Verify the signature with the hash
bool ok = csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA256"), pdfSing.GetSignature());
}
優秀的答案!謝謝 – A77ak
@mkl你有什麼想法,我們怎樣才能得到pdf的字節除了簽名值?這樣我們可以重新計算簽名pdf中的哈希值? –
@emrenevayeshirazi你可以簡單地採用iText'AcroFields'類的代碼,看看它的'verifySignature'方法和''updateByteRange''方法。 – mkl