phpseclib:使用证书

时间:2018-04-03 03:15:35

标签: php encryption x509 phpseclib

我有private.pempublic.crt。我的目标是使用private.pem进行签名,并使用public.crt验证其签名。如何使用phpseclib实现这一目标?

$data = 'test';
$rsa = new RSA();
$privatekey = file_get_contents(storage_path('app/private.pem'));
$rsa->loadKey($privatekey); 
$signed = $rsa->sign($data);

$publickey = file_get_contents(storage_path('app/public.crt'));
$rsa->loadKey($publickey);

return $rsa->verify($data, $signed) ? 'verified' : 'unverified';

1 个答案:

答案 0 :(得分:2)

得到了我的回答here

<?php
$data = 'test';
$rsa = new RSA();
$x509 = new X509();
$privatekey = file_get_contents(storage_path('app/private.pem'));
$rsa->loadKey($privatekey);
$signed = $rsa->sign($data);

$publickey = file_get_contents(storage_path('app/public.crt'));
$x509->loadX509($publickey);
$rsa = $x509->getPublicKey();
return $rsa->verify($data, $signed) ? 'verified' : 'unverified';