How to solve the problem of OpenSSL? Pkey? Export(): cannot get key from parameter 1 in windows system

In order to solve this problem, I Baidu many methods, but many methods have no effect.

Method 1:

If you are installing phpstudy, then

1. You need to go to php extension to open the extension.

2. Go to php.ini and remove the ";" before "extension=php_openssl.dll". If not, add extension=php_openssl.dll.

3. Copy libeay32.dll and ssleay32.dll in the PHP installation directory to c:\windows\system32 (if it is apache, these two files are in E:\phpStudy\PHPTutorial\php\php-7.0.12-nts).

4. Copy php_openssl.dll to C: \ windows \ system32 (if it is apache, the file is in E:\phpStudy\PHPTutorial\php\php-7.0.12-nts\ext).

5. Restart IIS or apache environment

If method 1 still doesn't work, you can add a variable to the environment variable,

  1. This may help if you are on windows:

  2. Click on the START button
  3. Click on CONTROL PANEL
  4. Click on SYSTEM AND SECURITY
  5. Click on SYSTEM
  6. Click on ADVANCED SYSTEM SETTINGS
  7. Click on ENVIRONMENT VARIABLES
  8. Under "System Variables" click on "NEW"
  9. Enter the "Variable name" OPENSSL_CONF
  10. Enter the "Variable value". My is - C:\wamp\bin\apache\Apache2.2.17\conf\openssl.cnf
  11. Click "OK" and close all the windows and RESTART your computer.
  12. The OPENSSL should be correctly working.

This means to set an environment variable and put the path of openssl.cnf into the environment variable. I saw this method in foreign forums, but the effect is not good.

Method two:

Code for

 1 <?php
 2  
 3 $configs['config'] = 'E:\phpStudy\PHPTutorial\Apache\conf\openssl.cnf';
 4  
 5 $config = array(
 6     //"digest_alg" => "sha512",
 7     "private_key_bits" => 512,                     //Bytes    512 1024 2048 4096, etc
 8     "private_key_type" => OPENSSL_KEYTYPE_RSA,     //Encryption type
 9 );
10     
11 //Create public and private key return resources
12 $res = openssl_pkey_new($config+$configs);
13  
14 //Get the private key from the obtained resource    And assign the private key to $<span style="font-family: Arial, Helvetica, sans-serif;">privKey</span>
15 openssl_pkey_export($res, $privKey, null, $config);
16  
17 //<span style="font-family: Arial, Helvetica, sans-serif;">Get the public key from the obtained resource    Return to public key </span><span style="font-family: Arial, Helvetica, sans-serif;">$pubKey</span><span style="font-family: Arial, Helvetica, sans-serif;">
18 $pubKey = openssl_pkey_get_details($res);
19  
20 $pubKey = $pubKey["key"];
21 //var_dump($privKey);
22 //var_dump($pubKey);
23 var_dump(array('privKey'=>$privKey,'pubKey'=>$pubKey));
24 die;

The result is

array(2) { ["privKey"]=> NULL ["pubKey"]=> string(182) "-----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPSkEAT0A8zZKBjWSph22wn2m3mI947X 7wbMDnAju94LMTZ4uVSqjVMvSMGC35OcmHQr3c5HUFalJCmyYXU3IEkCAwEAAQ== -----END PUBLIC KEY----- " }

But this method is not very ideal, because every time you generate a public key and a private key, you need to call $configs ['config '] =' e: \ phpstudy \ phptutorial \ Apache \ conf \ OpenSSL. CNF ';, isn't it a bit troublesome? Hahaha, OK, I'll give you the last method. I think this method is very good

Method three:

First, find it in phpinfo, as shown in the figure below

Create the corresponding path, copy openssl.cnf in apache, and then you can try the test results

Test method:

 1 <?php
 2  
 3 $config = array(
 4     "digest_alg"    => "sha512",
 5     "private_key_bits" => 4096,           //Number of bytes: 512, 1024, 2048, 4096, etc ,You can't use quotation marks. The length here is related to the length of the encrypted string. You can test it yourself
 6     "private_key_type" => OPENSSL_KEYTYPE_RSA,   //Encryption type
 7   );
 8 $res =    openssl_pkey_new($config); 
 9  
10 //Extract private key
11 openssl_pkey_export($res, $private_key);
12  
13 //Generate public key
14 $public_key = openssl_pkey_get_details($res);
15 // var_dump($public_key);
16  
17 $public_key=$public_key["key"];
18  
19 //Display data
20 var_dump($private_key);    //Private key
21 echo "<br/>";
22 var_dump($public_key);     //Public key
23 echo "<br/>";
24 //Data to encrypt
25 $data = "http://www.cnblogs.com/wt645631686/";
26 echo 'Encrypted data:'.$data."\r\n";  
27 echo "<br/>";
28 //Data encrypted by private key
29 openssl_private_encrypt($data,$encrypted,$private_key);
30  
31 //Encrypted content usually contains special characters and requires base64 Code conversion down
32 $encrypted = base64_encode($encrypted);
33 echo "Data encrypted by private key:".$encrypted."\r\n";  
34 echo "<br/>";
35 //Public key decryption  
36 openssl_public_decrypt(base64_decode($encrypted), $decrypted, $public_key);
37 echo "Data after public key decryption:".$decrypted,"\r\n";  
38   echo "<br/>";
39 //----Reverse operation. Public key encryption 
40 openssl_public_encrypt($data, $encrypted, $public_key);
41 $encrypted = base64_encode($encrypted);  
42 echo "Data encrypted by public key:".$encrypted."\r\n";
43 echo "<br/>";
44 openssl_private_decrypt(base64_decode($encrypted), $decrypted, $private_key);//Private key decryption  
45 echo "Data after private key decryption:".$decrypted."n";
46  
47 echo "<br/>";
48  
49 echo "---------------------------------------Dividing line---------------------------------------";
50  
51 echo "<br/>";

Keywords: PHP Apache OpenSSL Windows

Added by Aretai on Sat, 14 Dec 2019 17:33:49 +0200