简体   繁体   English

openssl_pkey_get_public返回false,但是密钥确实存在

[英]openssl_pkey_get_public returning false, but key does exist

I'm using a library that uses openssl_pkey_get_public, but it's returning false. 我正在使用使用openssl_pkey_get_public的库,但它返回的是false。 It seems that openssl is enabled, and the key exists. 似乎已启用openssl,并且该密钥存在。 Below are the few lines from the library I'm using, btw which I am debugging but cannot modify as it is not my code base: 下面是我正在使用的库btw中的几行内容,我正在调试它们,但由于它不是我的代码库而无法修改:

protected function decrypt($encryptedData)
    {
        $publicKey = openssl_pkey_get_public($this->publicKey->getKeyPath());
        $publicKeyDetails = @openssl_pkey_get_details($publicKey);
        if ($publicKeyDetails === null) {
            throw new \LogicException(
                sprintf('Could not get details of public key: %s', $this->publicKey->getKeyPath())
            );
        }
.
.
.

I have the inserted the following debug code: 我插入了以下调试代码:

$keyPath = $this->publicKey->getKeyPath(); // returns file:///var/www/sso/website/storage/id_rsa.pub
var_dump(file_exists($keyPath)); // outputs true
var_dump(openssl_pkey_get_public($keyPath)); // returns false

Below shows the contents of $keyPath: 下面显示了$ keyPath的内容:

echo file_get_content($keyPath);

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChY1gtF0Oeku62+4HCisIswcDu9fjZV7fImTlqQej/UsmsJH7jz5EF/ZXCWTKV/bgOwzV2oeHomukITqiR14D01W3mVcpTBAp5AP4JN25am57xdc6Nxd8Lo/NsCKKqQ4/uBmpYBVZm8Ye/hu3ixM6y/xbCGnw/ca4z0DKDa94z1XrRc6FrV1mXx5lItQEo/v8wVKX9NJVAANYZ/jJEk7jGTB9WkSTNR5l/tNBBF3MFuBigjSuaxUsnKT2IwOV5g2ewN4TzXARi2/BI7rweNsUFCWRbkUa7VJc3XOVZbS50TzUpAIqHI9Q8enBs95A1JvSTDvlT3efEHrM2T7KP7QOz ubuntu@ubuntu-xenial

I had previously created the keys with the following command: 我以前使用以下命令创建了密钥:

ssh-keygen -f storage/id_rsa -t rsa -N ''

Some additional info if it help: 一些其他信息,如果有帮助的话:

$ php -i | grep openssl
openssl
Openssl default config => /usr/lib/ssl/openssl.cnf
openssl.cafile => no value => no value
openssl.capath => no value => no value

$ php -m | grep openssl
openssl

Is there any reason why this might be happening? 有什么原因可能会发生这种情况?

You're using an SSH formatted public key. 您正在使用SSH格式的公用密钥。 OpenSSL doesn't support that format. OpenSSL不支持该格式。 But you know what does support that format? 但是您知道什么支持该格式吗? phpseclib. phpseclib。 It'll auto-detect the format and, once loaded, will let you do whatever RSA operations you need to do. 它会自动检测格式,并在加载后让您执行所需的任何RSA操作。 Be aware, tho, that phpseclib expects the actual key to be passed to it - not a path to the key on the file system but the key itself. 请注意,phpseclib希望将实际的密钥传递给它-不是文件系统上密钥的路径,而是密钥本身。

Sample code: http://phpseclib.sourceforge.net/rsa/examples.html#encrypt,enc 示例代码: http : //phpseclib.sourceforge.net/rsa/examples.html#encrypt,enc

$pub_key = openssl_pkey_get_public("-----BEGIN PUBLIC KEY-----\n".file_get_contents('public_key.pem')."\n-----END PUBLIC KEY-----"); 
var_dump($pub_key); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM