3

I generate a pair of keys using openssl:

shell> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mike/.ssh/id_rsa): /path/to/test_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /path/to/test_rsa.
Your public key has been saved in /path/to/test_rsa.pub.

And then, I generate modulus from private key:

shell> openssl rsa -in /path/to/test_rsa -noout -modulus > /path/to/modulus.txt

Now, is there any way to get test_rsa.pub(public key) just from modulus?

Dariusz
  • 21,561
  • 9
  • 74
  • 114
Mike.G
  • 87
  • 3
  • 9
  • 3
    Maybe I'm wrong, it must use an exponent addition. But, if I have modulus and exponent, can I get the public key? – Mike.G Jun 07 '13 at 22:52
  • I found a post http://stackoverflow.com/questions/15420259/generate-rsa-public-key-from-modulus-and-exponent-in-bytes-in-objective-c , but it used object-c, actually I want a PHP solution. – Mike.G Jun 07 '13 at 22:55
  • How about this one? http://stackoverflow.com/questions/3116907/rsa-get-exponent-and-modulus-given-a-public-key – Chiara Hsieh Jun 08 '13 at 13:28
  • 2
    An RSA key is a pair of integers `exponent,modulus`. So, the answer is no, you cannot create a usable key from just modulus. Though many implementations do use a fixed public key exponent such as `65537`. – laalto Jun 09 '13 at 20:27
  • @Mike.G I think you should update your question to indicate that you want the public key from the modulus and the exponent. – Jaime Hablutzel Feb 21 '16 at 01:56

1 Answers1

9

You can get the public key in a more standardized format using phpseclib, a pure PHP RSA implementation. eg.

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

$modulus = 'yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=';
$exponent = 'AQAB';

$rsa = new Crypt_RSA();

$modulus = new Math_BigInteger(base64_decode($modulus), 256);
$exponent = new Math_BigInteger(base64_decode($exponent), 256);

$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setPublicKey();

echo $rsa->getPublicKey();
neubert
  • 15,947
  • 24
  • 120
  • 212