RSA test in PHP and JS, using openssl with XAMPP in Windows

Hello,

after some failed tests to generate a RSA key pair, I found a post in the wonderful site stackoverflow, saying php openssl doesn’t work in Windows until you configure the following system environment variable (in ControlPanel.SystemAndSecurity.AdvancedSystemSettings.EnvironmentVariables), and restart the computer:

OPENSSL_CONF = D:\xampp5.6.23\apache\conf\openssl.cnf

where the fullpath should the one on your xampp installation.

 

If everything goes right, this php code should work correctly:

$rsakeysize = 512; // 1024, 2040
$config = array('private_key_bits' => $rsakeysize,'private_key_type' => OPENSSL_KEYTYPE_RSA);
$res=openssl_pkey_new($config);
openssl_pkey_export($res, $prk);
$pubkey=openssl_pkey_get_details($res);
$puk=$pubkey["key"];

Otherwise you’d likely get the message:
Warning: openssl_pkey_export() [function.openssl-pkey-export]: cannot get key from parameter 1 in <<your php file>> on line<<…>>
Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in <<your php file>> on line<<…>>.

 

A simple test has been made, using a single php file, with php and js source.
In php the operations are made by openssl (part of recent php standard language).
In JS the JSEncrypt library has been used.
This library (along with the others) can be downloaded automatically from cdnjs repository.

The source of the test file is on Github at this link: https://github.com/Fred68/crypto.js.php.

BR.

 

 

P.S. Sincere thanks to Travis Tidwell’s and Tom Wu‘s amazing works.