Packagist を覗いていると「windwalker/crypt」というのがあったので試してみる
composer require 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;
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;
crypt_blowfish.php | crypt_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) |