php SHA256WithRSA 簽名驗簽&加密解密


廢話不多說,直接上代碼!

使用:

$pri_key =""
$pub_key = "";
$char = '方方塊兒';//要加密的字符
$sign = $this->RsaEncrypt($char,$pri_key);//加密結果
 $result = $this->RsaDecrypt($sign,$pub_key);//對加密結果進行解密

方法:

加密:

public function RsaEncrypt($str,$pri_key){
        $pi_key =openssl_pkey_get_private($pri_key);
        if(!$pi_key)return false;//秘鑰不可用
        openssl_private_encrypt($str,$encrypted,$pi_key);
        $encrypted =base64_encode($encrypted);
        return $encrypted;
}

解密:
public function RsaDecrypt($str,$pub_key){
        $pu_key =openssl_pkey_get_public($pub_key);
        if(!$pu_key)return false;//秘鑰不可用
        openssl_public_decrypt(base64_decode($str),$decrypted,$pu_key);
        return $decrypted;
}

注:開啟PHP的php_openssl擴展


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM