php-tool-box:code-book
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン最新のリビジョン両方とも次のリビジョン | ||
php-tool-box:code-book [2009/05/08 22:04] – 0.1.1 phpタグ誤りを訂正 dgbadmin | php-tool-box:code-book [2011/09/16 03:56] – 0.2.1 暗号鍵長チェックを追加 dgbadmin | ||
---|---|---|---|
行 20: | 行 20: | ||
===== 使い方の例 ===== | ===== 使い方の例 ===== | ||
<code php> | <code php> | ||
- | $key = ' | + | <?php |
+ | require(' | ||
+ | |||
+ | $key = ' | ||
$text = ' | $text = ' | ||
- | + | ||
- | + | ||
- | /* AES、CBC、Null文字でPAD */ | + | /* AES(RIJNDAEL128)、CBC、PKCS#5でPAD */ |
- | $c1 = new CodeBook(); | + | $codeBook |
- | list($encrypted, | + | list($encrypted, |
- | $decrypted = $c1-> | + | $decrypted = $codeBook-> |
- | + | ||
- | echo $text . ' => ' . $decrypted; | + | // => " |
+ | echo $text . ' => ' . $encrypted . ' => ' . $decrypted; | ||
echo '< | echo '< | ||
- | echo var_dump($text === $decrypted); | + | echo var_dump($text === $decrypted); |
+ | |||
echo '< | echo '< | ||
- | + | ||
- | + | ||
- | /* Blowfish、CBC、PKCS#5でPAD */ | + | /* RIJNDAEL256、ECB、Null文字でPAD */ |
- | $c2 = new CodeBook(MCRYPT_BLOWFISH); | + | $codeBook |
- | $padded = $c2-> | + | list($encrypted) = $codeBook-> |
- | list($encrypted, $iv) = $c2-> | + | $decrypted |
- | $decryptedAndPadded | + | |
- | $decrypted = $c2-> | + | // => " |
- | + | echo $text . ' => ' . $encrypted . ' => ' . $decrypted; | |
- | echo $text . ' => ' . $decrypted; | + | |
echo '< | echo '< | ||
- | echo var_dump($text === $decrypted); | + | echo var_dump($text === $decrypted); |
+ | |||
echo '< | echo '< | ||
- | + | ||
- | + | ||
- | /* RIJNDAEL256、ECB、スペースでPAD */ | + | /* Blowfish、CBC、スペースでPAD */ |
- | $c3 = new CodeBook(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); | + | $codeBook |
- | $padded = $c3-> | + | list($encrypted, $iv) = $codeBook-> |
- | list($encrypted) = $c3-> | + | $decrypted = $codeBook-> |
- | $decrypted = $c3-> | + | |
- | + | // => " | |
- | echo $text . ' => ' . $decrypted; | + | echo $text . ' => ' . $encrypted . ' => ' . $decrypted; |
echo '< | echo '< | ||
- | echo var_dump($text === $decrypted); | + | echo var_dump($text === $decrypted); |
</ | </ | ||
行 67: | 行 70: | ||
/** | /** | ||
| | ||
- | | + | |
| | ||
- | | + | |
| | ||
*/ | */ | ||
class CodeBook { | class CodeBook { | ||
- | | + | /** PKCS# |
- | private $_mode; | + | const PAD_PKCS5 = ' |
- | + | /** Null文字でパディング */ | |
- | /** | + | const PAD_NULL = " |
- | | + | /** スペースでパディング */ |
- | | + | const PAD_SPACE = ' '; |
- | | + | |
- | | + | private $_cipher; |
- | | + | private $_mode; |
- | | + | private $_padding; |
- | | + | |
- | public function __construct($cipher = MCRYPT_RIJNDAEL_128, | + | /** |
- | $this-> | + | * コンストラクタ |
- | $this-> | + | * デフォルトは、AES(ブロック長128bit)、CBCモード、PKCS# |
- | } | + | * @param |
- | /** | + | * @param |
- | | + | |
- | | + | * @see http://www.php.net/ |
- | | + | * @see http://www.php.net/ |
- | | + | */ |
- | | + | public function __construct($cipher = MCRYPT_RIJNDAEL_128, |
- | | + | $this-> |
- | | + | $this-> |
- | public function encrypt($key, | + | $this-> |
- | if (!$iv) { | + | } |
- | $iv = $this->_getRandIV(); | + | /** |
- | } | + | * 暗号化する |
- | $bin = mcrypt_encrypt($this-> | + | * |
- | return array(bin2hex($bin), | + | * @param |
- | } | + | * @param |
- | /** | + | * @param |
- | | + | * @return array |
- | * mcrypt_encrypt()のデフォルトのパディング文字は" | + | */ |
- | * @param | + | public function encrypt($key, |
- | | + | $this-> |
- | | + | |
- | * @param | + | if (!$iv) { |
- | * @return string | + | $iv = $this->_getRandIv(); |
- | | + | } |
- | public function decrypt($key, | + | |
- | $bin = self::hex2bin($encrypted); | + | if ($this-> |
- | if ($iv) { | + | $encryptee = $this-> |
- | $iv = self::hex2bin($iv); | + | } else { |
- | } else { | + | $encryptee = $this-> |
- | $iv = $this->_getRandIV(); // | + | } |
- | } | + | |
- | $decrypted = mcrypt_decrypt($this-> | + | $bin = mcrypt_encrypt($this-> |
- | if ($trimChar !== false) { | + | return array(bin2hex($bin), |
- | $decrypted = rtrim($decrypted, | + | } |
- | } | + | /** |
- | return $decrypted; | + | * 復号する |
- | } | + | * @param |
- | /** | + | * @param |
- | | + | * @param |
- | | + | * @return string |
- | | + | */ |
- | | + | public function decrypt($key, |
- | | + | $this-> |
- | public function pad($data, $padChar) { | + | $bin = $this->hex2bin($encrypted); |
- | $size = $this-> | + | |
- | return str_pad($data, | + | if ($iv) { |
- | } | + | $iv = $this->hex2bin($iv); |
- | /** | + | } else { |
- | | + | $iv = $this->_getRandIv(); // |
- | | + | } |
- | | + | |
- | | + | $decrypted = mcrypt_decrypt($this-> |
- | public function padPkcs5($data) { | + | |
- | $size = $this-> | + | if ($this-> |
- | $padLen = $size - (strlen($data) % $size); | + | $decrypted = $this-> |
- | return $data . str_repeat(chr($padLen), | + | } else { |
- | } | + | $decrypted = rtrim($decrypted, |
- | /** | + | } |
- | | + | |
- | | + | return $decrypted; |
- | | + | } |
- | | + | /** |
- | public function trimPkcs5($data) { | + | * ブロック長に合わせてパディングする |
- | return substr($data, | + | * @param |
- | } | + | * @param |
- | /** | + | * @return string |
- | | + | */ |
- | | + | public function pad($data, $padChar) { |
- | | + | $size = $this-> |
- | | + | return str_pad($data, |
- | public | + | } |
- | return pack(' | + | /** |
- | } | + | * PKCS# |
- | + | * @param | |
- | private function | + | * @return string |
- | srand(); | + | */ |
- | return mcrypt_create_iv($this-> | + | public function padPkcs5($data) { |
- | } | + | $size = $this-> |
- | + | $padLen = $size - (strlen($data) % $size); | |
- | private function _getBlockSize() { | + | return $data . str_repeat(chr($padLen), |
- | return mcrypt_get_iv_size($this-> | + | } |
- | } | + | /** |
+ | * PKCS# | ||
+ | * @param | ||
+ | * @return string | ||
+ | */ | ||
+ | public function trimPkcs5($data) { | ||
+ | return substr($data, | ||
+ | } | ||
+ | /** | ||
+ | * hex化したデータをバイナリに変換する(bin2hex()の反対) | ||
+ | * @param | ||
+ | * @return string | ||
+ | */ | ||
+ | public function hex2bin($hex) { | ||
+ | return pack(' | ||
+ | } | ||
+ | /** | ||
+ | | ||
+ | * @param | ||
+ | * @throws Exception | ||
+ | */ | ||
+ | private function | ||
+ | $sizes = mcrypt_module_get_supported_key_sizes($this-> | ||
+ | |||
+ | // | ||
+ | if ($sizes && !in_array(strlen($key), | ||
+ | throw new Exception(" | ||
+ | } | ||
+ | } | ||
+ | /** | ||
+ | * ランダムな初期化ベクトル(IV)を生成する | ||
+ | * @return string | ||
+ | */ | ||
+ | private function _getRandIv() { | ||
+ | srand(); | ||
+ | return mcrypt_create_iv($this-> | ||
+ | } | ||
+ | /** | ||
+ | * 暗号アルゴリズムと暗号モードに応じたブロックサイズを取得する | ||
+ | * @return integer ブロックサイズ | ||
+ | */ | ||
+ | private function _getBlockSize() { | ||
+ | return mcrypt_get_iv_size($this-> | ||
+ | } | ||
} | } | ||
</ | </ | ||
php-tool-box/code-book.txt · 最終更新: 2020/10/25 12:07 by dgbadmin