[function.openssl-public-encrypt]:密钥参数不是有效的公钥
长时间监听者/第一次调用者
我已经设置了一个测试页面,如下所示,
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的路径似乎不正确:
并且
应该是
如果 .pem 文件位于父目录中,或者:
如果 .pem 文件位于当前工作目录中。或者,您可以使用绝对路径来确保获取正确的文件。
Your paths appear to be incorrect:
and
Should be
If the .pem file is in the parent directory, or:
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.