PHP rsa从pem文件获取公钥

PHP rsa从pem文件获取公钥,php,pear,Php,Pear,如何从基于rsa 364创建的pem文件中获取公钥。 已安装的crypt(RSA.php)库仍低于错误 致命错误:调用C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\RSA.php中未定义的方法Crypt_RSA::loadKey() $file = "C:\key_file.pem"; $keypair = Crypt_RSA_KeyPair::fromPEMString(file_get_contents($file

如何从基于rsa 364创建的pem文件中获取公钥。 已安装的crypt(RSA.php)库仍低于错误

致命错误:调用C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\RSA.php中未定义的方法Crypt_RSA::loadKey()

$file = "C:\key_file.pem"; 
$keypair = Crypt_RSA_KeyPair::fromPEMString(file_get_contents($file));
$public_key = $keypair->getPublicKey(); 
$rsa_pub_key = Crypt_RSA_Key::fromString($public_key->toString()); 
$rsa_obj = new Crypt_RSA; 
$verify_status = $rsa_obj->validateSign($text,$recieved_signed_sign, $rsa_pub_key) ? 'valid' : 'invalid'; 
获取错误为致命错误:调用C:\Program Files\xxxx\rsa.php中未定义的方法PEAR\u error::getPublicKey()

$file = "C:\key_file.pem"; 
$keypair = Crypt_RSA_KeyPair::fromPEMString(file_get_contents($file));
$public_key = $keypair->getPublicKey(); 
$rsa_pub_key = Crypt_RSA_Key::fromString($public_key->toString()); 
$rsa_obj = new Crypt_RSA; 
$verify_status = $rsa_obj->validateSign($text,$recieved_signed_sign, $rsa_pub_key) ? 'valid' : 'invalid'; 
尝试了相同的东西openssl\u验证。验证正在重新启动0 正在尝试验证使用384 rsa密钥base64_encode接收到的签名

**$base64DecodedStr = base64_decode("A1a0o8JzF7q12Sr4gJvYjslhg5XVA2fWy28.JyohJ05uyiZGyBpqazqb");
$status = openssl_verify($msg,$base64DecodedStr,$pub_key);**
请帮我解决这个问题。非常感谢。

根据,Crypt\u RSA类没有loadKey()方法。将公钥作为参数关联数组的一部分传递给构造函数:

$rsa_obj = new Crypt_RSA(array('public_key' => $publickey));

我的建议是:不要使用PEAR的Crypt_RSA,而是使用phpseclib的Crypt_RSA

PEAR的Crypt#u RSA与PKCS#1不兼容,这意味着用它生成的签名或密码将无法与其他语言进行互操作,它不支持密码私钥,并且多年来没有得到有效维护

有关phpseclib的更多信息:


这是如何在php中加载公钥,如何知道加密中使用的位数以及如何加密数据。请记住将数据拆分为最大大小为key bytes size的块

<?php

// Get the public Key 
$pubKey = file_get_contents("public.key");

//echo $pubKey; echo "<br>";

$res=openssl_get_publickey($pubKey);   //convert pubkey into resource
$array=openssl_pkey_get_details($res); //read the resource details
$chunksize= $array['bits'];            //this is the chunk size 4096


$data = 'plaintext data goes here, please encrypt and decrypt the following data';
openssl_public_encrypt($data, $encrypted, $pubKey);
?>


谢谢David,我将代码更改为$file=“C:\key\u file.pem”$keypair=Crypt\u RSA\u keypair::fromPEMString(file\u get\u contents($file))$公钥=$keypair->getPublicKey()$rsa_pub_key=Crypt_rsa_key::fromString($public_key->toString())$rsa_obj=新密码rsa$验证_status=$rsa_obj->validateSign($text、$received_signed_sign、$rsa_pub_key)?”有效':'无效';将错误获取为致命错误:调用C:\Program Files\xxxx\rsa中未定义的方法PEAR\U error::getPublicKey()。php@mazheruddin,检查
文件\u获取内容($file)
结果。您的
Crypt\u RSA\u KeyPair::fromPEMString
无法从PEMString获取密钥对,只有当您使用的字符串出现问题时才会出现这种情况passed@Rumm,尝试$fp=fopen($file,“r”)$pem_data=fread($fp,filesize($file));fclose($fp);已将$pem_数据传递给fromPEMString func。仍然是相同的错误,无法回显$pem_数据