加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

用PHP验证签名的PDF文档

发布时间:2020-12-13 22:52:16 所属栏目:PHP教程 来源:网络整理
导读:我有一份签名的PDF文件.它是使用TCPDF签署的.现在我想验证它.这是我的解决方案: 获取已签名pdf的内容. 根据/ ByRange字段获取原始内容和签名值. 从签名值获取加密摘要消息.它是签名值末尾的八位字符串. 使用Openssl_public_decrypt()函数使用公钥解密加密的
我有一份签名的PDF文件.它是使用TCPDF签署的.现在我想验证它.这是我的解决方案:

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

我的问题是在最后一步,当我将D1与D2进行比较时,它们并不相等.我不知道为什么.
谢谢你的帮助.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读