爲了解決這個問題我百度了不少方法,但是不少方法並無效果。php
方法一:
若是你安裝的是phpstudy這個集成環境,那麼html
一、你就要去php拓展裏面去打開php_opemssl中打開這個擴展。apache
二、去php.ini裏面設置,把「extension=php_openssl.dll」前面的「;」去掉,若是沒有這個就添加extension=php_openssl.dll這個。windows
三、複製php安裝目錄中的: libeay32.dll、ssleay32.dll至c:\windows\system32(若是是apache這兩個文件在E:\phpStudy\PHPTutorial\php\php-7.0.12-nts)。測試
四、複製php_openssl.dll至c:\windows\system32(若是是apache,那麼這個文件在E:\phpStudy\PHPTutorial\php\php-7.0.12-nts\ext)。編碼
五、重啓IIS或者apache環境加密
若是方法一仍是沒有用,那麼你能夠在環境變量中去添加一個變量試試看,spa
-
This may help if you are on windows:code
- Click on the START button
- Click on CONTROL PANEL
- Click on SYSTEM AND SECURITY
- Click on SYSTEM
- Click on ADVANCED SYSTEM SETTINGS
- Click on ENVIRONMENT VARIABLES
- Under "System Variables" click on "NEW"
- Enter the "Variable name" OPENSSL_CONF
- Enter the "Variable value". My is - C:\wamp\bin\apache\Apache2.2.17\conf\openssl.cnf
- Click "OK" and close all the windows and RESTART your computer.
-
The OPENSSL should be correctly working.htm
這個意思是設置一個環境變量,將openssl.cnf的路徑放到環境變量中,這個我是在國外的論壇看到的方法,可是效果也很差。
方法二:
代碼爲
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, //字節數 512 1024 2048 4096 等 8 "private_key_type" => OPENSSL_KEYTYPE_RSA, //加密類型 9 ); 10 11 //建立公鑰和私鑰 返回資源 12 $res = openssl_pkey_new($config+$configs); 13 14 //從獲得的資源中獲取私鑰 並把私鑰賦給$<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;">從獲得的資源中獲取公鑰 返回公鑰 </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;
結果爲
array(2) { ["privKey"]=> NULL ["pubKey"]=> string(182) "-----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPSkEAT0A8zZKBjWSph22wn2m3mI947X 7wbMDnAju94LMTZ4uVSqjVMvSMGC35OcmHQr3c5HUFalJCmyYXU3IEkCAwEAAQ== -----END PUBLIC KEY----- " }
可是這個方法不是很理想,由於每次生成公鑰和私鑰都要調用$configs['config'] = 'E:\phpStudy\PHPTutorial\Apache\conf\openssl.cnf';,是否是有點麻煩呀,哈哈哈,好了,給你最後一個方法,我的以爲這個方法卻是挺好的
方法三:
首先在phpinfo中找到以下圖所示
建立對應的路徑,將apache中的openssl.cnf複製進去,而後就能夠嘗試測試結果了
測試方法:
1 <?php 2 3 $config = array( 4 "digest_alg" => "sha512", 5 "private_key_bits" => 4096, //字節數 512 1024 2048 4096 等 ,不能加引號,此處長度與加密的字符串長度有關係,能夠本身測試一下 6 "private_key_type" => OPENSSL_KEYTYPE_RSA, //加密類型 7 ); 8 $res = openssl_pkey_new($config); 9 10 //提取私鑰 11 openssl_pkey_export($res, $private_key); 12 13 //生成公鑰 14 $public_key = openssl_pkey_get_details($res); 15 // var_dump($public_key); 16 17 $public_key=$public_key["key"]; 18 19 //顯示數據 20 var_dump($private_key); //私鑰 21 echo "<br/>"; 22 var_dump($public_key); //公鑰 23 echo "<br/>"; 24 //要加密的數據 25 $data = "http://www.cnblogs.com/wt645631686/"; 26 echo '加密的數據:'.$data."\r\n"; 27 echo "<br/>"; 28 //私鑰加密後的數據 29 openssl_private_encrypt($data,$encrypted,$private_key); 30 31 //加密後的內容一般含有特殊字符,須要base64編碼轉換下 32 $encrypted = base64_encode($encrypted); 33 echo "私鑰加密後的數據:".$encrypted."\r\n"; 34 echo "<br/>"; 35 //公鑰解密 36 openssl_public_decrypt(base64_decode($encrypted), $decrypted, $public_key); 37 echo "公鑰解密後的數據:".$decrypted,"\r\n"; 38 echo "<br/>"; 39 //----相反操做。公鑰加密 40 openssl_public_encrypt($data, $encrypted, $public_key); 41 $encrypted = base64_encode($encrypted); 42 echo "公鑰加密後的數據:".$encrypted."\r\n"; 43 echo "<br/>"; 44 openssl_private_decrypt(base64_decode($encrypted), $decrypted, $private_key);//私鑰解密 45 echo "私鑰解密後的數據:".$decrypted."n"; 46 47 echo "<br/>"; 48 49 echo "---------------------------------------分割線---------------------------------------"; 50 51 echo "<br/>";
原文出處:https://www.cnblogs.com/wadhf/p/12041267.html