openssl_pkey_get_public返回0

时间:2015-06-12 10:13:33

标签: php codeigniter openssl

我按照教程here

我按如下方式生成密钥:

# 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

当我尝试使用它们时:

$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);

它回归:

Response does not contain any data.

密码是:1234 //我设置1234进行测试

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

注意:

这个测试对我有用:

$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; 

1 个答案:

答案 0 :(得分:1)

file protocol通常对每个环境的工作方式不同

Filesystem是PHP使用的默认包装器,代表本地文件系统。指定相对路径(不以/\\\或Windows驱动器号开头的路径)时,将提供针对当前工作目录的路径。在许多情况下,这是脚本所在的目录,除非它已被更改。使用CLI sapi,默认为调用脚本的目录。

对于某些功能,例如fopen()file_get_contents(),也可以选择include_path搜索相对路径。

注意return或返回resource或返回false。这看起来像是自定义错误:Response does not contain any data.

在Windows $_SERVER['DOCUMENT_ROOT']中返回类似此E:/wamp/www/project的内容,在Like-unix中返回如此/var/etc/www/project,如果在...].'/codeigniter/...不是$folder = 'file:///'.$_SERVER['DOCUMENT_ROOT'].'/application/third_party/RSA/'; $pubKey = openssl_pkey_get_public($folder.'mykey.pub'); 的情况下在Codeigniter文件夹中执行php脚本需要的。

首先尝试这个:

is_file

如果不起作用,请使用file_get_contents$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)); ,例如:

APPPATH

为简化起见,您可以使用CodeIgniter中的$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)); 常量,例如:

openssl_public_encrypt

注意:我认为正确使用<?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); 是(示例):

:Plus    
echo-------------------------------------------------------------------------    
echo Please enter starting number...    
set /p time=%time%    
echo Please enter ending number...    
set /p etime=%etime%    
echo-------------------------------------------------------------------------    
echo.%time%    
set time=%time%     

:loop1    
set /p time=%time%+1    
if %time%==%etime% goto timesup1    
echo.%time%    
ping localhost -n 2 > nul    
goto loop1    

:timesup1    
echo.%etime%    
echo Time is Up!    
goto Opt    

(The Minus Option just shows the number typed then '-1')    
:Minus    
echo ------------------------------------------------------------------------    
echo Please enter starting number...    
set /p time=%time%    
echo Please enter ending number...    
set /p etime=%etime%    
echo-------------------------------------------------------------------------   
echo %time%    
set time=%time%     

:loop2    
set /p time=%time%-1    
if %time%==%etime% goto timesup2    
echo %time%    
ping localhost -n 2 > nul    
goto loop2    

:timesup2    
echo %etime%    
echo Time is Up!    
goto Opt