PHPプログラムに関する各種メモ書き

phpで文字列の可逆暗号(暗号化/復号化)を行う(その2)

Packagist を覗いていると「windwalker/crypt」というのがあったので試してみる

● windwalker/crypt のインストール

composer require windwalker/crypt

● windwalker/crypt を使用した暗号化/復号化

crypt_blowfish.php

require_once "vendor/autoload.php";
use Windwalker\Crypt\Cipher\BlowfishCipher;
use Windwalker\Crypt\Crypt;

$crypt = new Crypt(new BlowfishCipher, 'My private key');
$raw       = 'あいうえおかきくけこさしすせそ';
$encrypted = $crypt->encrypt($raw);
$decrypted = $crypt->decrypt($encrypted);

print <<< DOC_END
<table border="1" style="word-break: break-all;">
	<tr><th>raw</th><td>{$raw}</td></tr>
	<tr><th>encrypted</th><td>{$encrypted}</td></tr>
	<tr><th nowrap>decrypted</th><td>{$decrypted}</td></tr>
</table>
DOC_END;

● AES-256-CBC との速度比較

crypt_aes256.php

$method = 'AES-256-CBC';
$iv_length = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_length);
$options = 0;

$raw       = 'あいうえおかきくけこさしすせそ';
$encrypted = openssl_encrypt($raw, $method, $password, $options, $iv);
$decrypted = openssl_decrypt($encrypted, $method, $password, $options, $iv);

print <<< DOC_END
<table border="1" style="word-break: break-all;">
	<tr><th>raw</th><td>{$raw}</td></tr>
	<tr><th>encrypted</th><td>{$encrypted}</td></tr>
	<tr><th nowrap>decrypted</th><td>{$decrypted}</td></tr>
</table>
DOC_END;

● ベンチマーク結果(ab , Requests per second比較。大きいほど早い)

crypt_blowfish.phpcrypt_aes256.php
45.42 [#/sec] (mean)117.17 [#/sec] (mean)
33.58 [#/sec] (mean)105.73 [#/sec] (mean)
40.69 [#/sec] (mean)105.13 [#/sec] (mean)
No.1149
06/02 19:36

edit