php 證書加密 和證書解密方法


//證書加密需要傳的參數  字符串  證書地址  證書密碼!

function sign($data,$pfxpath,$pfxpwd) {
$certs = array();
openssl_pkcs12_read(file_get_contents($pfxpath), $certs, $pfxpwd); // 其中password為你的證書密碼
if (!$certs) {
return;
}
$signature = '';
openssl_sign($data, $signature, $certs['pkey']);
return base64_encode($signature);
}

//證書解密字符串  ,編碼加密的字符串

function verify($data, $signature) {
$certs = array();
openssl_pkcs12_read(file_get_contents('./test.pfx'), $certs, 'hzsmk');
if(!$certs) {
return;
}
$result = openssl_verify($data, base64_decode($signature), $certs['cert']); // openssl_verify驗簽成功返回1,失敗0,錯誤返回-1
return $result;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM