加密已正确完成,但何时使用openssl_get_privatekey
并保存在$key_resource
中,此值为空值,因此会引发错误:openssl_private_decrypt(): key parameter is not a valid private key in ...
function encriptarDatos($datos){
$fp=fopen("public.key","r");
$pub_key=fread($fp,8192);
fclose($fp);
$key_resource = openssl_get_publickey($pub_key);
openssl_public_encrypt($datos,$encriptado,$key_resource );
/*uses the already existing key resource*/
return(base64_encode($encriptado));
}
function desencriptarDatos($datos){
$fp=fopen("private.key","r");
$priv_key=fread($fp,8192);
fclose($fp);
echo "priv_key: $priv_key <br/><br/>";
$key_resource = openssl_get_privatekey($priv_key);
echo "key_resourse: $key_resource";
openssl_private_decrypt($datos,$desencriptado,$key_resource);
/*uses the already existing key resource*/
return(base64_encode($desencriptado));
}
然后我改变了它:
$fp=fopen("private.key","r");
$priv_key=fread($fp,8192);
fclose($fp);
openssl_private_decrypt($datos, $desencriptado, openssl_pkey_get_private($priv_key, "phrase"));
/*uses the already existing key resource*/
return(base64_encode($desencriptado));
但结果也是null