开发者

[function.openssl-public-encrypt]: key parameter is not a valid public key

开发者 https://www.devze.com 2023-03-20 04:51 出处:网络
Long time listener/first time caller I have set up a test page as follows <form action=\"\" method=\"post\">

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 err开发者_如何学Pythonor

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号