I'm trying to encrypt some text with openssl_private_encrypt but function returns FALSE and doesn't encrypt anything.
i made two version of my code
//$privateKey = file_get_contents(__DIR__.'/key_dev_exe_1k.pem');
$privateKey = openssl_pkey_get_private(__DIR__.'/key_dev_exe_1k.pem');
var_dump(openssl_private_encrypt($referrer_string, $crypted, $privateKey));
var_dump(array($referrer_string, $crypted, $privateKey)); exit();
and
$privateKey = file_get_contents(__DIR__.'/key_dev_exe_1k.pem');
//$privateKey = openssl_pkey_get_private(__DIR__.'/key_dev_exe_1k.pem');
var_dump(openssl_private_encrypt($referrer_string, $crypted, $privateKey));
var_dump(array($referrer_string, $crypted, $privateKey)); exit();
First output contains Warning.
Warning: openssl_private_encrypt(): key param is not a valid private key
Second output is without any warnings, but shows that encryption functions return FALSE.
bool(false) //encryption function return value
array(3) {
[0]=>
string(218) "..." // here plain text WITH(!) spaces.
[1]=>
string(0) "" // $decrypt value
[2]=>
string(887) "-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCxP65lb3J9rOYLnXEwQHVkave/4t1D+7IfTgh/2L5TJeQC3mi2
[...]
iWegTa/JaMHANXGMAEA8pXIW0P8nPMV587nEqoN44g==
-----END RSA PRIVATE KEY-----
"
}
key_dev_exe_1k.pem is in this same catalog.
This works for me
//index.php
$data = "Hello World";
$fp = fopen("keys/doe.key","r");
$priv_key = fread($fp, 8192);
fclose($fp);
openssl_private_encrypt($data, $crypttext, $priv_key);
echo $crypttext;
//doe.key
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC6b/s0KbyEUUppH2IF0pr6JHSO815Sjg/HnHiJUIUYqjU35FE1
N1AfvbHzK5tpMu7FfjulJPGwbQo3OQSKN4QEPr6QFrEAQ0XcISrnhXPeGMJUPq/v
4FGi34f480Wr6wsp8gfCY06Z1A1745EWlPAFvtM4ne5psvbf/BvKGa4hywIDAQAB
AoGAEw9XsyGBMFXPMagPsWCyj1giwmqG/hGf81w/xmoA8WCVSD2DP9mDy3rtMlsG
7F5UosQg/OUmAE9ld/fQSwEZ8dhKvGGYlXnRWbEJ4Gjab7LxCrqWsJ2RQyzfe+An
ARz4KV6iP9j/Ot0gLqfVEb+ZiLl17kitNmcN34eRfJ6BW0ECQQDv6OZRx7l6Y/GP
Tw3Bq++wEiYen+Qw3x3Rz3xR78otF/oGA2Au8s1wPAo1OZTDbnix2zvROw6oyDzA
/kSqv2GjAkEAxvD+u7RHJsS+TxYGmB0xTGKg60e1UsHQKs+JVFlZQrcvqYxQC71Q
Huz2fqrlIteVzPtcEc04VfpsAIpVSlRRuQJBALBocyNjVzF12CPmn1jv0rmHPtx5
+dMt3MFwbhvZ1LI/1SsixFEBXKrQmzjz9D2YN8ugR2XcpTTauEG5NkjPnT0CQQCW
w5goo+ywArW+tZB1wtnTa4NMCuHtP5gEeZc3ZybwGmdydDVNUEDhzu7W+6f+iaaq
22rgyseA0PD8T9lnJRNxAkAXr4kEazYVMRHQkqVFNw3I3tUnl9gNel/UH3EPFQjD
YiRgQZ+BowgzAr0NPC2hWgA03LbhQEJVGcF7pGVSt/Gh
-----END RSA PRIVATE 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.