3

I'm having some problems loading in a public key for encrypting using openssl_seal function in PHP...

I've created the public & private keys using the openSSL command line tool:

openssl genrsa -des3 -out private.pem 1024

openssl rsa -in private.pem -out public.pem -outform PEM -pubout

However, when I run it in my PHP code I get the following errors:

openssl_seal() [function.openssl-seal]: Don't know how to get public key from this private key
openssl_seal() [function.openssl-seal]: not a public key (1th member of pubkeys)

When I verify the public key using: openssl verify public.pem, I get:

unable to load certificate
1876:error:0906D06C:PEM routines:PEM_read_bio:no start line:.\crypto\pem\pem_lib
.c:648:Expecting: TRUSTED CERTIFICATE

Anyone know why this is happening?

PHP Code:

public function encrypt($valueToEncrypt, $publicKeyFile)
{
    $pk = file_get_contents($publicKeyFile);
    $publicKey = openssl_pkey_get_public($pk);

    $encrypted = '';
    $a_envelope = array();
    $a_key = array($publicKey);
    if (openssl_seal($valueToEncrypt, $encrypted, $a_envelope, $a_key) === FALSE)
            {
                while ($msg = openssl_error_string())
                    echo $msg . "<br />\n";
                die('Failed to encrypt data!');
            }
            openssl_free_key($publicKey);
....
StuffandBlah
  • 1,047
  • 4
  • 13
  • 22
  • Please add your PHP code. Kinda hard to see what's wrong without it. From the looks of the message though I'd assume you're passing a private key where you should be passing a public key. – Bart S. May 02 '12 at 22:08
  • Code added... checked file and it's the public key. Also get this: error:0906D06C:PEM routines:PEM_read_bio:no start line on loading public key – StuffandBlah May 02 '12 at 22:15

2 Answers2

1

For anyone experiencing the same problem, the issue related to an install of xampp on windows. Running on the command line on a fresh install of just php worked fine.

StuffandBlah
  • 1,047
  • 4
  • 13
  • 22
0

In addition to StuffandBlah's answer: It is in fact related to the different OpenSSL versions in Apache and PHP in XAMPP on Windows. Here's a post of how to solve the problem (copying DLLs).

Community
  • 1
  • 1
binwiederhier
  • 1,893
  • 1
  • 16
  • 23