phpseclib createKey() using own primes

252 Views Asked by At

Is it possible to generate private and public key in PKCS#1 format using phpseclib and my own primes? I mean I already have p and q and I want to generate both keys. I am trying to do something like this:

$p = new Math_BigInteger(...);
$q = new Math_BigInteger(...);
$custom_primes = serialize(array('primes'=>array(1=>$p,2=>$q)));
extract($rsa->createKey(512,10,$custom_primes));

But this gives me the fatal error:

Fatal error: Call to a member function divide() on a non-object in /volume1/web/phpseclib/Crypt/RSA.php on line 705

I checked this and it is trying to divide:

list($temp) = $lcm['top']->divide($lcm['bottom']);

Obviously I am not setting up lcm in my $custom_primes structure as I only have my two primes. So the question is: is it possible at all in phpseclib?

1

There are 1 best solutions below

0
On

It looks like you're trying to use phpseclib's partial key functionality to achieve this. Problem is that expects more than just primes. See this, for example:

 return array(
    'privatekey' => '',
    'publickey' => '',
    'partialkey' => serialize(array(
        'primes' => $primes,
        'coefficients' => $coefficients,
        'lcm' => $lcm,
        'exponents' => $exponents
    ))
);

$lcm is defined, initially, like this:

$lcm = array(
    'top' => $this->one->copy(),
    'bottom' => false
);

So maybe try doing that as well. You can probably strip out all of the calculation functions from phpseclib, do them yourself and then pass $partial into phpseclib and let it generate a key in whatever format you want it generated in.