暗号化と復号化
個人情報保護法などの影響もあり、氏名やメールアドレスを暗号化しておきたいという顧客からの要望も出てきます。
データベースの中身だけが見られた場合など、多少の防衛になるかもしれません。
そこでmcriptライブラリを使わずに出来る暗号化を紹介します。
PEARのCrypt_Blowfishはライブラリ等のインストールなしで使えてとてもお手軽です。
GnuPGは外部コマンドを実行しているだけのクラスです。
OpenSSLではマニュアルにあるコードを使っています。
(OpenSSL関数群、さくらインターネットで使用可)
GnuPG関連参考:
GnuPG公式サイト
OpenPKSDプロジェクト
proc_open()
OpenSSL関連参考:
日本のLinux情報
openssl_public_encrypt()
openssl_private_decrypt()
秘密鍵と公開鍵の作成
openssl genrsa -des3 -out private.pem 1024
openssl rsa -pubout -in private.pem -out public.pemCrypt_Blowfish関連参考:
Crypt_Blowfishクラス
GnuPGで暗号化と複合化
OpenSSLで暗号化と複合化
Blowfishで暗号化と複合化
GnuPG暗号クラス
/* ======================================================================== - [Crypt/rkt_gpg.php] - 内容:GnuPG暗号クラス - 作成:高橋 裕志郎 - ライセンス: - This source file is subject to version 3.0 of the PHP license, - that is available at http://www.php.net/license/3_0.txt - If you did not receive a copy of the PHP license and are unable - to obtain it through the world-wide-web, please send a note to - license@php.net so we can mail you a copy immediately. - 問い合わせ先: - yujiro@rakuto.net - http://rakuto.net/ - Copyright (C) 2003-2006 'rakuto.net'. All Rights Reserved. - 注意事項: - GnuPG がインストールされていることを要します。 - http://www.gnupg.org/ - 更新履歴: - [2006/06/26] 作成 - ======================================================================== */ if (!defined('GPG_COMMAND_PATH')){ /** * gpgコマンドパス * @const GPG_COMMAND_PATH */ define('GPG_COMMAND_PATH','/usr/local/bin/'); } if (!defined('ERROR_LOG_FILE')){ /** * エラーログファイル * @const ERROR_LOG_FILE */ define('ERROR_LOG_FILE','error.log'); } /** * GnuPG暗号クラス * * @author 高橋 裕志郎 <yujiro@rakuto.net> * @package RKT_gpg * @access public * @version 0.1 */ class RKT_gpg { /** * パスフレーズ * @access private * @var string */ var $pass_phrase = ''; /** * 入力データ * @access private * @var string */ var $data = ''; /** * 暗号・復号データ * @access private * @var string */ var $crypted = ''; /** * コンストラクタ * * @access public * @param string $source * @return void */ function RKT_gpg($pass_phrase) { $this->pass_phrase = $pass_phrase; $this->data = ''; $this->crypted = ''; } /** * コマンドの実行 * * @access public * @param string $command コマンド名 * @param string $param パラメータ * @return boolean */ function command($command, $param) { $command = '"'.GPG_COMMAND_PATH.$command.'" '.$param; $inout = substr_count($param, ':-'); $descriptorspec = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('file', ERROR_LOG_FILE, 'a') ); $pipes = array(); $process = proc_open($command, $descriptorspec, $pipes); if (!is_resource($process)) { return false; } fwrite($pipes[0], $this->pass_phrase."\r\n".$this->data); fclose($pipes[0]); ob_start(); fpassthru($pipes[1]); $this->crypted = ob_get_contents(); ob_end_clean(); fclose($pipes[1]); proc_close($process); return true; } /** * データの暗号化 * * @access public * @param string $plain * @return binary 暗号化されたバイナリデータ */ function encrypt($plain) { $this->data = $plain; $this->crypted = ''; $param = '-o - --batch --passphrase-fd 0 -c'; $this->command('gpg', $param); return $this->crypted; } /** * データの復号化 * * @access public * @param string $crypted * @return string 複合化されたデータ */ function decrypt($crypted) { $this->data = $crypted; $this->crypted = ''; $param = '--batch --quiet --passphrase-fd 0'; $this->command('gpg', $param); return $this->crypted; } } // RKT_gpg
OpenSSL暗号クラス
/* ======================================================================== - [Crypt/rkt_openssl.php] - 内容:OpenSSL暗号クラス - 作成:高橋 裕志郎 - ライセンス: - This source file is subject to version 3.0 of the PHP license, - that is available at http://www.php.net/license/3_0.txt - If you did not receive a copy of the PHP license and are unable - to obtain it through the world-wide-web, please send a note to - license@php.net so we can mail you a copy immediately. - 問い合わせ先: - yujiro@rakuto.net - http://rakuto.net/ - Copyright (C) 2003-2006 'rakuto.net'. All Rights Reserved. - 注意事項: - OpenSSL 関数を使用するためには、OpenSSL パッケージがインストール - されていることを要します。 - http://jp2.php.net/manual/ja/ref.openssl.php - 更新履歴: - [2006/06/26] 作成 - ======================================================================== */ if (!defined('RKT_OPENSSL_NUMBITS')){ /** * 鍵作成時のbit数 * @const RKT_OPENSSL_NUMBITS */ define('RKT_OPENSSL_NUMBITS', 512); } /** * OpenSSL暗号クラス * * @author 高橋 裕志郎 <yujiro@rakuto.net> * @package RKT_openssl * @access public * @version 0.1 */ class RKT_openssl { /** * パスフレーズ * @access private * @var string */ var $pass_phrase = ''; /** * 秘密鍵のリソース * @access private * @var resource */ var $private_key = null; /** * 公開鍵 * @access private * @var string */ var $public_key = ''; /** * コンストラクタ * * @access public * @param string $source * @return void */ function RKT_openssl($pass_phrase) { $this->pass_phrase = $pass_phrase; $private_key = '-----BEGIN RSA PRIVATE KEY-----'."\n". 'Proc-Type: 4,ENCRYPTED'."\n". 'DEK-Info: DES-EDE3-CBC,8154CDF668046AB2'."\n". "\n". 'HwFb+62bd+VjD3lDll6nROkXi77IsceYhNd6EkpuDGh7J7tT27abwc235TIyvq5m'."\n". '+9bQdjr7cpUJlG0nL3RzQwPH5u0ReNTcRSQZIu07swXWoEc1UPNrym8VdGQFKg4v'."\n". '0LTt3CaepvItI7uQeY1X9LlZSa3AaD1kpNPS76t/DIobY3nTe4lLCkSK79orGAG/'."\n". '+2iYvReTs2xW6FLeTFgg4upFNynUN3aLiizGq7E4xWwfaFzlD4DXszs/yCPGmZmS'."\n". 'p33wixkqhg6i9gE6SbDvotHQLULTXT/37qBNwX/RDlHhw536VmEQzTB50vaG76va'."\n". '3ePaH+RwBHtkJGSY+YRpI3aMrFFUV9NCVHE1IvQtxooG2LYRr61t3tScLgZQ/256'."\n". 'i2GR7BaUJ908MDysqhB5z0a2dKDqO5Nvk8CHIKfLyhU='."\n". '-----END RSA PRIVATE KEY-----'."\n"; $this->private_key = openssl_get_privatekey($private_key, $this->pass_phrase); $this->public_key = '-----BEGIN PUBLIC KEY-----'."\n". 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMa8IYdy5EgAlH9xZzh7RRd7WvL5MUbc'."\n". 'jfy+ehP1Ilh7aH/A6JKTBe8ejlfhjbFOp8CeOLtqDVrAEOn1b7PulsMCAwEAAQ=='."\n". '-----END PUBLIC KEY-----'."\n"; } /** * 秘密鍵ファイルの読み込み * * @access public * @param string $filename * @return boolean */ function readPrivateKey($filename) { if (!file_exists ($filename)){ return false; } /* ファイルの中身を取得 */ $handle = fopen($filename, 'r'); if (!is_resource($handle)) { return false; } $private_key = fread($handle, filesize($filename)); fclose($handle); $this->private_key = openssl_get_privatekey($private_key, $this->pass_phrase); return true; } /** * 秘密鍵ファイルの読み込み * * @access public * @param string $filename * @return boolean */ function readPublicKey($filename) { if (!file_exists ($filename)){ return false; } /* ファイルの中身を取得 */ $handle = fopen($filename, 'r'); if (!is_resource($handle)) { return false; } $this->public_key = fread($handle, filesize($filename)); fclose($handle); return true; } /** * データの暗号化 * * @access public * @param string $plain * @param integer $numbits * @return binary 暗号化されたバイナリデータ */ function encrypt($plain, $numbits=RKT_OPENSSL_NUMBITS) { $maxlength = array( '256'=> 21, '512'=> 53, '1024'=> 117, ); $result = ''; while ($plain) { $crypted = ''; $data = substr($plain, 0, $maxlength[$numbits]); $plain = substr($plain, $maxlength[$numbits]); openssl_public_encrypt($data, $crypted, $this->public_key); $result .= $crypted; } return $result; } /** * データの復号化 * * @access public * @param string $crypted * @param integer $numbits * @return string 複合化されたデータ */ function decrypt($crypted, $numbits=RKT_OPENSSL_NUMBITS) { $maxlength = array( '256'=> 32, '512'=> 64, '1024'=> 128, ); $result = ''; while ($crypted) { $plain = ''; $data = substr($crypted, 0, $maxlength[$numbits]); $crypted = substr($crypted, $maxlength[$numbits]); openssl_private_decrypt($data, $plain, $this->private_key); $result .= $plain; } return $result; } } // RKT_openssl
実行ソースコード
define('GPG_COMMAND_PATH','/usr/local/bin/'); define('ERROR_LOG_FILE','error.log'); // パスフレーズ define('PASS_PHRASE', '57db199ed97ce210989424d4f9c7e1bb'); require_once 'Benchmark/Timer.php'; include_once './libs/rkt_gpg.php'; $crypt = new RKT_gpg(PASS_PHRASE); $data = '暗号化複合化テスト'; $timer = new Benchmark_Timer; // オブジェクトの生成 $timer->start(); // ベンチスタート /* 暗号化と復号化 */ $encrypted = $crypt->encrypt($data); $decrypted = $crypt->decrypt($encrypted); $timer->stop(); // ベンチストップ $profile = $timer->getProfiling(); //結果を連想配列に格納 $diff = (double)$profile[1]['diff'];
