Home > php教程 > php手册 > php 使用openssl_verify验证签名实例程序

php 使用openssl_verify验证签名实例程序

WBOY
Release: 2016-06-13 09:50:27
Original
1430 people have browsed it

下面我们一起来看看关于php 使用openssl_verify验证签名实例程序,希望本文章对各位同学会有所帮助。

 代码如下 复制代码


/**
 
* 验证签名
 
* TobeVerified 待验证签名的密文
 
* PlainText 待验证签名的明文
 
* CertFile 签名者公钥证书
 
* return 验证成功返回true,失败返回false(从LastErrMsg属性获取失败原因)
 
*/
function VerifyMsg($TobeVerified, $PlainText, $CertFile,$signature_alg=OPENSSL_ALGO_SHA1)
{
 
   
//用公钥验签
    $fp=fopen($CertFile,"r");
    if(!$fp)
    {
       
//echo "Error Number:-10005, Error Description: ER_FIND_CERT_FAILED(找不到证书)";
        return false;
    }
    $pub_key=fread($fp,8192);
    fclose($fp);
    $res = openssl_get_publickey($pub_key);
    if (1==openssl_verify($PlainText,pack("H" . strlen($TobeVerified), $TobeVerified) , $res,$signature_alg))
    {
       
//print("www.bKjia.c0m提示您:验证成功"."
");
        return true;
    }
    else
    {
       
//echo "Error Number:-10021, Error Description: ER_VERIFY_ERROR(验签失败)|".openssl_error_string();
        return false;
    }
 
}
 
?>

openssl_verify 可能有三个返回值1,0,-1,只有返回1表示验证签名成功。

$signature_alg 默认OPENSSL_ALGO_SHA1 ,如果是DSA加密要设置成OPENSSL_ALGO_DSS1

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template