6

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.

xmllmx
  • 39,765
  • 26
  • 162
  • 323

1 Answers1

5

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.

  • Thank you very much. This is just what I want. Please tell me how to get it from the modulus / public exponent! – xmllmx May 12 '11 at 04:53
  • DER/PEM files are not necessarily private key files. They can be just about anything, often X.509 certificates. – President James K. Polk May 12 '11 at 22:54
  • I think Crypt_RSA changed interface, I am getting `Call to undefined method Crypt_RSA::getPublicKey()` :( – wadkar Nov 18 '11 at 07:03
  • That function is still there. Are you doing Crypt_RSA::getPublicKey() or $rsa->getPublicKey()? Are you using the latest SVN or what? –  Apr 23 '12 at 04:39