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.

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况