简体   繁体   English

PHP rsa 从 pem 文件中获取公钥

[英]PHP rsa get public key from pem file

How can i get publickey from pem file which is created based on rsa 364. installed crypt(RSA.php) library still getting below error我如何从基于 rsa 364 创建的 pem 文件中获取公钥。安装的 crypt(RSA.php) 库仍然低于错误

Fatal error: Call to undefined method Crypt_RSA::loadKey() in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\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'; 

getting error as Fatal error: Call to undefined method PEAR_Error::getPublicKey() in C:\\Program Files\\xxxx\\rsa.php将错误视为致命错误:调用 C:\\Program Files\\xxxx\\rsa.php 中未定义的方法 PEAR_Error::getPublicKey()

Tried same thing openssl_verify.尝试过同样的事情 openssl_verify。 verify is rturning 0 Trying to verify sign received with base64_encode with 384 rsa key.验证正在 rturning 0 尝试使用 384 rsa 密钥验证使用 base64_encode 收到的签名。

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

Please help me to resolve this issue.请帮我解决这个问题。 Thanks a lot.非常感谢。

According to the Crypt_RSA documentation , the Crypt_RSA class doesn't have a loadKey() method.根据Crypt_RSA 文档,Crypt_RSA 类没有 loadKey() 方法。 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));

My recommendation: don't use PEAR's Crypt_RSA but rather phpseclib's Crypt_RSA.我的建议:不要使用 PEAR 的 Crypt_RSA,而是使用 phpseclib 的 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. PEAR 的 Crypt_RSA 不符合 PKCS#1,这意味着用它生成的签名或密文不会与其他语言互操作,它不支持密码私钥,并且多年来没有积极维护。

More info on phpseclib:有关 phpseclib 的更多信息:

http://phpseclib.sourceforge.net/ http://phpseclib.sourceforge.net/

this is how to load public key in a php and how to know the number of bits used in its encryption and how to encrypt data.这是如何在 php 中加载公钥以及如何知道其加密中使用的位数以及如何加密数据。 remember to split the data into chunks with maximum size of 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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM