开发者

Using openssl with Kohana 3.0 framework returning error

The code for my encryption controller is shown below. This works fine by itself in a separate working directory from my kohana installation.

class Controller_Crypt extends Controller_Home
{
    public static function Encrypt_This($sensitive)
    {
    //file path must be relative to directory, also derp.
        $pubkey = openssl_pkey_get_public('file://' . URL::site('../../private/public');
openssl_public_encrypt($sensitive, $cipher, $pubkey);
    return base64_encode($cipher);  
    return $cipher;
    }
    public static function Decrypt_This($cipher, $pass)
    {
     $cipher = base64_decode($cipher);
        //file path must be relative to directory, also derp.
    $prkey = 'file://' . URL::site('../../private/private');
    $privatekey = openssl_pkey_get_private($prkey, $pass);
    openssl_private_decrypt($cipher, $sensitive, $privatekey);

    retur开发者_如何学编程n $sensitive;
    }
}

Though, for some reason I do not understand I am getting this error:

ErrorException [ Warning ]: openssl_private_decrypt() [function.openssl-private-decrypt]: key parameter is not a valid private key

Once again I have checked this code by itself outside of the kohana application and it works fine, decrypting the $cipher.

I am by no means an expert with using openssl so if anyone knows what the issue is here and could enlighten me I would greatly appreciate it.


From reading the documentation on openssl_pkey_get_private it appears you need to specify the full (absolute) path.

Using URL::site would append http:// and thus you would get:

file://http://localhost/../../private/private

which is invalid. I guess that causes your next function call to fail. The solution would be to either

  1. Load the private key into a string and pass that or
  2. Use a function like realpath to get the absolute path.

PS: Don't forget to accept correct answers otherwise you'll get a lower response rate in the future.


Pixel has the right diagnosis. You'll end up wanting to something like this:

openssl_pkey_get_public('file://' .APPPATH. 'private/public');

The path constants like this are defined in index.php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜