## Fatal error when creating RSA keys

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

### Fatal error when creating RSA keys

Hi all,

I was just trying to use phpseclib and encountered a fatal error in the key generator routine in a very simple setup.

Code:
Code: Select all
$includeFolder = getcwd() . DIRECTORY_SEPARATOR . 'be_inc' . DIRECTORY_SEPARATOR . 'crypto';set_include_path(get_include_path(). PATH_SEPARATOR .$includeFolder);require 'Crypt/RSA.php';define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);$rsa = new Crypt_RSA();$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);define('CRYPT_RSA_EXPONENT', 36075);define('CRYPT_RSA_SMALLEST_PRIME', 64);$rsa->createKey(2048);

Result:
Fatal error: Cannot use object of type Math_BigInteger as array in H:\xampp\htdocs\drhhp\be_inc\crypto\Crypt\RSA.php on line 639

On this line the variable $lcm is a Math_BigInteger itself instead splitting a top and bottom part, so$lcm['top'] is causing this error. When adding
Code: Select all
if($lcm instanceOf Math_BigInteger)$lcm = array('top' => \$lcm, 'bottom' => false);

on the line above, the script runs ages and returns even more errors:

Code: Select all
Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3088Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3092Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3088Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3092Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3088Warning: Division by zero in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 3092Fatal error: Maximum execution time of 30 seconds exceeded in H:\xampp\htdocs\drhhp\be_inc\crypto\Math\BigInteger.php on line 348

Before I keep on messing around with stuff I don't fully understand I would like to know, if I was missing something obvious or if this is really a bug?

Kevin
CommanderTomalak
Traveler

Posts: 2
Joined: Wed Jul 31, 2013 6:40 pm

### Re: Fatal error when creating RSA keys

It's sorta a bug. I'd say your code has a bug that itself is exposing a bug in phpseclib.

phpseclib's bug has been fixed in github. Your bug is that you have a bad exponent.

Quoting from wikipedia's article on RSA...

Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1; i.e. e and φ(n) are coprime.

I suppose e doesn't have to be prime so long as it is co-prime, however, none-the-less, e is usually set to a prime number all the same since a prime number is also trivially easily co-prime.
TerraFrost
Legendary Guard

Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am

### Re: Fatal error when creating RSA keys

Marvellous, that did it! Thanks a lot! I haven't done much research on the RSA code itself and thought, e was arbitrary^^

I checked your changes in the repository as well, my (wrong) code is still produces a lot of zero-division-warnings. If you want to make the lib idiot-proof (i.e. for people like me ) you could perhaps throw an exception or something similar in that case.

Regards,
Kevin
CommanderTomalak
Traveler

Posts: 2
Joined: Wed Jul 31, 2013 6:40 pm

### Re: Fatal error when creating RSA keys

The division by zero errors should hopefully be fixed now - thanks for the heads up!!
TerraFrost
Legendary Guard

Posts: 12357
Joined: Wed Dec 04, 2002 6:37 am