Ethan H
Ethan H

Reputation: 625

PHP OpenSSL Error

I am getting the error Warning: openssl_get_publickey() [function.openssl-get-publickey]: Don't know how to get public key from this private key on line 5 When trying to get a public key from a private key. Here is my PHP code:

<?php
$privatekeyorig = openssl_pkey_new();
openssl_pkey_export($privatekeyorig,$privatekey);
echo '<b>Private Key:</b> ' . $privatekey . '<br>';
$publickey = openssl_get_publickey($privatekeyorig);
echo '<b>Public Key:</b> ' . $publickey . '<br>';
?>

According to the PHP manual I am doing this correctly. Anyone spot an error? Help would be greatly appreciated!

Upvotes: 2

Views: 2855

Answers (2)

walrii
walrii

Reputation: 3522

Check out the last comment on http://php.net/manual/en/function.openssl-pkey-new.php

[UPDATE] From the comment right before the above :) and it works on my system to give you a textual public key:

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

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

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

Upvotes: 1

neubert
neubert

Reputation: 16792

My recommendation would be to use phpseclib, a pure PHP RSA implementation. eg.

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey('...');

$privatekey = $rsa->getPrivateKey();
$publickey = $rsa->getPublicKey();
?>

Upvotes: 2

Related Questions