PHP rsa从pem文件中获取公钥

问题描述 投票:2回答:3

我如何从基于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_Error :: getPublicKey()的方式获取错误

尝试过相同的事情openssl_verify。验证正在回零尝试验证使用384 rsa键的base64_encode收到的符号。

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

[请帮助我解决此问题。非常感谢。

php pear
3个回答
2
投票

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

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

0
投票

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

PEAR的Crypt_RSA不符合PKCS#1,这意味着它生成的签名或密码不会与其他语言互操作,它不支持密码私钥,并且已经有多年未得到积极维护。

有关phpseclib的更多信息:

http://phpseclib.sourceforge.net/


0
投票

这是如何在php中加载公共密钥,以及如何知道其加密中使用的位数以及如何对数据进行加密。切记将数据分成最大大小为关键字节大小的块。

<?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);
?>
© www.soinside.com 2019 - 2024. All rights reserved.