PHP實現非對稱式加密

來源:互聯網
上載者:User
非對稱式加密

至於什麼是非對稱式加密,這裡就不說啦,大家Google去吧。這裡說明的是,最近在做一個對外的儲值Data Encryption Service,那麼涉及到這個加密的處理,中間遇到幾個小問題,所以記錄下,方便自己下次查閱。

詳細代碼
_keyPath = $path;    }    /**     * 建立公開金鑰和私密金鑰     *      */    public function createKey()    {        $config = [            "config" => 'D:\wamp\bin\apache\apache2.4.9\conf\openssl.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($r);        $pubKey = $rsaPri['key'];        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);        $this->_pubKey = openssl_pkey_get_public($pubKey);    }    /**     * 設定私密金鑰     *      */    public function setupPrivKey()    {        if (is_resource($this->_privKey)) {            return true;        }        $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';        $privKey = file_get_contents($file);        $this->_privKey = openssl_pkey_get_private($privKey);        return true;    }    /**     * 設定公開金鑰     *      */    public function setupPubKey()    {        if (is_resource($this->_pubKey)) {            return true;        }        $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';        $pubKey = file_get_contents($file);        $this->_pubKey = openssl_pkey_get_public($pubKey);        return true;    }    /**     * 用私密金鑰加密     *      */    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;    }    /**     * 私密金鑰解密     *      */    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;    }    /**     * 公開金鑰加密     *      */    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;    }    /**     * 公開金鑰解密     *      */    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;    }    /**     * __destruct     *      */    public function __destruct() {        @fclose($this->_privKey);        @fclose($this->_pubKey);    }}?> 
測試
$rsa = new Rsa('ssl-key');//私密金鑰加密,公開金鑰解密echo "待加密資料:segmentfault.com\n";$pre = $rsa->privEncrypt("segmentfault.com");echo "加密後的密文:\n" . $pre . "\n";$pud = $rsa->pubDecrypt($pre);echo "解密後資料:" . $pud . "\n";//公開金鑰加密,私密金鑰解密echo "待加密資料:segmentfault.com\n";$pue = $rsa->pubEncrypt("segmentfault.com");echo "加密後的密文:\n" . $pue . "\n";$prd = $rsa->privDecrypt($pue);echo "解密後資料:" . $prd;
重要問題

這裡特別要注意的是在配置中要指定openssl.cnf的檔案地址,或者設定個OPENSSL_CONF全域變數就可以了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.