//證書加密需要傳的參數 字符串 證書地址 證書密碼!
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;
}