在 PHP 中验证签名的 PDF 文档 [英] Verify signed PDF Document in PHP

查看:50
本文介绍了在 PHP 中验证签名的 PDF 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个签名的 PDF 文档.它是使用 TCPDF 签名的.现在我想验证一下.这是我的解决方案:

I have a signed PDF document. It was signed by using TCPDF. Now I want to verify it. This is my solution:

  • 获取已签名 pdf 的内容.
  • 根据/ByRange 字段获取原始内容和签名值.
  • 从签名值中获取加密的摘要消息.它是签名值末尾的八位字节字符串.
  • 使用 Openssl_public_decrypt() 函数用公钥解密加密的摘要消息.然后我们有一个带有前缀的字符串(3021300906052b0e03021a05000414").此前缀表示使用的哈希函数是 SHA-1.去掉前缀后,我们得到摘要消息 D1.
  • 使用 SHA1() 函数对原始内容进行散列,我们得到摘要消息 D2.
  • 比较 D1 和 D2.如果 D1 = D2,则签名有效,反之亦然.

我的问题是在最后一步,当我比较 D1 和 D2 时,它们不相等.我不知道为什么.感谢您的帮助.

My problem is in last step, when I compare D1 with D2, they are not equal. I don't know why. Thanks for any help.

推荐答案

You should try based on following example
<?php
// $data and $signature are assumed to contain the data and the signature

// fetch public key from certificate and ready it
$pubkeyid = openssl_pkey_get_public("file://src/openssl-0.9.6/demos/sign/cert.pem");

// state whether signature is okay or not
$ok = openssl_verify($data, $signature, $pubkeyid);
if ($ok == 1) {
    echo "good";
} elseif ($ok == 0) {
    echo "bad";
} else {
    echo "ugly, error checking signature";
}
// free the key from memory
openssl_free_key($pubkeyid);
?>
more Examples ad explanation
 http://www.php.net/manual/en/function.openssl-verify.php

这篇关于在 PHP 中验证签名的 PDF 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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