[function.openssl-public-encrypt]:密钥参数不是有效的公钥

发布于 2024-11-24 02:23:08 字数 1446 浏览 2 评论 0原文

长时间监听者/第一次调用者

我已经设置了一个测试页面,如下所示,

<form action="" method="post">
<input type="text" name="value" width="20" max="30" value=""/>
<input type="submit" value="go"/>
</form>

使用一些 PHP 来加密发布数据“值”并将其存储在变量中,就像这样

$pubKey = openssl_pkey_get_public(".../public.pem");
openssl_public_encrypt($_POST["value"], $var, $pubKey);

echo $var;

也尝试

$publicKey = ".../public.pem";
$plaintext = $_POST['value'];

openssl_public_encrypt($plaintext, $encrypted, $publicKey);

echo $encrypted;

过不断收到错误

警告:openssl_public_encrypt() [function.openssl-public-encrypt]: key 参数不是有效的公钥

我使用 openssl 创建了密钥:

# generate a 1024 bit rsa private key, ask for a passphrase to encrypt it and save to file
openssl genrsa -des3 -out /path/to/privatekey 1024

# generate the public key for the private key and save to file

openssl rsa -in /path/to/privatekey -pubout -out /path/to/publickey

来自此网站 http://andytson.com/blog/2009/07/ php-public-key-cryptography-using-openssl/

也尝试使用此方法创建密钥:

openssl req \
  -x509 -nodes -days 365 \
  -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

仍然是相同的错误。抱歉,但整个加密对我来说很神秘。对 openssl 也不是很熟悉,所以代码示例会很棒。

Long time listener/first time caller

I have set up a test page as follows

<form action="" method="post">
<input type="text" name="value" width="20" max="30" value=""/>
<input type="submit" value="go"/>
</form>

with some PHP to encrypt the post data "value" and store it in a variable like so

$pubKey = openssl_pkey_get_public(".../public.pem");
openssl_public_encrypt($_POST["value"], $var, $pubKey);

echo $var;

also have tried

$publicKey = ".../public.pem";
$plaintext = $_POST['value'];

openssl_public_encrypt($plaintext, $encrypted, $publicKey);

echo $encrypted;

Keep getting the error

Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key

I created the keys with openssl using:

# generate a 1024 bit rsa private key, ask for a passphrase to encrypt it and save to file
openssl genrsa -des3 -out /path/to/privatekey 1024

# generate the public key for the private key and save to file

openssl rsa -in /path/to/privatekey -pubout -out /path/to/publickey

from this website http://andytson.com/blog/2009/07/php-public-key-cryptography-using-openssl/

also tried creating a key with this method:

openssl req \
  -x509 -nodes -days 365 \
  -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

still same error. Sorry but the whole encryption this is cryptic to me. also not very familiar with openssl so code examples would be great.

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

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

发布评论

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

评论(1

淡忘如思 2024-12-01 02:23:09

您的路径似乎不正确:

$pubKey = openssl_pkey_get_public(".../public.pem");

并且

$publicKey = ".../public.pem";

应该是

$publicKey = "../public.pem";
$pubKey = openssl_pkey_get_public("../public.pem");

如果 .pem 文件位于父目录中,或者:

$publicKey = "./public.pem";
$pubKey = openssl_pkey_get_public("./public.pem");

如果 .pem 文件位于当前工作目录中。或者,您可以使用绝对路径来确保获取正确的文件。

Your paths appear to be incorrect:

$pubKey = openssl_pkey_get_public(".../public.pem");

and

$publicKey = ".../public.pem";

Should be

$publicKey = "../public.pem";
$pubKey = openssl_pkey_get_public("../public.pem");

If the .pem file is in the parent directory, or:

$publicKey = "./public.pem";
$pubKey = openssl_pkey_get_public("./public.pem");

if the .pem file is in the current working directory. Alternately, you can use absolute paths to be sure you are grabbing the correct file.

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