问题
I installed php5-cli package on my raspbian (debian OS) and I downloaded phpseclib in /cli/ directory. I created a test file to encrypt text with a public key (generated within my apache server on which I have the same library) in this way:
include('libs/Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey($myPublicKey);
$encrypted = $rsa->encrypt("my text");
echo "result: " .$encrypted;
If I try to type php test.php the result variable is empty. Why? It's due to the fact I execute the file from terminal e with php5-cli instead of php5 ?
How Can I obtain the result of this encrypting operation?
Thanks.
回答1:
The source code you have provided is correct. If the value of $encrypted is empty, it's possible due to an incorrect key supplied in loadKey.
According to the comments in the file RSA.php, then the command will return false if the key is invalid:
/**
* Loads a public or private key
*
* Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)
*
* @access public
* @param String $key
* @param Integer $type optional
*/
function loadKey($key, $type = false){
...
}
回答2:
- Make sure
error_reportingis on, and turned up high enough. - Try
var_dump($encrypted)instead of echoing. PHPseclib's documentation is a bit lacking, and it doesn't detail the behaviour of the Crypt_RSA::encrypt() function on failure. It could be returningfalse,NULL, or an empty string, andechoisn't going to tell you which.
来源:https://stackoverflow.com/questions/20688693/phpseclib-not-works-with-php5-cli-from-terminal