如何从 PHP OpenSSL 库中现有的 RSA 公钥获取模数和指数

use*_*543 2 php openssl rsa

有私钥、公钥、x509 Pem 文件。我想从这些文件中获取有关 RSA 公钥的信息。

我可以通过 shell 脚本进行确认,如下所示。$ openssl x509 -in cert.pem -text

...

    Subject Public Key Info:
        Public Key Algorithm: rsaEncryption
        RSA Public Key: (2048 bit)
            Modulus (2048 bit):
                00:e1:92:dc:05:84:c7:e1:2d:db:f3:48:84:90:32:
                ...
                da:7d:2f:95:d2:ab:28:6e:6c:be:0a:af:e0:cb:24:
                18:db
            Exponent: 65537 (0x10001)
Run Code Online (Sandbox Code Playgroud)

...

我可以用同样的方式在 PHP OpenSSL 库中获取 Modulus 和 Exponent 的值吗?

小智 6

您可以尝试以下操作:

$key = file_get_contents("root/to/private_key.pem");
$data = openssl_pkey_get_private($key);
$data = openssl_pkey_get_details($data);

$key = $data['key'];
$modulus = $data['rsa']['n'];
$exponent = $data['rsa']['e'];

echo "Modulus: $modulus <br>Exponent: $exponent";
Run Code Online (Sandbox Code Playgroud)

如果回显 $exponent在屏幕上没有显示任何内容,您可以尝试var_dump($exponent)var_dump($data),您会看到一些奇怪的字符。