简体   繁体   中英

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

function encrypt($string) {

    $key = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIiJg//yXqt7ln+orfSuRjH0FjTKQyj6ti2tX8PR8yhvoeocfHg0RtBNBJFaghoezGgbsmsUcac8GSgoiL9kzzMCAwEAAQ==";

    $keyFinal = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split($key) . "\r\n-----END PUBLIC KEY-----";

    openssl_public_encrypt($string, $encrypted_data, $keyFinal);

    echo base64_encode($encrypted_data); 
}

(Every other key I try gives the same error, too)

am trying to encrypt a string using rsa but i get an error (openssl_public_encrypt(): key parameter is not a valid public key)

i have been trying to do this for hours now please help

The function chunk_split includes a trailing separator, which in your case is \r\n . So in your case, just don't concatenate an extra one in.

Change this:

$keyFinal = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split($key) . "\r\n-----END PUBLIC KEY-----";

To this:

$keyFinal = "-----BEGIN PUBLIC KEY-----\r\n" . chunk_split($key) . "-----END PUBLIC KEY-----";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM