当前位置: 首页 > 后端技术 > PHP

PHP+openssl实现非对称加密

时间:2023-03-29 17:46:56 PHP

_keyPath=$path;}}/***创建公钥和私钥**/publicfunctioncreateKey(){$config=["config"=>'D:\Min\Install\wamp\wamp64\bin\php\php5.6.25\extras\ssl\openssl.cnf',"digest_alg"=>"sha512","private_key_bits"=>4096,"private_key_type"=>OPENSSL_KEYTYPE_RSA,];//生成私钥$rsa=openssl_pkey_new($config);openssl_pkey_export($rsa,$privK嘿,NULL,$config);file_put_contents($this->_keyPath.DIRECTORY_SEPARATOR.'priv.key',$privKey);$this->_privKey=openssl_pkey_get_public($privKey);//生成公钥$rsaPri=openssl_pkey_get_details($rsa);$pubKey=$rsaPri['key'];file_put_contents($this->_keyPath.DIRECTORY_SEPARATOR.'pub.key',$pubKey);$this->_pubKey=openssl_pkey_get_public($pubKey);}/**设置私钥*@returnbool*/publicfunctionsetupPrivKey(){if(is_resource($this->_privKey)){returntrue;}//从文件中获取/*$file=$this->_keyPath.DIRECTORY_SEPARATOR.'priv.key';$privKey=file_get_contents($file);*/$privKey=$this->_priKeyLink;$this->_privKey=openssl_pkey_get_private($privKey);返回真;}/**设置公钥*@returnbool*/publicfunctionsetupPubKey(){//从文件中获取/*$file=$this->_keyPath。DIRECTORY_SEPARATOR。'pub.key';$pubKey=file_get_contents($file);*///数据源$pubKey=$this->_pubKeyLink;$this->_pubKey=openssl_pkey_get_public($pubKey);返回真;}/**使用私钥加密*@param$data*@returnnull|string*/publicfunctionprivEncrypt($data){if(!is_string($data)){returnnull;}$this->setupPrivKey();$result=openssl_private_encrypt($data,$encrypted,$this->_privKey);如果($result){返回base64_encode($encrypted);}返回空值;}/**私钥解密*@param$encrypted*@returnnull*/publicfunctionprivDecrypt($encrypted){if(!is_string($encrypted)){returnnull;}$this->setupPrivKey();$encrypted=base64_decode($encrypted);$result=openssl_private_decrypt($encrypted,$decrypted,$this->_priv键);如果($result){返回$decrypted;}返回空值;}/**公钥加密*@param$data*@returnnull|string*/publicfunctionpubEncrypt($data){if(!is_string($data)){returnnull;}$this->setupPubKey();$result=openssl_public_encrypt($data,$encrypted,$this->_pubKey);如果($result){返回base64_encode($encrypted);}返回空值;}/**公钥解密*@param$crypted*@returnnull*/publicfunctionpubDecrypt($crypted){if(!is_string($crypted)){returnnull;}$this->setupPubKey();$crypted=base64_decode($crypted);$result=openssl_public_decrypt($crypted,$decrypted,$this->_pubKey);如果($result){返回$decrypted;}返回空值;}/**私钥签名*@param$data*@returnstring*/publicfunctionpriKeySign($data){if(!is_string($data))返回空;$private_key=openssl_get_privatekey($this->_priKeyLink);$original_str=$data;//原始数据openssl_sign($original_str,$sign,$private_key);openssl_free_key($private_key);$sign=base64_encode($sign);//最后的签名return$sign;}/**公钥验证签*@param$sign*@param$data*@returnbool*/publicfunctionpubKeyCheck($sign,$data){if(!is_string($sign)||!is_string($data))返回空;$public_key=openssl_get_publickey($this->_pubKeyLink);$sign=base64_decode($sign);//得到的签名$original_str=$data;$result=(bool)openssl_verify($original_str,$sign,$public_key);openssl_free_key($public_key);返回$结果;}/***__destruct**/publicfunction__destruct(){@fclose($this->_privKey);@fclose($this->_pubKey);}}$rsa=新rsa();echo"openssl_private_encrypt,openssl_public_decrypt","
";//私钥加密,公钥解密echo"私钥加密,公钥验证","
";echo"待加密数据:testInfo","
";$pre=$rsa->privEncrypt("testInfo");echo"加密后的密文:
"。$前。"
";$pud=$rsa->pubDecrypt($pre);echo"Decrypteddata:".$布德。"
";echo"


";//公钥加密,私钥解密echo"openssl_public_encrypt,openssl_private_decrypt","
";echo"公钥加密,私钥验证","
";echo"待加密数据:ssh-test","
";$pue=$rsa->pubEncrypt("ssh-test");echo"加密密文:","
”。$普埃。"
";$prd=$rsa->privDecrypt($pue);echo"解密数据:".$prd;echo"
";echo"
";echo"openssl_sign,openssl_verify","
";echo"私钥签名,公钥验证","
";echo"待加密数据:test=32","
";$pre=$rsa->priKeySign('test=32');echo"加密后的密文:","
"。$前。"
";$pud=$rsa->pubKeyCheck($pre,'test=32');echo"解密成功:".$布德。“
”;回声“
”;