将 openssl 与 Kohana 3.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过阅读 openssl_pkey_get_private 上的文档,它出现您需要指定完整(绝对)路径。
使用
URL::site
会附加http://
,因此您会得到:这是无效的。我猜这会导致您的下一个函数调用失败。解决方案是
PS:不要忘记接受正确答案,否则您将来得到的回复率会较低。
From reading the documentation on openssl_pkey_get_private it appears you need to specify the full (absolute) path.
Using
URL::site
would appendhttp://
and thus you would get:which is invalid. I guess that causes your next function call to fail. The solution would be to either
PS: Don't forget to accept correct answers otherwise you'll get a lower response rate in the future.
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
.