我有一个RSA私钥和一个RSA公钥。
两个rsa密钥都是xml版本(<RSAKeyValue><Modulus>....
);
我需要从私钥创建一个PKCS8签名,并通过php
中的publik密钥对其进行测试我使用此代码片段进行签名:
$content = "test string";
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents("private.txt"));
$pem_format = $rsa->getPrivateKey();
$pvk_res = openssl_get_privatekey($pem_format);
$sig = '';
openssl_sign($content , $sig, $pvk_res, OPENSSL_ALGO_SHA1);
$signature = base64_encode($sig);
这是制作签名的正确方法吗?
现在如何使用公钥来测试签名的准确性?
答案 0 :(得分:1)
PKCS8涉及关键格式 - 而不是签名。
另外,我看到你正在使用phpseclib将密钥转换为pem然后使用openssl。为什么不把phpseclib用于一切?此时您可以使用此示例:
http://phpseclib.sourceforge.net/rsa/examples.html#sign,sign2
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
//$rsa->setPassword('password');
$rsa->loadKey('...'); // private key
$plaintext = '...';
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$signature = $rsa->sign($plaintext);
$rsa->loadKey('...'); // public key
echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
?>