成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

PHP+openssl實(shí)現(xiàn)非對(duì)稱(chēng)加密

animabear / 1269人閱讀

摘要:使用實(shí)現(xiàn)非對(duì)稱(chēng)加密私鑰公鑰保存文件地址公鑰私鑰創(chuàng)建公鑰和私鑰生成私鑰生成公鑰設(shè)置私鑰從文件中獲取設(shè)置公鑰從文件中獲取數(shù)據(jù)源用私鑰加密

_keyPath = $path;
        }
    }

    /**
     * 創(chuàng)建公鑰和私鑰
     * 
     */
    public function createKey()
    {
        $config = [
            "config" => "D:MinInstallwampwamp64inphpphp5.6.25extrassslopenssl.cnf",
            "digest_alg" => "sha512",
            "private_key_bits" => 4096,
            "private_key_type" => OPENSSL_KEYTYPE_RSA,

        ];
        // 生成私鑰
        $rsa = openssl_pkey_new($config);
        openssl_pkey_export($rsa, $privKey, 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);
    }

    /** 設(shè)置私鑰
     * @return bool
     */
    public function setupPrivKey()
    {
        if (is_resource($this->_privKey)) {
            return true;
        }
        //從文件中獲取
        /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . "priv.key";
        $privKey = file_get_contents($file);*/
        $privKey = $this->_priKeyLink;
        $this->_privKey = openssl_pkey_get_private($privKey);
        return true;
    }

    /** 設(shè)置公鑰
     * @return bool
     */
    public function setupPubKey()
    {
        //從文件中獲取
        /*$file = $this->_keyPath . DIRECTORY_SEPARATOR . "pub.key";
        $pubKey = file_get_contents($file);*/
        //數(shù)據(jù)源
        $pubKey = $this->_pubKeyLink;
        $this->_pubKey = openssl_pkey_get_public($pubKey);
        return true;
    }

    /** 用私鑰加密
     * @param $data
     * @return null|string
     */
    public function privEncrypt($data)
    {
        if (!is_string($data)) {
            return null;
        }
        $this->setupPrivKey();
        $result = openssl_private_encrypt($data, $encrypted, $this->_privKey);
        if ($result) {
            return base64_encode($encrypted);
        }
        return null;
    }

    /** 私鑰解密
     * @param $encrypted
     * @return null
     */
    public function privDecrypt($encrypted)
    {
        if (!is_string($encrypted)) {
            return null;
        }
        $this->setupPrivKey();
        $encrypted = base64_decode($encrypted);
        $result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
        if ($result) {
            return $decrypted;
        }
        return null;
    }

    /** 公鑰加密
     * @param $data
     * @return null|string
     */
    public function pubEncrypt($data)
    {
        if (!is_string($data)) {
            return null;
        }
        $this->setupPubKey();
        $result = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
        if ($result) {
            return base64_encode($encrypted);
        }
        return null;
    }

    /** 公鑰解密
     * @param $crypted
     * @return null
     */
    public function pubDecrypt($crypted)
    {
        if (!is_string($crypted)) {
            return null;
        }
        $this->setupPubKey();
        $crypted = base64_decode($crypted);
        $result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
        if ($result) {
            return $decrypted;
        }
        return null;
    }

    /** 私鑰簽名
     * @param $data
     * @return string
     */
    public function priKeySign($data)
    {
        if(!is_string($data)) return null;
        $private_key=openssl_get_privatekey($this->_priKeyLink);
        $original_str= $data ;//原數(shù)據(jù)
        openssl_sign($original_str,$sign,$private_key);
        openssl_free_key($private_key);
        $sign=base64_encode($sign);//最終的簽名
        return $sign ;
    }

    /** 公鑰驗(yàn)簽
     * @param $sign
     * @param $data
     * @return bool
     */
    public  function pubKeyCheck($sign,$data)
    {
        if(!is_string($sign) || !is_string($data)) return null;
        $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);
        return $result ;
    }

    /**
     * __destruct
     * 
     */
    public function __destruct() {
        @fclose($this->_privKey);
        @fclose($this->_pubKey);
    }
}
$rsa = new Rsa();

echo "openssl_private_encrypt,openssl_public_decrypt","
"; //私鑰加密,公鑰解密 echo "私鑰加密,公鑰驗(yàn)簽","
"; echo "待加密數(shù)據(jù):testInfo","
"; $pre = $rsa->privEncrypt("testInfo"); echo "加密后的密文:
" . $pre . "
"; $pud = $rsa->pubDecrypt($pre); echo "解密后數(shù)據(jù):" . $pud . "
"; echo "
"; //公鑰加密,私鑰解密 echo "openssl_public_encrypt,openssl_private_decrypt","
"; echo "公鑰加密,私鑰驗(yàn)簽","
"; echo "待加密數(shù)據(jù):ssh-test","
"; $pue = $rsa->pubEncrypt("ssh-test"); echo "加密后的密文:","
" . $pue . "
"; $prd = $rsa->privDecrypt($pue); echo "解密后數(shù)據(jù):" . $prd; echo "
";echo "
"; echo "openssl_sign,openssl_verify","
"; echo "私鑰簽名,公鑰驗(yàn)簽","
"; echo "待加密數(shù)據(jù):test=32","
"; $pre = $rsa->priKeySign("test=32"); echo "加密后的密文:","
" . $pre . "
"; $pud = $rsa->pubKeyCheck($pre,"test=32"); echo "是否解密成功:" . $pud . "
"; echo "
";

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/30984.html

相關(guān)文章

  • PHPopenssl加密擴(kuò)展使用小結(jié)

    摘要:非對(duì)稱(chēng)加密與對(duì)稱(chēng)加密相對(duì)的是非對(duì)稱(chēng)加密,非對(duì)稱(chēng)加密的核心思想是使用一對(duì)相對(duì)的密匙,分為公匙和私匙,私匙自己安全保存,而將公匙公開(kāi)。 引言 互聯(lián)網(wǎng)的發(fā)展史上,安全性一直是開(kāi)發(fā)者們相當(dāng)重視的一個(gè)主題,為了實(shí)現(xiàn)數(shù)據(jù)傳輸安全,我們需要保證:數(shù)據(jù)來(lái)源(非偽造請(qǐng)求)、數(shù)據(jù)完整性(沒(méi)有被人修改過(guò))、數(shù)據(jù)私密性(密文,無(wú)法直接讀?。┑取km然現(xiàn)在已經(jīng)有SSL/TLS協(xié)議實(shí)現(xiàn)的HTTPS協(xié)議,但是因在客戶(hù)...

    dockerclub 評(píng)論0 收藏0
  • 對(duì)稱(chēng)加密

    摘要:與對(duì)稱(chēng)加密不同的是,非對(duì)稱(chēng)加密和解密使用的是不同的密鑰,其中一個(gè)對(duì)外公開(kāi)作為公鑰,另一個(gè)只有所有者擁有,稱(chēng)為私鑰。中提供基于算法的擴(kuò)展可實(shí)現(xiàn)對(duì)數(shù)據(jù)的非對(duì)稱(chēng)加密。 與對(duì)稱(chēng)加密不同的是,非對(duì)稱(chēng)加密和解密使用的是不同的密鑰,其中一個(gè)對(duì)外公開(kāi)作為公鑰,另一個(gè)只有所有者擁有,稱(chēng)為私鑰。用私鑰加密的信息只有公鑰才能解開(kāi),或者反之用弓腰加密的信息只有私鑰才能解開(kāi)。常用的非對(duì)稱(chēng)加密有RSA算法,RSA...

    MobService 評(píng)論0 收藏0
  • 對(duì)稱(chēng)加密提交表單到PHP

    摘要:非對(duì)稱(chēng)加密提交表單到首先用工具生成一對(duì)非對(duì)稱(chēng)密鑰然后在前端引入庫(kù)用于非對(duì)稱(chēng)加密再綁定的事件對(duì)表單需加密數(shù)據(jù)進(jìn)行加密處理然后就可以在后端中通過(guò)方法解析傳輸過(guò)來(lái)的加密數(shù)據(jù)了 非對(duì)稱(chēng)加密提交表單到PHP 首先用openssl工具生成一對(duì)RSA非對(duì)稱(chēng)密鑰 openssl genrsa -out rsa_1024_priv.pem 1024 openssl rsa -pubout -in rsa...

    sherlock221 評(píng)論0 收藏0
  • 對(duì)稱(chēng)加密提交表單到PHP

    摘要:非對(duì)稱(chēng)加密提交表單到首先用工具生成一對(duì)非對(duì)稱(chēng)密鑰然后在前端引入庫(kù)用于非對(duì)稱(chēng)加密再綁定的事件對(duì)表單需加密數(shù)據(jù)進(jìn)行加密處理然后就可以在后端中通過(guò)方法解析傳輸過(guò)來(lái)的加密數(shù)據(jù)了 非對(duì)稱(chēng)加密提交表單到PHP 首先用openssl工具生成一對(duì)RSA非對(duì)稱(chēng)密鑰 openssl genrsa -out rsa_1024_priv.pem 1024 openssl rsa -pubout -in rsa...

    zoomdong 評(píng)論0 收藏0
  • PHP實(shí)現(xiàn)對(duì)稱(chēng)加密

    摘要:非對(duì)稱(chēng)加密至于什么是非對(duì)稱(chēng)加密,這里就不說(shuō)啦,大家谷歌去吧。這里說(shuō)明的是,最近在做一個(gè)對(duì)外的充值加密服務(wù),那么涉及到這個(gè)加密的處理,中間遇到幾個(gè)小問(wèn)題,所以記錄下,方便自己下次查閱。 非對(duì)稱(chēng)加密 至于什么是非對(duì)稱(chēng)加密,這里就不說(shuō)啦,大家谷歌去吧。這里說(shuō)明的是,最近在做一個(gè)對(duì)外的充值加密服務(wù),那么涉及到這個(gè)加密的處理,中間遇到幾個(gè)小問(wèn)題,所以記錄下,方便自己下次查閱。 詳細(xì)代碼 測(cè)試...

    Euphoria 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<