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).