I follow the tutorial here:
I generate the keys as follow:
# generate a 1024 bit rsa private key, ask for a passphrase to encrypt it and save to file
openssl genrsa -des3 -out mykey.private 1024
# generate the public key for the private key and save to file
openssl rsa -in mykey.private -pubout -out mykey.pub
And when I will try to use they:
$folder = 'file://'.$_SERVER['DOCUMENT_ROOT'].'/codeigniter/application/third_party/RSA/';
$pubKey = openssl_pkey_get_public($folder.'mykey.pub');
return $pubKey;
//openssl_public_encrypt($sensitiveData, $encryptedData, $pubKey);
it retuns:
Response does not contain any data.
the passphrase is: 1234 //I set 1234 for test
mykey.private:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,656AAE01548C6B4A
qw0+DGFuoQDqLsaYTgaklEaNPMSpgmMoUZIWdawKVKxkebQXiGMHyajpwWyCjtV9
ru9iinDjDgG97T3q2+k1i60fwXECMMAb/ndHAa+ckqVOsyASmmO/57ulxeiKe70L
ThmU3BvYDUJCNgNgpumi8uOmzVeJJl9v6/qHc/pTPCVIlvQOHaMCBnxnxO1gI6yG
7PIWOWyakH+xW6LPOj6Wba7RQOtFYxtbB80EymTLX+kVJ6yHb63EYW48moe9GgzB
XZDSW0ICKmge5galdhvZ0hpobS1fBIpyswUW/zZz0Jf5QkIEjgZRzNuP5XKg4BT7
MH8aDT/ZV/0kwK9R83/W+Kp2rGb2SbtUqceCo//6wH1qqfXSKqDyKljTg4Vbwp3r
Ad4rkZUuvsPfS6Z9TvUFcGeFdZ06Mg4Xq5O673E1Ibam0Qc0+ZxyNzqo3m6yJqgG
gKmQpHUGc5OrA0ElqVDzRcabPm1MI/5dK7B1+jXyN7/af5LgHoPGUV8OvKIWwyCj
OUiRDS6YrxB3bIDUdAJTJyeINu8yGJstBmOw17EycyOZy/cMo900z2i1guyCfeHr
Qp+tw23FhtAoBPLEWBIR91/COxRud229al0XsU2axuAnBcHoYyyWz3MZ5Gci9q89
CuseDLvlQZw8Q54Z79b2UO2Klh18NMu4KRpiBPLoM3V+Q1Q4focqW/2/KDfCnxLs
yGv3OEm688ajq7wxYnH5nuxiBBnDlT364xmUoB0FCb5wNg2KPaZgLXqKpi7eXcZg
JfXZLuOc4EeXqHqgzTrLmG+CbLsG+i5rWuABdz0ZQ/eM8DI2KgLQeQ==
-----END RSA PRIVATE KEY-----
mykey.public:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEhwuJ7clKxs9aoBWQAAuE0vmh
XYPNn/I4/OaFkaDqGjxsmzmMwcKWkGyJuBsheC12pibPLjQqOb7/dq2XMvL/I1hx
70NaWbafSF8MsCwXD2azm18Y1aachqXnrFcBEFdf2PPRxebqf5JPKKxqRV89fAS3
LrOYhx9YUMrVgd4WNwIDAQAB
-----END PUBLIC KEY-----
NOTE:
this test worked for me:
$key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEhwuJ7clKxs9aoBWQAAuE0vmh
XYPNn/I4/OaFkaDqGjxsmzmMwcKWkGyJuBsheC12pibPLjQqOb7/dq2XMvL/I1hx
70NaWbafSF8MsCwXD2azm18Y1aachqXnrFcBEFdf2PPRxebqf5JPKKxqRV89fAS3
LrOYhx9YUMrVgd4WNwIDAQAB
-----END PUBLIC KEY-----';
$res = openssl_pkey_get_public($key);
openssl_public_encrypt("hello", $encryptedData, $key);
echo $encryptedData;
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 param
openssl_public_encrypt("hello", $encryptedData, $resource);
//Get response
var_dump($encryptedData);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With