How to generate a DER/PEM certificate from public exponent and modulus of RSA?

允我心安 提交于 2019-12-22 05:58:24

问题


As we know, a public key consists of a public exponent and a modulus.

My questions is:

How to generate a DER/PEM certificate from public exponent and modulus of RSA?

Thank you very much in advance.


回答1:


With a public exponent and modulus the best you could hope to do is to get something like this:

-----BEGIN PUBLIC KEY-----
MIGGAoGAfHlcdrcuOK6C02rbGR3SgV/ZJ2wnTiFBguh5FHduoB6LcZz49LIC/KcIiH/TckK8GxQd
oJ7wHCPBpNiumrlC6caj/C8jO/HZ3cb12Wuk4gUuJq1lg5+HTv4KRJ9pFeEFQqS6X+BTztY+EoRx
uc8MlLXS4PUeouwd9Ios2K0Y5/sCASU=
-----END PUBLIC KEY-----

That said, usually DER/PEM files are used to hold private keys and you're not going to be able to get the private exponent when all you have is the public one. If, however, the above is what you're looking for, let me know and I can post further instructions on how to get it from the modulus / public exponent!

edit: Here's how I'd do it:

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

$modulus = new Math_BigInteger($modulusBinaryString, 256);
$exponent = new Math_BigInteger($exponentBinaryString, 256);

$rsa = new Crypt_RSA();
$rsa->modulus = $modulus;
$rsa->exponent = $exponent;
$rsa->publicExponent = $exponent;
$rsa->k = strlen($rsa->modulus->toBytes());

echo $rsa->getPublicKey(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
?>

I'm using phpseclib, a pure PHP RSA implementation.



来源:https://stackoverflow.com/questions/5973086/how-to-generate-a-der-pem-certificate-from-public-exponent-and-modulus-of-rsa

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