phpseclib not works with php5-cli from terminal

左心房为你撑大大i 提交于 2019-12-24 08:38:38

问题


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:


  1. Make sure error_reporting is on, and turned up high enough.
  2. 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 returning false,NULL, or an empty string, and echo isn't going to tell you which.


来源:https://stackoverflow.com/questions/20688693/phpseclib-not-works-with-php5-cli-from-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!