openssl_pkey_get_public return 0 openssl_pkey_get_public return 0 codeigniter codeigniter

openssl_pkey_get_public return 0


The file protocol generally works differently for each environment

Filesystem is the default wrapper used with PHP and represents the local filesystem. When a relative path is specified (a path which does not begin with /, \, \\, or a Windows drive letter) the path provided will be applied against the current working directory. In many cases this is the directory in which the script resides unless it has been changed. Using the CLI sapi, this defaults to the directory from which the script was called.

With some functions, such as fopen() and file_get_contents(), include_path may be optionally searched for relative paths as well.

Note return or return resource or return false. This looks like a custom error: Response does not contain any data.

In Windows $_SERVER['DOCUMENT_ROOT'] returns something like this E:/wamp/www/project, in Like-unix returns like this /var/etc/www/project, if php script is executed in Codeigniter folder when this ...].'/codeigniter/... is not needed.

First try this:

$folder = 'file:///'.$_SERVER['DOCUMENT_ROOT'].'/application/third_party/RSA/';$pubKey = openssl_pkey_get_public($folder.'mykey.pub');

If not work, use is_file and file_get_contents, eg.:

$folder = 'file:///'.$_SERVER['DOCUMENT_ROOT'].'/application/third_party/RSA/mykey.pub';if (false === is_file(is_file))    return 'File not found';if (false === is_readable(is_file))    return 'File not readable';else    return openssl_pkey_get_public(file_get_contents($folder));

For simplify, you can use APPPATH constant from CodeIgniter, eg.:

$folder = APPPATH . '/third_party/RSA/mykey.pub';if (false === is_file(is_file))    return 'File not found';if (false === is_readable(is_file))    return 'File not readable';else    return openssl_pkey_get_public(file_get_contents($folder));

Note: I think the correct use of openssl_public_encrypt is (example):

<?php//Set $myResource var$myResource = openssl_pkey_get_public('test.pem');//Use $myResource var in third paramopenssl_public_encrypt("hello", $encryptedData, $resource);//Get responsevar_dump($encryptedData);