PHP rsa get public key from pem file

别等时光非礼了梦想. 提交于 2019-12-04 01:50:50

问题


How can i get publickey from pem file which is created based on rsa 364. installed crypt(RSA.php) library still getting below error

Fatal error: Call to undefined method Crypt_RSA::loadKey() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\rsa.php

$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'; 

getting error as Fatal error: Call to undefined method PEAR_Error::getPublicKey() in C:\Program Files\xxxx\rsa.php

Tried same thing openssl_verify. verify is rturning 0 Trying to verify sign received with base64_encode with 384 rsa key.

**$base64DecodedStr = base64_decode("A1a0o8JzF7q12Sr4gJvYjslhg5XVA2fWy28.JyohJ05uyiZGyBpqazqb");
$status = openssl_verify($msg,$base64DecodedStr,$pub_key);**

Please help me to resolve this issue. Thanks a lot.


回答1:


According to the Crypt_RSA documentation, the Crypt_RSA class doesn't have a loadKey() method. You pass the public key to the constructor as part of an associative array of parameters:

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



回答2:


My recommendation: don't use PEAR's Crypt_RSA but rather phpseclib's Crypt_RSA.

PEAR's Crypt_RSA isn't PKCS#1 compliant, meaning signatures or ciphertexst's generated with it are not going to be interoperable with other languages, it doesn't support passworded private keys, and hasn't been actively maintained for years.

More info on phpseclib:

http://phpseclib.sourceforge.net/



来源:https://stackoverflow.com/questions/4648743/php-rsa-get-public-key-from-pem-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!