PHP uses openssl_verify to verify signature example program_PHP tutorial

WBOY
Release: 2016-07-13 10:48:09
Original
2568 people have browsed it

Let’s take a look at the example program for using openssl_verify to verify signatures in PHP. I hope this article will be helpful to all students.

The code is as follows
 代码如下 复制代码


/**

* 验证签名

* 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;
    }
 
}
 
?>

Copy code


/**

* Verify signature

* TobeVerified The ciphertext of the signature to be verified

* PlainText The plain text of the signature to be verified

* CertFile signer public key certificate

* return Returns true if the verification is successful, false if failed (get the failure reason from the LastErrMsg attribute)

*/
function VerifyMsg($TobeVerified, $PlainText, $CertFile,$signature_alg=OPENSSL_ALGO_SHA1)
{


//Verify signature using public key
$fp=fopen($CertFile,"r");
If(!$fp)
{
                                 
//echo "Error Number:-10005, Error Description: ER_FIND_CERT_FAILED (Certificate not found)";
         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 prompts you: Verification successful."
");
         return true;
}  
else
{
                                 
//echo "Error Number:-10021, Error Description: ER_VERIFY_ERROR (signature verification failed)|".openssl_error_string();
         return false;
}  

}

?>

openssl_verify may have three return values ​​1, 0, -1. Only returning 1 indicates that the signature verification is successful.

$signature_alg defaults to OPENSSL_ALGO_SHA1. If it is DSA encryption, set it to OPENSSL_ALGO_DSS1 http://www.bkjia.com/PHPjc/632817.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632817.html
TechArticle
Let’s take a look at the example program for using openssl_verify to verify signatures in PHP. I hope this article will be useful to all students. help. The code is as follows Copy code ?php /** * Verify signature...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template