将 openssl 与 Kohana 3.0 框架一起使用返回错误

发布于 2024-09-10 07:56:23 字数 1144 浏览 2 评论 0原文

我的加密控制器的代码如下所示。这在我的 kohana 安装的单独工作目录中本身可以正常工作。

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

    return $sensitive;
    }
}

不过,由于某种原因,我不明白我收到了这个错误:

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

我再次在 kohana 应用程序之外自行检查了这段代码,它工作正常,解密了 $cipher。

我绝不是使用 openssl 的专家,因此如果有人知道这里的问题是什么并且可以启发我,我将不胜感激。

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

    return $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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-09-17 07:56:23

通过阅读 openssl_pkey_get_private 上的文档,它出现您需要指定完整(绝对)路径。

使用 URL::site 会附加 http:// ,因此您会得到:

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

这是无效的。我猜这会导致您的下一个函数调用失败。解决方案是

  1. 将私钥加载到字符串中并传递它,或者
  2. 使用类似 realpath 获取绝对路径。

PS:不要忘记接受正确答案,否则您将来得到的回复率会较低。

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.

守不住的情 2024-09-17 07:56:23

Pixel 的诊断是正确的。您最终会想要这样的东西:

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

像这样的路径常量是在 index.php 中定义的

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文