Question

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.

Was it helpful?

Solution

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){
    ... 
}

OTHER TIPS

  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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top