PHP rsa加密生成(加密通信)

<?php
/**
 * User: orzblankcat
 * Date: 2019/1/25
 * Time: 17:30
 */
class RsaClass
{
    protected $config = [
//        "digest_alg"    => 'sha512',               //加密模式
        "private_key_bits" => 4096,                  //字節(jié)數(shù)  512 1024 2048  4096 等
        "private_key_type" => OPENSSL_KEYTYPE_RSA,   //加密類型
        "config" => "E:/phpStudy/PHPTutorial/Apache/conf/openssl.cnf",
        "encrypt_key"=>true //私鑰加密
    ];
    protected $passphrase = '123456';//密碼 當(dāng)encrypt_key=>true必填
    protected $pi_key='';
    protected $pu_key='';
    protected $priPath='Common/rsa/pri.key';
    protected $pubPath='Common/rsa/pub.key';

    public function __construct()
    {
        extension_loaded('openssl') or die('php需要openssl擴(kuò)展支持');
        if(!file_exists($this->pubPath)||!file_exists($this->priPath))
        {
            $this->createKey();
        }
    }

    public function createKey()
    {
        $res = openssl_pkey_new($this->config);
        //提取私鑰
        openssl_pkey_export($res, $private_key, $this->passphrase,$this->config);

        //生成公鑰
        $public_key = openssl_pkey_get_details($res);
        $public_key = $public_key["key"];

        //顯示數(shù)據(jù)
        $pri = fopen($this->priPath, "w") or die("Unable to open file!");
        fwrite($pri, $private_key);

        $pub = fopen($this->pubPath, "w") or die("Unable to open file!");
        fwrite($pub, $public_key);

        fclose($pri);
        fclose($pub);
    }

    /**
     * 公鑰加密數(shù)據(jù)
     */
    public function pubEncryptKey($data)
    {
        $this->pu_key = openssl_pkey_get_public(file_get_contents($this->pubPath));
        openssl_public_encrypt($data, $encrypted, $this->pu_key);
        return base64_encode($encrypted);
    }
    /**
     * 公鑰解密數(shù)據(jù)
    */
    public function pubDecodeKey($data)
    {
        $this->pu_key = openssl_pkey_get_public(file_get_contents($this->pubPath));
        openssl_public_decrypt(base64_decode($data), $decrypted, $this->pu_key);//公鑰解密
        return $decrypted;
    }
    /**
     * 私鑰加密數(shù)據(jù)
    */
    public function priEncryptKey($data)
    {
        $this->pi_key = openssl_pkey_get_private(file_get_contents($this->priPath),$this->passphrase);
        openssl_private_encrypt($data, $encrypted, $this->pi_key);
        return base64_encode($encrypted);
    }
    /**
     * 私鑰解密數(shù)據(jù)
     */
    public function priDecodeKey($data)
    {
        $this->pi_key = openssl_pkey_get_private(file_get_contents($this->priPath),$this->passphrase);
        openssl_private_decrypt(base64_decode($data), $decrypted, $this->pi_key);//私鑰解密
        return $decrypted;
    }

}

使用的時(shí)候

$Rsa = new RsaClass();

$result = $Rsa->pubEncryptKey(time());
var_dump($result);
$decoded = $Rsa->priDecodeKey($result);
var_dump($decoded);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容