ゼロと無限の間に

フリーでオープンソースなJavaScriptとかPHPとか。

ユーザ用ツール

サイト用ツール


php-tool-box:code-book

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
php-tool-box:code-book [2009/04/22 22:33] dgbadminphp-tool-box:code-book [2020/10/25 12:07] (現在) dgbadmin
行 1: 行 1:
 ====== PHPでお手軽に暗号化、復号する - CodeBook.php ====== ====== PHPでお手軽に暗号化、復号する - CodeBook.php ======
 +
 +===== 注意 =====
 +
 +**CodeBook.phpが内部的に使用しているphp-mcryptは、[[http://0-oo.net/php-manual/intro.mcrypt.html|PHP 7.1.0 で 非推奨 となり、 PHP 7.2.0 で削除]]されました。**
 +
 +----
  
 コメントと更新履歴は[[http://0-oo.net/log/category/php-tool-box/code-book/|Code Book Archive - ゼロと無限の間のログ]]へどうぞ。 コメントと更新履歴は[[http://0-oo.net/log/category/php-tool-box/code-book/|Code Book Archive - ゼロと無限の間のログ]]へどうぞ。
行 20: 行 26:
 ===== 使い方の例 ===== ===== 使い方の例 =====
 <code php> <code php>
-$key = '秘密の鍵';+<?php 
 +require('CodeBook.php'); 
 + 
 +$key = '秘密の鍵128b';
 $text = '秘密のメッセージ'; $text = '秘密のメッセージ';
- +  
- +  
-/* AES、CBC、Null文字でPAD */ +/* AES(RIJNDAEL128)、CBC、PKCS#5でPAD */ 
-$c1 = new CodeBook(); +$codeBook = new CodeBook(); 
-list($encrypted, $iv) = $c1->encrypt($key, $text);  //暗号化 +list($encrypted, $iv) = $codeBook->encrypt($key, $text); //暗号化 
-$decrypted = $c1->decrypt($key, $encrypted, $iv);   //復号 +$decrypted = $codeBook->decrypt($key, $encrypted, $iv); //復号 
- +  
-echo $text . ' => ' . $decrypted;   // => "秘密のメッセージ => 秘密のメッセージ"+// => "秘密のメッセージ => (略) => 秘密のメッセージ" 
 +echo $text . ' => ' . $encrypted . ' => ' . $decrypted;
 echo '<br />'; echo '<br />';
-echo var_dump($text === $decrypted);    // => bool(true) +echo var_dump($text === $decrypted); // => bool(true) 
 + 
 echo '<hr />'; echo '<hr />';
- +  
- +  
-/* BlowfishCBCPKCS#5でPAD */ +/* RIJNDAEL256ECBNull文字でPAD */ 
-$c2 = new CodeBook(MCRYPT_BLOWFISH); +$codeBook = new CodeBook(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB, CodeBook::PAD_NULL); 
-$padded = $c2->padPkcs5($text); //パディング +list($encrypted) = $codeBook->encrypt($key, $text); //暗号化(ECBはIV不要) 
-list($encrypted, $iv) = $c2->encrypt($key, $padded);    //暗号化 +$decrypted = $codeBook->decrypt($key, $encrypted); //復号 
-$decryptedAndPadded = $c2->decrypt($key, $encrypted, $iv);  //復号 +  
-$decrypted = $c2->trimPkcs5($decryptedAndPadded);   //PADを除去 +// => "秘密のメッセージ => (略) => 秘密のメッセージ" 
- +echo $text . ' => ' . $encrypted . ' => ' . $decrypted;
-echo $text . ' => ' . $decrypted;   // => "秘密のメッセージ => 秘密のメッセージ"+
 echo '<br />'; echo '<br />';
-echo var_dump($text === $decrypted);    // => bool(true) +echo var_dump($text === $decrypted); // => bool(true) 
 + 
 echo '<hr />'; echo '<hr />';
- +  
- +  
-/* RIJNDAEL256ECB、スペースでPAD */ +/* BlowfishCBC、スペースでPAD */ 
-$c3 = new CodeBook(MCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB); +$codeBook = new CodeBook(MCRYPT_BLOWFISHMCRYPT_MODE_CBCCodeBook::PAD_SPACE); 
-$padded = $c3->pad($text' '); //パディング +list($encrypted, $iv) = $codeBook->encrypt($key, $text); //暗号化 
-list($encrypted) = $c3->encrypt($key, $padded); //暗号化 +$decrypted = $codeBook->decrypt($key, $encrypted, $iv); //復号 
-$decrypted = $c3->decrypt($key, $encrypted, null, ' '); //復号&PADを除去 +  
- +// => "秘密のメッセージ => (略) => 秘密のメッセージ" 
-echo $text . ' => ' . $decrypted;   // => "秘密のメッセージ => 秘密のメッセージ"+echo $text . ' => ' . $encrypted . ' => ' . $decrypted;
 echo '<br />'; echo '<br />';
-echo var_dump($text === $decrypted);    // => bool(true)+echo var_dump($text === $decrypted); // => bool(true)
 </code> </code>
  
行 67: 行 76:
 /** /**
   CodeBook.php   CodeBook.php
-  @version   0.1.0+  @version   0.2.1
   @see       http://0-oo.net/sbox/php-tool-box/code-book   @see       http://0-oo.net/sbox/php-tool-box/code-book
-  @copyright 2009 dgbadmin@gmail.com+  @copyright 2009-2011 dgbadmin@gmail.com
   @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)   @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)
  */  */
-<?php 
 class CodeBook { class CodeBook {
-    private $_cipher; + /** PKCS#5でパディング */ 
-    private $_mode; + const PAD_PKCS5 = 'PAD_PKCS5'; 
-     + /** Null文字でパディング */ 
-    /** + const PAD_NULL = "\0"; 
-     *  コンストラクタ + /** スペースでパディング */ 
-     *  デフォルトは、AES(ブロック長128bits/CBCモード + const PAD_SPACE = ' '; 
-     *  @param  string  $cipher (省略可)暗号アルゴリズム +  
-     *  @param  string  $mode   (省略可)暗号モード + private $_cipher; 
-     *  @see http://jp2.php.net/manual/ja/mcrypt.ciphers.php + private $_mode; 
-     *  @see http://jp2.php.net/manual/ja/mcrypt.constants.php + private $_padding; 
-     */ +  
-    public function __construct($cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC) { + /** 
-        $this->_cipher = $cipher; +  *  コンストラクタ 
-        $this->_mode = $mode; +  *  デフォルトは、AES(ブロック長128bitCBCモード、PKCS#5でパディング 
-    +  *  @param  string  $cipher     (省略可暗号アルゴリズム 
-    /** +  *  @param  string  $mode       (省略可暗号モード 
-     *  暗号化する +   @param  string  $padding    (省略可)パディング方法(このクラスの定数 or 文字) 
-     *  IVを渡さない場合、ランダムなIVが生成される +  *  @see http://www.php.net/manual/ja/mcrypt.ciphers.php 
-     *  @param  string  $key        暗号鍵 +  *  @see http://www.php.net/manual/ja/mcrypt.constants.php 
-     *  @param  string  $encryptee  暗号化するデータ +  */ 
-     *  @param  string  $iv         (省略可)初期化ベクトル + public function __construct($cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC, $padding = self::PAD_PKCS5) { 
-     *  @return array   hex化した暗号化済みデータと、hex化したIV + $this->_cipher = $cipher; 
-     */ + $this->_mode = $mode; 
-    public function encrypt($key, $encryptee, $iv = null) { + $this->_padding = $padding; 
-        if (!$iv) { +
-            $iv = $this->_getRandIV(); + /** 
-        +  *  暗号化する 
-        $bin = mcrypt_encrypt($this->_cipher, $key, $encryptee, $this->_mode, $iv); +  *  初期化ベクトル(IVを渡さない場合、ランダムな初期化ベクトルを生成る 
-        return array(bin2hex($bin), bin2hex($iv)); +  *  @param  string  $key        暗号鍵 
-    +  *  @param  string  $encryptee  暗号化するデータ 
-    /** +  *  @param  string  $iv         省略可初期化ベクトル(ECBでは不要) 
-     *  復号する +  *  @return array   hex化した暗号化済みデータと、hex化した初期化ベクトル(IV 
-     *  mcrypt_encrypt()のデフォルトのパディング文字は"\0"(Null文字) +  */ 
-     *  @param  string  $key        号鍵 + public function encrypt($key, $encryptee, $iv = null) { 
-     *  @param  string  $encrypted  暗号化されたデータ + $this->_checkKeySize($key); 
-     *  @param  string  $iv         (省略可)hex化した初期化ベクトル(ECBでは不要) +  
-     *  @param  string  $trimChar   (省略可)除去するパディング文字 + if (!$iv) { 
-     *  @return string  復号したデータ + $iv = $this->_getRandIv(); 
-     */ +
-    public function decrypt($key, $encrypted, $iv = null, $trimChar = "\0") { +  
-        $bin = self::hex2bin($encrypted); + if ($this->_padding === self::PAD_PKCS5) { 
-        if ($iv) { + $encryptee = $this->padPkcs5($encryptee); 
-            $iv = self::hex2bin($iv); + } else { 
-        } else { + $encryptee = $this->pad($encryptee, $this->_padding); 
-            $iv = $this->_getRandIV();  //Warningを出さないためのダミーのIV +
-        +  
-        $decrypted = mcrypt_decrypt($this->_cipher, $key, $bin, $this->_mode, $iv); + $bin = mcrypt_encrypt($this->_cipher, $key, $encryptee, $this->_mode, $iv); 
-        if ($trimChar !== false) { + return array(bin2hex($bin), bin2hex($iv)); 
-            $decrypted = rtrim($decrypted, $trimChar); +
-        + /** 
-        return $decrypted; +  *  復号する 
-    + *  @param  string  $key        号鍵 
-    /** +  *  @param  string  $encrypted  暗号化されてhex化されたデータ 
-     *  ブロック長に合わせてパディングする +  *  @param  string  $iv         省略可hex化した初期化ベクトル(ECBでは不要) 
-     *  @param  string  $data       パディング対象のデータ + *  @return string  復号したデータ 
-     *  @param  string  $padChar    パディング文字 +  */ 
-     *  @return string  パディングしたデータ + public function decrypt($key, $encrypted, $iv = null) { 
-     */ + $this->_checkKeySize($key); 
-    public function pad($data, $padChar) { + $bin = $this->hex2bin($encrypted); 
-        $size = $this->_getBlockSize(); +  
-        return str_pad($data, ceil(strlen($data) / $size) * $size, $padChar); + if ($iv) { 
-    + $iv = $this->hex2bin($iv); 
-    /** + } else { 
-     *  PKCS#5でパディングする + $iv = $this->_getRandIv();  //Warningを出さないためのダミーのIV 
-     *  @param  string  $data   パディング対象のデータ +
-     *  @return string  パディングしたデータ +  
-     */ + $decrypted = mcrypt_decrypt($this->_cipher, $key, $bin, $this->_mode, $iv); 
-    public function padPkcs5($data) { +  
-        $size = $this->_getBlockSize(); + if ($this->_padding === self::PAD_PKCS5) { 
-        $padLen = $size - (strlen($data) % $size); + $decrypted = $this->trimPkcs5($decrypted); 
-        return $data . str_repeat(chr($padLen), $padLen); + } else { 
-    + $decrypted = rtrim($decrypted, $this->_padding); 
-    /** +
-     *  PKCS#5のパディングを除去する +  
-     *  @param  string  $data   PKCS#5でパディングされたデータ + return $decrypted; 
-     *  @return string  パディングしたデータ +
-     */ + /** 
-    public function trimPkcs5($data) { +  *  ブロック長に合わせてパディングする 
-        return substr($data, 0, ord(substr($data, -1, 1)) * -1); +  *  @param  string  $data       パディング対象のデータ 
-    +  *  @param  string  $padChar    パディング文字 
-    /** +  *  @return string  パディングしたデータ 
-     *  hex化したデータをバイナリに変換する +  */ 
-     *  @param  string  $hex    hex化されたデータ + public function pad($data, $padChar) { 
-     *  @return string  バイナリになったデータ + $size = $this->_getBlockSize(); 
-     */ + return str_pad($data, ceil(strlen($data) / $size) * $size, $padChar); 
-    public static function hex2bin($hex) { +
-        return pack('H*', $hex); + /** 
-    +  *  PKCS#5でパディングする 
-     +  *  @param  string  $data   パディング対象のデータ 
-    private function _getRandIV() { +  *  @return string  パディングしたデータ 
-        srand(); +  */ 
-        return mcrypt_create_iv($this->_getBlockSize(), MCRYPT_RAND); + public function padPkcs5($data) { 
-    + $size = $this->_getBlockSize(); 
-     + $padLen = $size - (strlen($data) % $size); 
-    private function _getBlockSize() { + return $data . str_repeat(chr($padLen), $padLen); 
-        return mcrypt_get_iv_size($this->_cipher, $this->_mode); +
-    }+ /** 
 +  *  PKCS#5のパディングを除去する 
 +  *  @param  string  $data   PKCS#5でパディングされたデータ 
 +  *  @return string  パディングしたデータ 
 +  */ 
 + public function trimPkcs5($data) { 
 + return substr($data, 0, ord(substr($data, -1, 1)) * -1); 
 +
 + /** 
 +  *  hex化したデータをバイナリに変換する(bin2hex()の反対) 
 +  *  @param  string  $hex    hex化されたデータ 
 +  *  @return string  バイナリになったデータ 
 +  */ 
 + public function hex2bin($hex) { 
 + return pack('H*', $hex); 
 +
 + /** 
 +   暗号鍵の長さをチェックする 
 + *  @param  string  $key    暗号鍵 
 + *  @throws Exception   長さが不正な場合に例外を投げる 
 + */ 
 + private function _checkKeySize($key) { 
 + $sizes = mcrypt_module_get_supported_key_sizes($this->_cipher); 
 +  
 + //可変の場合は空なのでチェックしない 
 + if ($sizes && !in_array(strlen($key), $sizes)) { 
 + throw new Exception("Invalid key length ($key)"); 
 +
 +
 + /** 
 + *  ランダムな初期化ベクトル(IV)を生成する 
 + *  @return string  生成した初期化ベクトル 
 + */ 
 + private function _getRandIv() { 
 + srand(); 
 + return mcrypt_create_iv($this->_getBlockSize(), MCRYPT_RAND); 
 +
 + /** 
 + *  暗号アルゴリズムと暗号モードに応じたブロックサイズを取得する 
 + *  @return integer ブロックサイズ 
 + */ 
 + private function _getBlockSize() { 
 + return mcrypt_get_iv_size($this->_cipher, $this->_mode); 
 + }
 } }
 </code> </code>
  
php-tool-box/code-book.1240407188.txt.gz · 最終更新: 2009/04/22 22:33 by dgbadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki