0

This is my first encryption try out. This is my code:

$privateKey = openssl_pkey_new(array(
    'private_key_bits' => 2048, //size of key
    'private_key_type' => OPENSSL_KEYTYPE_RSA,
));

//save the private key to private.key file. Never share this file with anyone.
openssl_pkey_export_to_file( $privateKey, 'private.key' );

//generate the public key for the private key
$a_key = openssl_pkey_get_details( $privateKey );
//save the public key in public.key file. Send this file to anyone who wants to send you the encrypted data.
file_put_contents( 'public.key', $a_key['key'] );

//free the private key
openssl_free_key( $privateKey );

I get the following errors;

Warning: openssl_pkey_export_to_file(): cannot get key from parameter 1 in C:\xampp\htdocs\PHP\25000.php on line 40

Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\PHP\25000.php on line 43

Warning: openssl_free_key() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\PHP\25000.php on line 48

I already searched SO. I found that I have to do following:

  • Install OpenSSL in XAMPP
  • Turn on PHP and Apache SSL mods

I am not sure if I have done these and I wonder if that is the reason for my error?

jww
  • 97,681
  • 90
  • 411
  • 885
Amali Perera
  • 115
  • 1
  • 1
  • 8
  • 2
    You should always check the return value of the functions. What does openssl_pkey_new return? If it returns false then you should not continue with calling other openssl functions. – John Fawcett Jun 11 '16 at 11:31
  • 1
    It generates what kind of key? Do you mean "public key"? – Håken Lid Jun 11 '16 at 12:11
  • @HåkenLid, sir I can see only the public key in my htdoc folder XAMPP. – Amali Perera Jun 11 '16 at 15:34
  • I have that stupid [hammer badge and I can't opt-out of it](http://meta.stackoverflow.com/q/323155). That means I can't cite a potential duplicate without closing the question. Please let me know if you object. Also see [OpenSSL and error in reading openssl.conf file](http://stackoverflow.com/q/7360602) and [Why does openssl_pkey_new() fail?](http://stackoverflow.com/q/4282507) – jww Jun 11 '16 at 22:00

0 Answers0