我确实有 private.pem
和 public.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';
最佳答案
得到了我的答案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';
关于phpseclib:使用证书验证签名数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49621672/