Debug School

rakesh kumar
rakesh kumar

Posted on

openssl_public_encrypt(): key parameter is not a valid public key

My Error

 openssl_public_encrypt($type_of_tasks, $encrypted, $pubskey );
Enter fullscreen mode Exit fullscreen mode

Solution:

The error message "openssl_public_encrypt(): key parameter is not a valid public key" indicates that the key you are using is not a valid public key.

This error can occur if you are trying to encrypt data using a private key instead of a public key. Public keys are used to encrypt data, while private keys are used to decrypt data. Therefore, if you are trying to use a private key for encryption, you will get this error.

Another reason for this error is that the public key you are using is not in the correct format. OpenSSL expects public keys to be in the PEM format, which is a base64-encoded format with header and footer lines indicating the type of key. If your public key is not in this format, you will need to convert it to the PEM format before using it with OpenSSL.

To resolve this error, make sure that you are using a valid public key in the correct format. If you are still having trouble, double-check that you are passing the correct parameters to the openssl_public_encrypt() function.

step1: while generating public key then first convert in encoded format then saved in database

Image description

Step2: in php myadmin database field size 512 and datatype is varchar

Image description
step 3: while encrypting data using public key retrive from database that is public key
first decode public key then encrypt data and check validation condition for public key

Image description

Top comments (0)