ds355020 2011-07-14 19:03
浏览 211
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • drv13270 2011-07-14 19:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊