PHPで非対称暗号化を実装する方法

藏色散人
リリース: 2023-03-05 12:28:02
オリジナル
1601 人が閲覧しました

php で非対称暗号化を実装する方法: 最初に PHP サンプル ファイルを作成し、次に openssl を使用して非対称暗号化を実装し、最後に "$rsa = new Rsa('ssl-key');" を通じてテストします。

PHPで非対称暗号化を実装する方法

# 推奨: 「

PHP ビデオ チュートリアル

PHP は非対称暗号化を実装します


##非対称暗号化とは何かについては、ここでは説明しません。Google で調べてください。ここで説明するのは、最近外部リチャージ暗号化サービスに取り組んでおり、暗号化処理に関して、その過程でいくつか小さな問題が発生したので、次回確認するために記録しておきます。

詳細なコード

<?php
/**
 * 使用openssl实现非对称加密
 * 
 * @since 2015-11-10
 */
class Rsa
{
    /**
     * 私钥
     * 
     */
    private $_privKey;
    /**
     * 公钥
     * 
     */
    private $_pubKey;
    /**
     * 保存文件地址
     */
    private $_keyPath;
    /**
     * 指定密钥文件地址
     * 
     */
    public function __construct($path)
    {
        if (empty($path) || !is_dir($path)) {
            throw new Exception(&#39;请指定密钥文件地址目录&#39;);
        }
        $this->_keyPath = $path;
    }
    /**
     * 创建公钥和私钥
     * 
     */
    public function createKey()
    {
        $config = [
            "config" => &#39;D:\wamp\bin\apache\apache2.4.9\conf\openssl.cnf&#39;,
            "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 . &#39;priv.key&#39;, $privKey);
        $this->_privKey = openssl_pkey_get_public($privKey);
        // 生成公钥
        $rsaPri = openssl_pkey_get_details($rsa);
        $pubKey = $rsaPri[&#39;key&#39;];
        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . &#39;pub.key&#39;, $pubKey);
        $this->_pubKey = openssl_pkey_get_public($pubKey);
    }
    /**
     * 设置私钥
     * 
     */
    public function setupPrivKey()
    {
        if (is_resource($this->_privKey)) {
            return true;
        }
        $file = $this->_keyPath . DIRECTORY_SEPARATOR . &#39;priv.key&#39;;
        $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 . &#39;pub.key&#39;;
        $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(&#39;ssl-key&#39;);
//私钥加密,公钥解密
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 グローバル変数を設定するだけです。

以上がPHPで非対称暗号化を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!