PHP 中 OpenSSL 与 Mcrypt 加密算法效率比较
最近因为工作需要,要对过兆的大文件进行3DES加密。在网上找了好多加解密的方法,结果并没有什么选择的余地(OpenSSL真的拥有超级牛力?)。不多说了,看看在PHP中Mcrypto与OpenSSL的对决吧!
一、测试环境准备
$ cat /etc/system-release
CentOS release 6.4 (Final)
$ uname -a
Linux usvr002.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC
2013 x86_64 GNU/Linux
$ rpm -qa | grep "openssl"
rpm -qa | grep -i "openssl"
openssl-1.0.1e-16.el6_5.4.x86_64
openssl-devel-1.0.1e-16.el6_5.4.x86_64
pyOpenSSL-0.10-2.el6.x86_64
openssl098e-0.9.8e-17.el6.centos.2.x86_64
$ rpm -qa | grep "php"
php-cli-5.4.25-2.el6.remi.x86_64
php-mcrypt-5.4.25-2.el6.remi.x86_64
php-common-5.4.25-2.el6.remi.x86_64
php-5.4.25-2.el6.remi.x86_64
二、加密速度比较
先来看看OpenSSL中各种DES和AES的数度比较吧:
$ openssl speed des aes
Doing des cbc for 3s on 16 size blocks: 9642571 des cbc's in 2.98s
Doing des cbc for 3s on 64 size blocks: 2527420 des cbc's in 2.99s
Doing des cbc for 3s on 256 size blocks: 592201 des cbc's in 2.98s
Doing des cbc for 3s on 1024 size blocks: 122972 des cbc's in 2.93s
Doing des cbc for 3s on 8192 size blocks: 16906 des cbc's in 2.97s
Doing des ede3 for 3s on 16 size blocks: 3467602 des ede3's in 2.98s
Doing des ede3 for 3s on 64 size blocks: 910272 des ede3's in 2.98s
Doing des ede3 for 3s on 256 size blocks: 192319 des ede3's in 2.97s
Doing des ede3 for 3s on 1024 size blocks: 49983 des ede3's in 2.96s
Doing des ede3 for 3s on 8192 size blocks: 5577 des ede3's in 2.98s
Doing aes-128 cbc for 3s on 16 size blocks: 13118363 aes-128 cbc's in 2.96s
Doing aes-128 cbc for 3s on 64 size blocks: 3854938 aes-128 cbc's in 2.98s
Doing aes-128 cbc for 3s on 256 size blocks: 939551 aes-128 cbc's in 2.97s
Doing aes-128 cbc for 3s on 1024 size blocks: 509362 aes-128 cbc's in 2.97s
Doing aes-128 cbc for 3s on 8192 size blocks: 59653 aes-128 cbc's in 2.97s
Doing aes-192 cbc for 3s on 16 size blocks: 10306361 aes-192 cbc's in 2.83s
Doing aes-192 cbc for 3s on 64 size blocks: 3184489 aes-192 cbc's in 2.97s
Doing aes-192 cbc for 3s on 256 size blocks: 757061 aes-192 cbc's in 2.97s
Doing aes-192 cbc for 3s on 1024 size blocks: 428066 aes-192 cbc's in 2.97s
Doing aes-192 cbc for 3s on 8192 size blocks: 37443 aes-192 cbc's in 2.97s
Doing aes-256 cbc for 3s on 16 size blocks: 4759675 aes-256 cbc's in 3.02s
Doing aes-256 cbc for 3s on 64 size blocks: 2045784 aes-256 cbc's in 2.97s
Doing aes-256 cbc for 3s on 256 size blocks: 541610 aes-256 cbc's in 2.97s
Doing aes-256 cbc for 3s on 1024 size blocks: 315557 aes-256 cbc's in 2.98s
Doing aes-256 cbc for 3s on 8192 size blocks: 46577 aes-256 cbc's in 2.96s
(略)
The 'numbers' are in 1000s of bytes per second processed.
type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes
des cbc 51772.19k 54098.62k 50873.64k 42977.25k 46630.96k
des ede3 18618.00k 19549.47k 16576.99k 17291.42k 15331.14k
aes-128 cbc 70910.07k 82790.61k 80984.87k 175618.41k 164537.84k
aes-192 cbc 58269.18k 68621.99k 65255.09k 147589.09k 103277.12k
aes-256 cbc 25216.82k 44084.23k 46684.23k 108433.01k 128904.99k
好了!这比自己实现要快得多了。下面来看看PHP中OpenSSL与Mcrypt支持的加密类型:
# OpenSSL
$ php -r "print_r(openssl_get_cipher_methods());"
Array (
[0] => AES-128-CBC,
[1] => AES-128-CFB,
[2] => AES-128-CFB1,
[3] => AES-128-CFB8,
[4] => AES-128-CTR,
[5] => AES-128-ECB,
[6] => AES-128-OFB,
[7] => AES-128-XTS,
[8] => AES-192-CBC,
[9] => AES-192-CFB,
[10] => AES-192-CFB1,
(略)
[165] => rc4,
[166] => rc4-40,
[167] => rc4-hmac-md5,
[168] => seed-cbc,
[169] => seed-cfb,
[170] => seed-ecb,
[171] => seed-ofb
)
# Mcrypt
$ php -r "print_r(mcrypt_list_algorithms());"
Array (
[0] => cast-128,
[1] => gost,
[2] => rijndael-128,
[3] => twofish,
[4] => arcfour,
[5] => cast-256,
[6] => loki97,
[7] => rijndael-192,
[8] => saferplus,
[9] => wake,
[10] => blowfish-compat,
[11] => des,
[12] => rijndael-256,
[13] => serpent,
[14] => xtea,
[15] => blowfish,
[16] => enigma,
[17] => rc2,
[18] => tripledes
)
从加密种类数量来说OpenSSL甩Mcrypto足足80条街。下面来看看实际应用中两个库中同类加密算法的速度吧:
<?php $n = 1000000; $message = '魔法少女まどか☆マギカ'; $key = 'Soul Gem'; $method = 'DES-ECB'; // OpenSSL $start = microtime(true); for($i=0; $i<$n; ++$i) { $crypto = openssl_encrypt( $message, $method, $key ); } echo "encrypt message = {$crypto} \n"; echo "openssl {$n} encrypt time = " . ( microtime(true) - $start ) . "\n"; $start = microtime(true); for($i=0; $i<$n; ++$i) { $message = openssl_decrypt( $crypto, $method, $key ); } echo "decrypt message = {$message} \n"; echo "openssl {$n} decrypt time = " . ( microtime(true) - $start ) . "\n"; // Mcrypt
$start = microtime(true); for($i=0; $i<$n; ++$i) { $crypto = base64_encode(mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_ECB)); } echo "encrypt message = {$crypto} \n"; echo "mcrypt {$n} encrypt time = " . ( microtime(true) - $start ) . "\n"; $start = microtime(true); for($i=0; $i<$n; ++$i) { $message = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($crypto), MCRYPT_MODE_ECB); } echo "decrypt message = {$message} \n"; echo "mcrypt {$n} decrypt time = " . ( microtime(true) - $start ) . "\n"; ?>
$ php des.php
encrypt message = s3TrEokf/1xTgoHIimEtUi8s5hNgultndusqMud+XGa0KXi6EbHF4w==
openssl 1000000 encrypt time = 1.7591309547424
decrypt message = 魔法少女まどか☆マギカ
openssl 1000000 decrypt time = 1.9514858722687
encrypt message = s3TrEokf/1xTgoHIimEtUi8s5hNgultndusqMud+XGZW+smvwY2mXw==
mcrypt 1000000 encrypt time = 231.36626815796
decrypt message = 魔法少女まどか☆マギカ
mcrypt 1000000 decrypt time = 218.42488908768
三、测试总结
很明显的结果:火箭 vs. 自行车!