Laravel GPG - 尝试编码时出现问题
Laravel GPG - Issue when attemping to Encode
因此,我正尝试在 Laravel 中使用 GPG 进行一些编码。我找到了包 https://github.com/jasonhinkle/php-gpg,虽然不再维护,但看起来它仍然可以满足我需要做的事情。我已将代码安装在自定义文件夹 app/Classes/GPG
中,并将其包含在将使用它的 artisan
命令中:
class CustomCommand extends Command {
private $gpg = null;
private $publicKey = null;
protected $signature = "custom:command";
public function __construct() {
$this->publicKey = new \GPG_Public_Key(file_get_contents(base_path()."/path/to/public/key.asc"));
$this->gpg = new \GPG();
parent::__construct();
}
public function handle() {
$this->gpg->encrypt($this->publicKey, "Hello World!");
}
}
有了上面的代码,就万事大吉了; $this->gpg
和 $this->publicKey
都没有问题地初始化,但是当我尝试 运行 php artisan custom:command
时,我收到以下错误:
(1/1) ErrorException
String offset cast occurred
in GPG.php line 112
喜欢112的GPG.php
如下:
private function gpg_session($key_id, $key_type, $session_key, $public_key){
...
$s = base64_decode($public_key);
$l = floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8);
if($key_type) {
...
$l2 = floor((ord($s[$l + 2]) * 256 + ord($s[$l + 3]) + 7) / 8) + 2; // 112
}
}
做 dd($s, $l)
会导致 string
乱七八糟,我可能无法在此处 post,并且 256.0
.
有人以前见过这个问题吗?
问题在于这一行:
$l = floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8); // 256.0
由于 $l
是 float
,而不是 integer
,这一行:
$l2 = floor((ord($s[$l + 2]) * 256 + ord($s[$l + 3]) + 7) / 8) + 2; // String offset cast occurred
在两种尝试访问 $s[$l ...]
的情况下均失败。要解决此问题,只需将 $l
的声明包装在 intval()
调用中以将其明确设置为 integer
:
$l = intval(floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8)); // 256
此错误的原因是 PHP 的版本。在 PHP 5.4 之前,由于转换,尝试访问带有 float
的数组是有效的。给定 $l = 256.0
,$s[$l]
将尝试访问 $s[256]
。在 PHP 5.4+ 中,引入了此错误,并且由于未维护此包,因此从未得到妥善处理。希望这可以帮助其他人。
因此,我正尝试在 Laravel 中使用 GPG 进行一些编码。我找到了包 https://github.com/jasonhinkle/php-gpg,虽然不再维护,但看起来它仍然可以满足我需要做的事情。我已将代码安装在自定义文件夹 app/Classes/GPG
中,并将其包含在将使用它的 artisan
命令中:
class CustomCommand extends Command {
private $gpg = null;
private $publicKey = null;
protected $signature = "custom:command";
public function __construct() {
$this->publicKey = new \GPG_Public_Key(file_get_contents(base_path()."/path/to/public/key.asc"));
$this->gpg = new \GPG();
parent::__construct();
}
public function handle() {
$this->gpg->encrypt($this->publicKey, "Hello World!");
}
}
有了上面的代码,就万事大吉了; $this->gpg
和 $this->publicKey
都没有问题地初始化,但是当我尝试 运行 php artisan custom:command
时,我收到以下错误:
(1/1) ErrorException
String offset cast occurred
in GPG.php line 112
喜欢112的GPG.php
如下:
private function gpg_session($key_id, $key_type, $session_key, $public_key){
...
$s = base64_decode($public_key);
$l = floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8);
if($key_type) {
...
$l2 = floor((ord($s[$l + 2]) * 256 + ord($s[$l + 3]) + 7) / 8) + 2; // 112
}
}
做 dd($s, $l)
会导致 string
乱七八糟,我可能无法在此处 post,并且 256.0
.
有人以前见过这个问题吗?
问题在于这一行:
$l = floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8); // 256.0
由于 $l
是 float
,而不是 integer
,这一行:
$l2 = floor((ord($s[$l + 2]) * 256 + ord($s[$l + 3]) + 7) / 8) + 2; // String offset cast occurred
在两种尝试访问 $s[$l ...]
的情况下均失败。要解决此问题,只需将 $l
的声明包装在 intval()
调用中以将其明确设置为 integer
:
$l = intval(floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8)); // 256
此错误的原因是 PHP 的版本。在 PHP 5.4 之前,由于转换,尝试访问带有 float
的数组是有效的。给定 $l = 256.0
,$s[$l]
将尝试访问 $s[256]
。在 PHP 5.4+ 中,引入了此错误,并且由于未维护此包,因此从未得到妥善处理。希望这可以帮助其他人。