PHP pfx私钥 cer公钥 SHA256WithRSA签名验签算法

加密 签名

	//url  私钥地址
	//pad 私钥密码
	$certs = array();
    openssl_pkcs12_read(file_get_contents($url), $certs, $psd);
    if(!$certs){
        return ;
    }
	//data  明文字符串
	//binarySignature 加密后的密文
    if (openssl_sign(utf8_encode($data), $binarySignature, $certs['pkey'], OPENSSL_ALGO_SHA256)) {
        return bin2hex($binarySignature);
    } 

解密 验签

	$sign_old = '待验密文字符串';
	$str = ‘待验明文字符串’;
	$cer= file_get_contents(公钥地址);
	$pem =  '-----BEGIN CERTIFICATE-----'.PHP_EOL
        .chunk_split(base64_encode($cer), 64, PHP_EOL)
        .'-----END CERTIFICATE-----'.PHP_EOL;
    $pubkeyid = openssl_get_publickey($pem);
    $len = strlen($sign_old );
    $signature= pack("H" . $len, $sign_old );
    $mark = (bool)openssl_verify(
        $str,
        $signature,
        $pubkeyid,
        OPENSSL_ALGO_SHA256
    );
    if($mark){
        var_dump($str);
    }

你可能感兴趣的:(PHP pfx私钥 cer公钥 SHA256WithRSA签名验签算法)