0

I created public key using the following code in C#:

using (var rsa = new RSACryptoServiceProvider(2048))
{
    rsa.PersistKeyInCsp = false;
    m_publicKey = rsa.ExportParameters(false);
}

And exported the public key to a file using the code from here.

The output file looks like:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA498EWxuZK/KsUgIEusEt
QOJulgTHwb8C4avtzJnzhosTeKooXvyGFPpex6HcQGSRqrWpNr2yhw1BvJvH2UyE
Jisl5BJA5Za+ofmbGifCFwCllZ37U1YpOmqpB2Yt+yYElGh5dp+lqs5Q3u3nPknd
nLS3bxH7qlZBvR9YPWj9x7IuSXJyopAmdJato8xeNHzmBxWD8FgQKICFpLtGsPXq
XRwT0imTs6/EcMqq6fdlp0OyBKyZjw6t47gMeqiuSYz6k41Nf/SbtIC4snUyoUgI
TvnHjWe1cY7js4kY62A9ZpHX0NpG7JXctxVb+aZOv1rS36bUjcP+bug1W3ZKrTG6
hwIDAQAB
-----END PUBLIC KEY-----

On server side I need to read the key file and use it for encryption/decryption using PHP. My PHP code is as following:

$pub_key = file_get_contents("../user_pub_key.crt");
$public_key = openssl_pkey_get_public($pub_key);
var_dump($public_key);
echo openssl_error_string();

Although $public_key is apparently created and var_dump returns the resource number of it:

resource(5) of type (OpenSSL key)

but strangely enough, openssl_error_string() is also shows the following error:

error:0906D06C:PEM routines:PEM_read_bio:no start line

I spent hours and hours searching for a solution to this. I tried the suggestions about end-of-line characters, encoding, etc., etc. in here, here, here, here, and many more links.

The only suggestions that might be relevant to this problem are here and here, but those solution seem to be for Windows systems not Mac OS, so I couldn't try them. Your help on this is much appreciated.

p.s. all the development and server are on Mac (not Windows).

Community
  • 1
  • 1
CloudWindMoonSun
  • 372
  • 1
  • 4
  • 13
  • *"but strangely enough, openssl_error_string() is also shows the following error..."* - Maybe your version of PHP is built against OpenSSL 0.9.8 and you are seeing the effects of ABI problems. I believe OS X still ships OpenSSL 0.9.8. Use `otool` to see what libraries PHP is using. Also be sure PHP can find OpenSSL's CONF file. – jww May 03 '17 at 01:23
  • @jww, I ran the command `openssl version` and I got `OpenSSL 0.9.8zh 14 Jan 2016`. I'm not familiar with `otool` but the command `otool -L php` listed many libraries including `php: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0) /usr/local/opt/openssl/lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)`. And when I run phpinfo in browser I can see `Openssl default config /private/etc/ssl/openssl.cnf`. Is there anything needs changing/upgrading on my machine? Thanks in advance for the help. – CloudWindMoonSun May 03 '17 at 23:00

0 Answers0