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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵