Get the key parameter is not a valid public key error in openssl_public_encrypt()

key param is not a valid private key
phpseclib
caused by exception unable to extract public key
extract rsa public key from certificate
extract public key from certificate
get public key from certificate php
php openssl_public_encrypt
rsa encryption in php

$publicKey = "../ssh/public/pub"; $plaintext = "String to encrypt";

$pubKey = openssl_pkey_get_public($publicKey);

openssl_public_encrypt($plaintext, $encrypted, $pubKey);

echo $encrypted;   //encrypted string

above code generating following error

openssl_public_encrypt() [http://php.net/function.openssl-public-encrypt]: key parameter is not a valid public key [APP/controllers/supportservice_controller.php, line 144]

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

In PHP 7.x and new versions of phpseclib (a pure PHP RSA implementation) and using composer to install phpseclib, you can do this:

    # Install the phpseclib from console
    composer require phpseclib/phpseclib:~2.0
    // In your php script:

    use phpseclib\Crypt\RSA;

    $rsa = new RSA();
    $rsa->loadKey($publicKey); # $publicKey is an string like "QEFAAOCAQ8AMIIBCgKCAQEAoHcbG....."
    $plaintext = '...';
    $ciphertext = $rsa->encrypt($plaintext);

    var_dump($ciphertext);

    #to decrypt:
    $rsa->loadKey('...'); // private key
    echo $rsa->decrypt($ciphertext);```



Get the key parameter is not a valid public key error , In PHP 7.x and new versions of phpseclib (a pure PHP RSA implementation) and using composer to install phpseclib, you can do this: # Install  Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in.. Is there some thing that i am doing wrong in my code? Help Please!

The public key has to be encapsulated in an X.509 certificate when using OpenSSL's functions in PHP. You can create this with a CSR. Or you can use phpseclib, a pure PHP RSA implementation, and use the raw public key directly. eg.

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey('...'); // public key

$plaintext = '...';

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($plaintext);

[function.openssl-public-encrypt]: key parameter is not a valid public , 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  encryption - PHP openssl_public_encrypt "key parameter is not a valid key" PHP OpenSSL Public Key Encryption With String Public Key; encryption - public key not valid php rsa; openssl - store public key in variable php; encryption - Encrypt a String with a given RSA public key OpenSSL

Like this you can add your key and encrypt the text

  $data = json_decode(file_get_contents('php://input'), true);
  $enctext = $data['enctext'];

  $pubkey = '-----BEGIN PUBLIC KEY-----
             PUBLIC  KEY PLACED HERE
             -----END PUBLIC KEY-----';

  openssl_public_encrypt($enctext, $crypted, $pubkey);
  $data['enctext'] =  $enctext;
  $data['Encryption_text'] = base64_encode($crypted);
  echo json_encode($data);
  exit;

Or instead of this you can also call a .cert file of public key

  $fp=fopen("publickey.crt","r"); 
  $pub_key_string=fread($fp,8192); 
  fclose($fp); 
  $key_resource = openssl_get_publickey($pub_key_string); 

  openssl_public_encrypt($enctext, $crypted, $key_resource );
  $data['enctext'] =  $enctext;
  $data['Encryption_text'] = base64_encode($crypted);
  echo json_encode($data);
  exit;

openssl_public_encrypt(): key parameter is not a valid public key , Estou com problemas no método getBearerToken() da class Config. openssl_public_encrypt(): key parameter is not a valid public key #4 to implement it on an hosted application (on the server) it returns this error message​. You should get an output saying bearer token missing, meaning connection is successful and  Warning: openssl_public_encrypt() function.openssl-public-encrypt: key parameter is not a valid public key in I'm not sure why this is happening. This is what the key looks like when I echo it:

In my case,I Splited the public key into mutiple lines,solved the problem.

PHP Version 7.1.17

    $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

    $str = "str to be encrypted";

    $opensslPublicEncrypt = openssl_public_encrypt($str, $encrypted, $publicKey);

Openssl: Problem With The Key To Encrypt And Decrypt, openssl: problem with the key to encrypt and decrypt: Warning: openssl_public_encrypt(): key parameter is not a valid public key in /var/www/​html/test/index.php on line 9 Use a function like file_get_contents to read the file and get the data. I've tried with file_get_contents(), but the problem persists. openssl_public_encrypt() encrypts data with public key and stores the result into crypted. Encrypted data can be decrypted via openssl_private_decrypt(). This function can be used e.g. to encrypt message which can be then read only by owner of the private key. It can be also used to store secure data in database.

key parameter is not a valid public key error in , openssl_public_encrypt() encrypts data with public key and stores the result into crypted . This function is not intended for general encryption and decryption. error:0906D06C:PEM routines:PEM_read_bio:no start line OR same as the parameter of http://www.php.net/manual/en/function.openssl-pkey-get-public.php Warning: openssl_public_encrypt(): key parameter is not a valid public key in /../gstnapiCall.php on line 45 It is almost certainly something wrong with your code. People are unlikely to be able to help you without looking at the code.

openssl_public_encrypt - Manual, In PHP 7.x and new versions of phpseclib (a pure PHP RSA implementation) and using composer to install phpseclib, you can do this: # Install the phpseclib  If you are trying to read a PKCS#1 RSA public key you run into trouble, because openssl wants the public key in X.509 style. The PKCS#1 RSA public key -----BEGIN RSA PUBLIC KEY-----

Get the key parameter is not a valid public key error in , above code generating following error. openssl_public_encrypt() [http://php.net/​function.openssl-public-encrypt]: key parameter is not a valid public key  The dwFlags parameter specified is not valid. NTE_BAD_TYPE: The key BLOB type is not supported by this CSP and is possibly not valid. NTE_BAD_UID: The hProv parameter does not contain a valid context handle. NTE_BAD_VER: The version number of the key BLOB does not match the CSP version. This usually indicates that the CSP needs to be upgraded.

Comments