13 Replies - 8051 Views - Last Post: 13 January 2011 - 02:24 PM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

php & openssl help

Posted 16 October 2010 - 04:10 AM

I need to use openssl within my php project, so I created a test php page using openssl. However, I keep getting these errors and I am not sure why. I thought maybe it couldn't locate the conf file but it is pointing to the correct one, and openssl is enabled.

Warning: openssl_pkey_export() [function.openssl-pkey-export]: cannot get key from parameter 1 in C:\xampp\htdocs\test.php on line 16

Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test.php on line 19


Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in C:\xampp\htdocs\test.php on line 28

Warning: openssl_private_decrypt() [function.openssl-private-decrypt]: key parameter is not a valid private key in C:\xampp\htdocs\test.php on line 32



<?php

if (isset($_SERVER['HTTPS']) )
{
    echo "SECURE: This page is being accessed through a secure connection.<br><br>";
}
else
{
    echo "UNSECURE: This page is being access through an unsecure connection.<br><br>";
}

// Create the keypair
$res=openssl_pkey_new();

// Get private key
openssl_pkey_export($res, $privatekey);

// Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"];

echo "Private Key:<BR>$privatekey<br><br>Public Key:<BR>$publickey<BR><BR>";

$cleartext = '1234 5678 9012 3456';

echo "Clear text:<br>$cleartext<BR><BR>";

openssl_public_encrypt($cleartext, $crypttext, $publickey);

echo "Crypt text:<br>$crypttext<BR><BR>";

openssl_private_decrypt($crypttext, $decrypted, $privatekey);

echo "Decrypted text:<BR>$decrypted<br><br>";
?>



Is This A Good Question/Topic? 0
  • +

Replies To: php & openssl help

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,221
  • Joined: 23-August 08

Re: php & openssl help

Posted 16 October 2010 - 05:19 AM

Obviously the openssl_pkey_new() function failed and returned FALSE. From the Installation doc:

Quote

This extension requires the following files to be in the PATH: libeay32.dll

Is that the case here?
Was This Post Helpful? 0
  • +
  • -

#3 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 16 October 2010 - 06:08 AM

That dll is located within my \xampp\apache\bin but I am unsure of how to check if it's in the PATH, I will check it out. Thanks

This post has been edited by megglz: 16 October 2010 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,221
  • Joined: 23-August 08

Re: php & openssl help

Posted 16 October 2010 - 06:20 AM

Open a command prompt (cmd.exe) and type echo %PATH%.
Was This Post Helpful? 1
  • +
  • -

#5 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 16 October 2010 - 09:44 AM

I've added the dll to my PATH now, but my test.php still has the same errors as above :(
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,221
  • Joined: 23-August 08

Re: php & openssl help

Posted 16 October 2010 - 10:19 AM

Did you restart the server?
Was This Post Helpful? 0
  • +
  • -

#7 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 16 October 2010 - 10:23 AM

I did
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,221
  • Joined: 23-August 08

Re: php & openssl help

Posted 16 October 2010 - 10:39 AM

Version of PHP? I'll look in the source and see if I can find the other reasons it might return FALSE.
Was This Post Helpful? 0
  • +
  • -

#9 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 16 October 2010 - 10:51 AM

These are what I installed if this also helps..

XAMPP for Windows bundle (apache, php, mysql openssl) http://www.apachefri...pp-windows.html


PHP 5.3.1
php.ini located in php/
does not have any ;extension=php_openssl.dll (there is also no php_openssl.dll anywhere)

openssl found in apache/bin

phpinfo() tells me it's enabled and is pointing to the conf correctly.

libeay32.dll also in apache/bin.

I've added it to my PATH and restarted my system, and XAMPP.

I've googled everywhere for help but old tutorials only seem to mention uncommenting the extension=php_openssl.dll bit.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,221
  • Joined: 23-August 08

Re: php & openssl help

Posted 17 October 2010 - 08:17 AM

I downloaded, installed, tried a bunch of things and got nowhere. These are the errors generated by OpenSSL:

OpenSSL Error: error:02001003:system library:fopen:No such process
OpenSSL Error: error:2006D080:BIO routines:BIO_new_file:no such file
OpenSSL Error: error:0E064002:configuration file routines:CONF_load:system lib
OpenSSL Error: error:02001003:system library:fopen:No such process
OpenSSL Error: error:2006D080:BIO routines:BIO_new_file:no such file
OpenSSL Error: error:0E064002:configuration file routines:CONF_load:system lib



I might suggest seeking help/support from XAMPP. Sorry.
Was This Post Helpful? 2
  • +
  • -

#11 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 17 October 2010 - 10:34 AM

Thanks very much for your help anyway :) As soon as I find a solution to whatever is up I'm going to blog it for the world!
Was This Post Helpful? 0
  • +
  • -

#12 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 20 October 2010 - 09:17 AM

still no joy with this whatever i do :(
Was This Post Helpful? 0
  • +
  • -

#13 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: php & openssl help

Posted 20 October 2010 - 01:07 PM

Update:

This isn't a 100% solution but I found that I can generate the private and public keys via the command line, and then reference these keys fine via php-ssl functions.

example, in commandline
# generate a 1024 bit rsa private key
openssl genrsa -des3 -out privkey.pem 1024
 
# generate the public key for the private key
openssl rsa -in privkey.pem -pubout -out pubkey.pem



php code sample

<?php
$fp=fopen ("../apache/bin/public.pem","r");
$pub_key=fread ($fp,8192);
fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
$plaintext = "String to encrypt";
echo $plaintext;
openssl_public_encrypt($plaintext, $encrypted, $PK);
echo $encrypted;   //encrypted string
?>



Hope this helps someone else :)
Was This Post Helpful? 1
  • +
  • -

#14 Guest_Suggy*


Reputation:

Re: php & openssl help

Posted 13 January 2011 - 02:24 PM

This code saved me HOURS. thank you.



View Postmegglz, on 20 October 2010 - 12:07 PM, said:

Update:

This isn't a 100% solution but I found that I can generate the private and public keys via the command line, and then reference these keys fine via php-ssl functions.

example, in commandline
# generate a 1024 bit rsa private key
openssl genrsa -des3 -out privkey.pem 1024
 
# generate the public key for the private key
openssl rsa -in privkey.pem -pubout -out pubkey.pem



php code sample

<?php
$fp=fopen ("../apache/bin/public.pem","r");
$pub_key=fread ($fp,8192);
fclose($fp);
$PK="";
$PK=openssl_get_publickey($pub_key);
$plaintext = "String to encrypt";
echo $plaintext;
openssl_public_encrypt($plaintext, $encrypted, $PK);
echo $encrypted;   //encrypted string
?>



Hope this helps someone else :)

Was This Post Helpful? 0

Page 1 of 1