0

我有公钥的模数和指数。我可以在 php 中使用 openssl api 生成公钥吗?我已经阅读了http://php.net/manual/en/ref.openssl.php但似乎没有存档的功能。

如果 openssl 不能做到这一点(用模数和指数构建公钥),我可以使用 php 中的任何选项来做到这一点吗?

4

2 回答 2

2

使用phpseclib,一个纯 PHP RSA 实现

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();

$rsa->loadKey( array ('e'=>new Math_BigInteger('65537'), 'n'=>new Math_BigInteger('11944573237954459805614520005393273287786384679965238498958830373752732874397055'.
'98832111464872863171681422024469555089029360351247093972211786644957214280299847'.
'26868375359168203283442617134197706515425366188396513684446494070223079865755643'.
'116690165578452542158755074958452695530623055205290232290667934914919')) );

echo $rsa;

输出如下:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVx
wTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFnc
CzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0T
p0GbMJDyR4e9T04ZZwIDAQAB
-----END PUBLIC KEY-----
于 2013-04-05T13:03:35.570 回答
1

如果您的模数和指数来自 XML 格式的 RSA 密钥,或者您正在从JWKS端点检索密钥,则可以使用以下代码:

use phpseclib\Crypt\RSA;

$n = "6R-qAvAPQNrx8I_Q7GmQE4zpKFBpKMM38yeFE797v9eLBdXr9JIW5PBuBqjkmzMug3MUvl4FM6_nU0SCMLDMSY_zGIt5xlmFF5iqRb8elTZ_bLe6bp_1cZncadl4vzRdi6jQE_aIZX_9CWypO4su-kNj6DVCh5wldbGZej0c-1sKCgkJF12hiAqQPxaIA4hDgDCFiCRWSPzxHRuS-aXV0h3DIhn84lCkzrBqWea-nS03z0k2ohmuNsTd0yIPz_s-R2TkE-lUaKVtKGqsRPL3GKzQJO4TUzYmqHkN8XyNkmvBaitWreiZjyX79qTJ46SNX55gzVAfWqevpJc0RezVsQ==";
$e = "AQAB";

// Convert Base64 uri-safe variant to default (needed for JWKS)
$n = strtr($n, '-_', '+/');

$rsa = new RSA();

$key = "<RSAKeyPair>"
        . "<Modulus>" . $n . "</Modulus>"
        . "<Exponent>" . $e . "</Exponent>"
        . "</RSAKeyPair>";

$rsa->loadKey($key, RSA::PUBLIC_FORMAT_XML);

echo $rsa;
于 2019-12-06T10:42:02.677 回答